互斥量程序问题

liyi54188 2011-08-08 12:39:02
为什么我运行结果不对阿,线程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%
...全文
119 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhang_int_int 2012-06-18
  • 打赏
  • 举报
回复
我给你看了下 是先等待 在输出 而不是先output 再wait
yong_f 2011-08-08
  • 打赏
  • 举报
回复
代码太多,分值又少
liyi54188 2011-08-08
  • 打赏
  • 举报
回复
代码里竟然有两遍err = pthread_join(tid2, NULL); if(err != 0)这样的语句,不好意思,是我复制代码过来的时候没注意复制错了,insert方法里的pid参数给了q->tid,后面printf里面用到了啊,===the 2nd put===下面有没内容不重要。只是我在insert里把item值修改都没成功,线程2也没执行啊,把sleep(5);注释掉也没用啊,结贴问题我问题解决了的帖子都结了啊,没结的都是还有没弄懂地方和问题还没解决的,以后会更早结贴的。
name_110 2011-08-08
  • 打赏
  • 举报
回复
同意楼上观点,还有,你总是只发帖,却从不结帖,这样很不好。。。

看了一下你的代码,觉得代码上关于互斥量那部分知识的问题不是很大,但是小错误一大堆:
1、你insert方法里的pid参数根本没用到,我想这应该不是你的本意把;
2、代码里竟然有两遍err = pthread_join(tid2, NULL); if(err != 0)这样的语句;
3、从逻辑上说,我觉得也不可能打印出你要的结果,因为主线程休息了5秒,两个从线程早走玩了,===the 2nd put===下面没内容是正常的。
本来想帮你找错误的,但是根本不知道需求,没法改。
还有,记住,发了帖子要结帖!
寻找fantasy 2011-08-08
  • 打赏
  • 举报
回复
同意楼上。先不要写那么多,简化一下。

23,217

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧