互斥量程序问题
为什么我运行结果不对阿,线程2也没执行,运行结果是:
===the 1st put===
thread 4294967295: 0
thread 4294967295: 0
===the 2nd put===
main thread done
但应该为:
===the 1st put===
thread 4294967295: 10
thread 4294967295: 1
thread 4294967295: 0
===the 2nd put===
thread 4294967295: 9
thread 4294967295: 2
thread 4294967295:1
main thread done
No job in the queue
多谢大家指教一下,谢谢!
1 #include <stdio.h>
2 #include <pthread.h>
3 #include <malloc.h>
4 #define MAX_ITEM 3
5
6 pthread_t tid;
7
8 typedef struct job * Job;
9
10 struct job
11 {
12 pthread_t tid;
13 Job * next;
14 int val;
15 };
16
17 pthread_mutex_t q_lock = PTHREAD_MUTEX_INITIALIZER;
18
19 int insert(Job *head, int val, pthread_t pid)
20 {
21 Job p, q;
22
23 p = *head;
24
25 if(p != NULL)
26 {
27 while(p->next != NULL)
28 p = p -> next;
29 }
30
31 q = (struct job *)malloc(sizeof(struct job));
32 if(q == NULL)
33 {
34 perror("fail to malloc");
35 return -1;
36 }
37
38 q -> next = NULL;
39 q -> val = val;
40 q -> tid = tid;
41 p -> next = q;
42
43 if(p == NULL)
44 {
45 *head = q;
46 return 0;
47 }
48
49 p -> next = q;
50
51 return 0;
52 }
53
54 void get_job(Job head, Job task, int *count)
55 {
56 struct job *p, *q;
57
58 q = head;
59
60 p = q -> next;
61
62 while(p != NULL)
63 {
64 if(tid == p->tid)
65 {
66 q->next = p->next;
67 p->next = task;
68 task = p;
69 p = q -> next;
70 *count ++;
71 }
72 else
73 {
74 q = p;
75 p = p -> next;
76 }
77 }
78 }
79
80 int free_job(Job head)
81 {
82 Job p, q;
83 for(p = head; p != NULL; p = p->next)
84 {
85 q = p;
86 p = p -> next;
87 free(q);
88 }
89
90 return 0;
91 }
92
93 void print(Job head)
94 {
95 Job p;
96
97 for(p = head; p != NULL; p = p->next)
98 printf("thread %u: %d\n", p->tid, p->val);
99 }
100
101 void * thfn(void * arg)
102 {
103 int count;
104 struct job * task = NULL;
105 pthread_t tid;
106
107 tid = pthread_self();
108
109 count = 0;
110 while(count < MAX_ITEM)
111 {
112 if(pthread_mutex_trylock(&q_lock) == 0)
113 {
114 get_job((Job) arg, task, &count);
115 pthread_mutex_unlock(&q_lock);
116 }
117
118 print((Job) arg);
119
120 if(free_job(task) == -1)
121 exit(1);
122
123 return (void *)0;
124 }
125 }
126
127 int main(void)
128 {
129 struct job * item;
130 pthread_t tid1, tid2;
131 int i, err;
132
133 item = (struct job *)malloc(sizeof(struct job));
134
135 item -> next = NULL;
136 item -> val = 0;
137 item -> tid = -1;
138
139 if(pthread_create(&tid1, NULL, thfn, item) == -1)
140 {
141 printf("fail to create thread %s\n");
142 exit(0);
143 }
144
145
146 if(pthread_create(&tid2, NULL, thfn, item) == -1)
147 {
148 printf("fail to create thread %s\n");
149 exit(0);
150 }
151
152 printf("===the 1st put===\n");
153
154 pthread_mutex_lock(&q_lock);
155 for(i = 0; i < 2; i ++)
156 {
157 if(insert(&item, i, tid1) == -1)
158 exit(1);
159 if(insert(&item, i + 1, tid2) == -1)
160 exit(1);
161 }
162
163 if(insert(&item, 10, tid1) == -1)
164 exit(1);
165
166 pthread_mutex_unlock(&q_lock);
167
168 sleep(5);
169
170 printf("===the 2nd put===\n");
171
172 pthread_mutex_lock(&q_lock);
173 if(insert(&item, 9, tid2) == -1)
174 exit(1);
175 pthread_mutex_unlock(&q_lock);
176
177 err = pthread_join(tid1, NULL);
178 if(err != 0)
179 {
180 printf("can not join thread %s\n", strerror(err));
181 exit(1);
182 }
183
184
185 err = pthread_join(tid2, NULL);
186 if(err != 0)
187 {
188 184
185 err = pthread_join(tid2, NULL);
186 if(err != 0)
187 {
188 printf("can not join thread %s\n", strerror(err));
189 exit(1);
190 }
191
192 printf("main thread done\n");
193 if(item->next == NULL)
194 printf("No job in the queue\n");
195
196 free(item);
197
198 return 0;
199 }
83,0-1 90%