Chinaunix
标题:
apue pthread_create例子的一些疑问
[打印本页]
作者:
butterinsect
时间:
2011-02-23 17:32
标题:
apue pthread_create例子的一些疑问
#include <pthread.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
pthread_t ntid;
void printids(const char *s)
{
pid_t pid;
pthread_t tid;
pid = getpid();
tid = pthread_self();
printf("%s pid %u tid %u (0x%x)\n", s, (unsigned int)pid,
(unsigned int)tid, (unsigned int)tid);
}
void *thr_fn(void *arg)
{
printids("new thread:");
return ((void *)0);
}
int main()
{
int err;
err = pthread_create(&ntid, NULL, thr_fn, NULL);
if(err!=0)
perror("can not create thread");
printids("main thread:");
sleep(1);
exit(0);
}
复制代码
在apue中文版11章290页中上面的例子
书上说:在本例中,主线程吧新线程ID存放在ntid中,但是新建的线程并不能安全使用它,
如果新线程在主线程
调用pthread_create返回之前就运行了
,那么新线程看到的是未经初始化的ntid的内容,这个内容不是正确的ID.
其中我有一点疑问:新线程没创建怎么就会被调用拉?
作者:
lwrsmy
时间:
2011-02-23 19:08
线程创建好了 已经执行
但是pthread_create还没有返回
pthread_create()
{
clone();//创建线程
.....
return;
}
作者:
butterinsect
时间:
2011-02-23 21:49
,懂了
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2