1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22: #include <libintl.h>
23: #include <locale.h>
24: #include <pthread.h>
25: #include <stdio.h>
26: #include <stdlib.h>
27: #include <string.h>
28:
29:
30: int result;
31:
32:
33: int flipflop;
34:
35: pthread_mutex_t lock;
36: pthread_cond_t waitqueue;
37:
38:
39:
40: static void
41: waitfor (int value)
42: {
43: if (pthread_mutex_lock (&lock))
44: exit (10);
45: while (flipflop != value)
46: if (pthread_cond_wait (&waitqueue, &lock))
47: exit (11);
48: }
49:
50:
51:
52: static void
53: setto (int value)
54: {
55: flipflop = value;
56: if (pthread_cond_signal (&waitqueue))
57: exit (20);
58: if (pthread_mutex_unlock (&lock))
59: exit (21);
60: }
61:
62: void *
63: thread1_execution (void *arg)
64: {
65: char *s;
66:
67: waitfor (1);
68: uselocale (newlocale (LC_ALL_MASK, "de_DE.ISO-8859-1", NULL));
69: setto (2);
70:
71: waitfor (1);
72: s = gettext ("beauty");
73: puts (s);
74: if (strcmp (s, "Sch\366nheit"))
75: {
76: fprintf (stderr, "thread 1 call 1 returned: %s\n", s);
77: result = 1;
78: }
79: setto (2);
80:
81: waitfor (1);
82: s = gettext ("beauty");
83: puts (s);
84: if (strcmp (s, "Sch\366nheit"))
85: {
86: fprintf (stderr, "thread 1 call 2 returned: %s\n", s);
87: result = 1;
88: }
89: setto (2);
90:
91: return NULL;
92: }
93:
94: void *
95: thread2_execution (void *arg)
96: {
97: char *s;
98:
99: waitfor (2);
100: uselocale (newlocale (LC_ALL_MASK, "fr_FR.ISO-8859-1", NULL));
101: setto (1);
102:
103: waitfor (2);
104: s = gettext ("beauty");
105: puts (s);
106: if (strcmp (s, "beaut\351"))
107: {
108: fprintf (stderr, "thread 2 call 1 returned: %s\n", s);
109: result = 1;
110: }
111: setto (1);
112:
113: waitfor (2);
114: s = gettext ("beauty");
115: puts (s);
116: if (strcmp (s, "beaut\351"))
117: {
118: fprintf (stderr, "thread 2 call 2 returned: %s\n", s);
119: result = 1;
120: }
121: setto (1);
122:
123: return NULL;
124: }
125:
126: int
127: main (void)
128: {
129: pthread_t thread1;
130: pthread_t thread2;
131:
132: unsetenv ("LANGUAGE");
133: unsetenv ("OUTPUT_CHARSET");
134: textdomain ("multithread");
135: bindtextdomain ("multithread", OBJPFX "domaindir");
136: result = 0;
137:
138: flipflop = 1;
139: if (pthread_mutex_init (&lock, NULL))
140: exit (2);
141: if (pthread_cond_init (&waitqueue, NULL))
142: exit (2);
143: if (pthread_create (&thread1, NULL, &thread1_execution, NULL))
144: exit (2);
145: if (pthread_create (&thread2, NULL, &thread2_execution, NULL))
146: exit (2);
147: if (pthread_join (thread2, NULL))
148: exit (3);
149:
150: return result;
151: }