- 论坛徽章:
- 0
|
- #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.
其中我有一点疑问:新线程没创建怎么就会被调用拉? |
|