|
|
发表于 2006-4-13 12:40:02
|
显示全部楼层
Post by chairman
我明白你的意思。sleep可以睡眠单个线程而不睡眠整个进程。因为Linux的内核调度的单位是线程不是进程
今天又试了一下,确实可以,看来上次大意了,抱歉,不过man里的确是说的是睡眠一个进程。
上次试的时候是在一个大程序里边,估计是其他的地方影响了结果,今天弄了个小的来试sleep,确实可以睡眠单个线程,再次抱歉
- #include <pthread.h>
- #include <stdio.h>
- #include <unistd.h>
- void *
- my_thread()
- {
- printf("Start another thread!\n");
- sleep(60);
-
- printf("Sleeped thread wake up!\n");
- return;
- }
- int main()
- {
- int thread_id;
- pthread_t p_thread;
-
- thread_id = pthread_create(&p_thread,NULL,my_thread,NULL);
- printf("This is the master thread!\n");
- sleep(10);
- printf("The master thread wake up!\n");
-
- pthread_join(p_thread,NULL);
- return 0;
- }
复制代码 |
|