linux中的线程没有得到预期的结果!!!!
源代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
void cleanup(void *arg)
{
printf("cleanup: %sn", (char *)arg);
}
void *thr_fn1(void *arg)
{
printf("thread 1 startn"
pthread_cleanup_push(cleanup,"thread 1 first hander"
pthread_cleanup_push(cleanup,"thread 1 second hander"
printf("thread 1 push completen"
if (arg)
return((void *)1);
pthread_cleanup_pop(0);
pthread_cleanup_pop(0);
return((void *)1);
}
void *thr_fn2(void *arg)
{
printf("thread 2 startn"
pthread_cleanup_push(cleanup,"thread 2 first hander"
pthread_cleanup_push(cleanup,"thread 2 second hander"
printf("thread 2 push completen"
if (arg)
return((void *)2);
pthread_cleanup_pop(0);
pthread_cleanup_pop(0);
return((void *)2);
}
int main(void)
{
int err;
pthread_t tid1, tid2;
void *tret;
err = pthread_create(&tid1, NULL, thr_fn1, (void *)1);
if (err != 0)
printf("can't create thread 1: %sn", strerror(err));
err = pthread_create(&tid2, NULL, thr_fn2, (void *)2);
if (err != 0)
printf("can't create thread 2: %sn", strerror(err));
err = pthread_join(tid1, &tret);
if (err != 0)
printf("can't join thread 1: %sn", strerror(err));
printf("thread 1 exit code %dn", (int)tret);
err = pthread_join(tid2, &tret);
if (err != 0)
printf("can't join thread 2: %sn", strerror(err));
printf("thread 2 exit code %dn", (int)tret);
exit(0);
}
执行结果:
thread 1 start
thread 1 push complete
thread 2 start
thread 2 push complete
thread 1 exit code 1
thread 2 exit code 2
预期结果:
thread 1 start
thread 1 push complete
thread 2 start
thread 2 push complete
cleanup: thread 2 second handler
cleanup: thread 2 first handler
thread 1 exit code 1
thread 2 exit code 2
我用gdb跟踪后发现在函数thr_fn1执行完后出现下面的信息
0x4002b2b6 in start_thread () from /lib/tls/libpthread.so.0
(gdb) s
Single stepping until exit from function start_thread,
which has no line number information.
[Switching to Thread 1073955456 (LWP 511]
我估计是这个地方出错的,请高手指点.谢谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
void cleanup(void *arg)
{
printf("cleanup: %sn", (char *)arg);
}
void *thr_fn1(void *arg)
{
printf("thread 1 startn");
pthread_cleanup_push(cleanup,(char*)"thread 1 first hander");
pthread_cleanup_push(cleanup,(char*)"thread 1 second hander");
printf("thread 1 push completen");
if (arg)
return((void *)1);
pthread_cleanup_pop(1);
pthread_cleanup_pop(1);
return((void *)1);
}
void *thr_fn2(void *arg)
{
printf("thread 2 startn");
pthread_cleanup_push(cleanup,(char*)"thread 2 first hander");
pthread_cleanup_push(cleanup,(char*)"thread 2 second hander");
printf("thread 2 push completen");
if (arg)
return((void *)2);
pthread_cleanup_pop(1);
pthread_cleanup_pop(1);
return((void *)2);
}
int main(void)
{
int err;
pthread_t tid1, tid2;
void *tret;
err = pthread_create(&tid1, NULL, thr_fn1, (void *)1);
if (err != 0)
printf("can't create thread 1: %sn", strerror(err));
err = pthread_create(&tid2, NULL, thr_fn2, (void *)0);
if (err != 0)
printf("can't create thread 2: %sn", strerror(err));
err = pthread_join(tid1, &tret);
if (err != 0)
printf("can't join thread 1: %sn", strerror(err));
printf("thread 1 exit code %dn", (int)tret);
err = pthread_join(tid2, &tret);
if (err != 0)
printf("can't join thread 2: %sn", strerror(err));
printf("thread 2 exit code %dn", (int)tret);
exit(0);
}
运行结果:
thread 1 start
thread 1 push complete
thread 2 start
thread 2 push complete
cleanup: thread 2 second hander
cleanup: thread 2 first hander
thread 1 exit code 1
thread 2 exit code 2