马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
就是一个简单的多线程例子,不在printf函数中调用getpid() retval就能正常接收到线程函数的返回,加上(如下代码)之后就不能就收到了(retval == NULL), 分成两个printf分别打印pid和thread_id也没问题,彻底晕了,我用的是Debian3.2.0-4-amd64 #1 SMP Debian 3.2.60-1+deb7u3 x86_64 GNU/Linux(uname -a), 不多说了 大家给看看吧,上代码!!!#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
void* thread_proc(void* arg)
{
printf("process %d begin thread 0x%x!\ndealing...\n",getpid(),pthread_self());
sleep(5);
printf("arg = %d\nend thread!\n", arg);
return arg;
}
int main(void)
{
pthread_t id;
void** retval;
printf("process %d thread 0x%x will call pthread_create!\n",getpid(),pthread_se lf());
int create_status = pthread_create(&id, NULL, thread_proc,(void*)1);
if(create_status)
{
printf("pthread_create failed!\n");
return -1;
}
printf("pthread_create success, will call pthread_join!\n");
int join_status = pthread_join(id, retval);
if(join_status)
{
printf("pthread_join failed!\n");
return -1;
}
printf("retval = %d\n", *retval);
printf("pthread_join return success!\nend main!\n");
return 0;
}
|