- 论坛徽章:
- 0
|
原帖由 zydzmapx 于 2008-8-22 09:46 发表
为了验证大家的方法,我作料一个小的测试程序如下(由于是从别的例子程序改造过来的,存在部分程序啰嗦,请别追究,谢谢,请重点关注线程释放部分的 程序)
# include
# include
# include
# include
# ...
楼主, 你看看下面的代码吧, 希望对你有用:
- #include<stdio.h>
- #include<pthread.h>
- #include<stdlib.h>
- #include<unistd.h>
- #include<sys/types.h>
- void* thr_fn(void* args)
- {
- while (1)
- {
- sleep(1);
- printf("my pid is %d\n", getpid());
- }
- }
- int main()
- {
- pthread_t tid;
- pid_t pid;
- printf("pid=%d\n", getpid());
- pthread_create(&tid, NULL, thr_fn, NULL);
- sleep(5);
- pthread_cancel(tid);
- while (1)
- {
- sleep(2);
- printf("in parent:tid=%u\n", tid);
- }
- return 0;
- }
复制代码 |
|