当前位置: 技术问答>linux和unix
线程问题(高分相送)
来源: 互联网 发布时间:2015-03-19
本文导语: 这个程序我始终编译不过去,不知道为什么?请帮忙看看哪里错了 #include #include pthread_key_t key; void echomsg(int t) { printf("destructor excuted in thread %d,param=%dn",pthread_self(),t); } void * child1(void *arg) { ...
这个程序我始终编译不过去,不知道为什么?请帮忙看看哪里错了
#include
#include
pthread_key_t key;
void echomsg(int t)
{
printf("destructor excuted in thread %d,param=%dn",pthread_self(),t);
}
void * child1(void *arg)
{
int tid=pthread_self();
printf("thread %d entern",tid);
pthread_setspecific(key,(void *)tid);
sleep(2);
printf("thread %d returns %dn",tid,pthread_getspecific(key));
sleep(5);
}
void * child2(void *arg)
{
int tid=pthread_self();
printf("thread %d entern",tid);
pthread_setspecific(key,(void *)tid);
sleep(1);
printf("thread %d returns %dn",tid,pthread_getspecific(key));
sleep(5);
}
int main(void)
{
int tid1,tid2;
printf("hellon");
pthread_key_create(&key,echomsg);
pthread_create(&tid1,NULL,child1,NULL);
pthread_create(&tid2,NULL,child2,NULL);
sleep(10);
pthread_key_delete(key);
printf("main thread exitn");
return 0;
}
#include
#include
pthread_key_t key;
void echomsg(int t)
{
printf("destructor excuted in thread %d,param=%dn",pthread_self(),t);
}
void * child1(void *arg)
{
int tid=pthread_self();
printf("thread %d entern",tid);
pthread_setspecific(key,(void *)tid);
sleep(2);
printf("thread %d returns %dn",tid,pthread_getspecific(key));
sleep(5);
}
void * child2(void *arg)
{
int tid=pthread_self();
printf("thread %d entern",tid);
pthread_setspecific(key,(void *)tid);
sleep(1);
printf("thread %d returns %dn",tid,pthread_getspecific(key));
sleep(5);
}
int main(void)
{
int tid1,tid2;
printf("hellon");
pthread_key_create(&key,echomsg);
pthread_create(&tid1,NULL,child1,NULL);
pthread_create(&tid2,NULL,child2,NULL);
sleep(10);
pthread_key_delete(key);
printf("main thread exitn");
return 0;
}
|
类型有误,会导致warning。
请
void echomsg(int t) ---- void echomsg(void * t)
其他的 int 改为 pthread_t
编译时注意:
gcc example1.c -lpthread -o example1
等你的高分相送呀!
请
void echomsg(int t) ---- void echomsg(void * t)
其他的 int 改为 pthread_t
编译时注意:
gcc example1.c -lpthread -o example1
等你的高分相送呀!
|
文件存为:mythread.c
cc -o mythread mythread.c -lpthread
我想你肯定是没有连接线程库,以下是运行结果:
hello
thread 1026 enter
thread 2051 enter
thread 2051 returns 2051
thread 1026 returns 1026
destructor excuted in thread 2051,param=2051
destructor excuted in thread 1026,param=1026
main thread exit
cc -o mythread mythread.c -lpthread
我想你肯定是没有连接线程库,以下是运行结果:
hello
thread 1026 enter
thread 2051 enter
thread 2051 returns 2051
thread 1026 returns 1026
destructor excuted in thread 2051,param=2051
destructor excuted in thread 1026,param=1026
main thread exit
|
你怎么编译的,除了设么出错信息???
|
gz