|
|
发表于 2006-3-31 10:25:02
|
显示全部楼层
看看你的程序,这个线程根本就没跑,因为main已经返回了,程序就退出了。
在main里面加上一行 pthread_join,等那个线程退出。
看我的测试:
- #include <stdio.h>
- #include <pthread.h>
- void* thread_entry(void* arg)
- {
- printf("thread_entry: begins, arg=%p\n", arg);
- getchar();
- printf("thread_entry: exits\n");
- }
- int main(int argc, char** argv)
- {
- pthread_t thread_id;
- if (pthread_create(&thread_id, NULL, thread_entry, NULL) < 0)
- exit(1);
- pthread_join(thread_id, NULL);
- return 0;
- }
复制代码
保存为test.c
- $ gcc test.c -lpthread -o test
复制代码
在另一个终端:
- $ ps ax -L |grep test
- 8519 8519 pts/5 Sl+ 0:00 ./test
- 8519 8520 pts/5 Sl+ 0:00 ./test
- 8522 8522 pts/2 R+ 0:00 grep test
复制代码
看,有两个线程在里面呢。 |
|