Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 睡眠函数是睡眠所有线程还是只睡眠调用它的线程?_C_Multithreading_Pthreads_Sleep_Thread Sleep - Fatal编程技术网

C 睡眠函数是睡眠所有线程还是只睡眠调用它的线程?

C 睡眠函数是睡眠所有线程还是只睡眠调用它的线程?,c,multithreading,pthreads,sleep,thread-sleep,C,Multithreading,Pthreads,Sleep,Thread Sleep,我在linux(Centos)上用pthread编程?我想小睡一会儿,等点什么。我正在尝试使用sleep()、nanosleep()或usleep(),或者一些可以做到这一点的东西。我想问:睡眠函数是睡眠所有线程还是只睡眠调用它的线程?如有任何建议或参考,将不胜感激 void *start_routine () { /* I just call sleep functions here */ sleep (1); /* sleep all threads or just the

我在linux(Centos)上用pthread编程?我想小睡一会儿,等点什么。我正在尝试使用sleep()、nanosleep()或usleep(),或者一些可以做到这一点的东西。我想问:睡眠函数是睡眠所有线程还是只睡眠调用它的线程?如有任何建议或参考,将不胜感激

void *start_routine () {
    /* I just call sleep functions here */
    sleep (1); /* sleep all threads or just the one who call it? 
                  what about nanosleep(), usleep(), actually I 
                  want the threads who call sleep function can 
                  sleep with micro-seconds or mili-seconds.  
               */
    ...
}

int main (int argc, char **argv) {
    /* I just create threads here */
    pthread_create (... ...);
    ...
    return 0;
}

我的测试程序:

#define _GNU_SOURCE
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <sched.h>
#include <unistd.h>

void *start_routine (void *j) {

    unsigned long sum;
    int i;
    int jj;
    jj = (int)j;
    do {
        sum = 1;
        for (i=0; i<10000000; i++) {
            sum = sum * (sum+i);
        }
        if (jj == 0) {
            printf ("\033[22;33m[jj%d.%ld]\t", jj, sum);
            sleep(1);           
        }
        else {
            printf ("\033[22;34m[jj%d.%ld]\t", jj, sum);
        }

    }while (1);

    pthread_exit((void *)0);
}
int main(int argc, char *argv[])
{
    cpu_set_t cpuset;
    pthread_t thread[2];
    int i;
    i = 0;
    CPU_ZERO(&cpuset);
    CPU_SET(i, &cpuset);

    pthread_create (&thread[0], NULL, start_routine, (void *)i);
    pthread_setaffinity_np(thread[0], sizeof(cpu_set_t), &cpuset);
    i = 1;
    CPU_ZERO(&cpuset);
    CPU_SET(i, &cpuset);
    pthread_create (&thread[1], NULL, start_routine, (void *)i);
    pthread_setaffinity_np(thread[1], sizeof(cpu_set_t), &cpuset);
    pthread_exit (NULL);
}
定义GNU源
#包括
#包括
#包括
#包括
#包括
void*start_例程(void*j){
无符号长和;
int i;
int jj;
jj=(int)j;
做{
总和=1;

对于(i=0;i仅调用函数的线程。

拼写为:

sleep()函数将导致调用线程被调用 暂停执行直到

其中一点同样清楚:

sleep()


然而,也有一些错误的引用,但它们却保持着相反的状态。linux.die.net用于声明
sleep
会导致进程等待。

@Kiril,来检查一下他的问题历史记录。这只是回答的一行。我的意思是“工作”,而不是“警告”@bestsss-要检查什么?我没有说过这个问题,我甚至投了赞成票。你为什么要调用sleep?你有没有测试过它?在我的测试程序中,它似乎使主线程休眠。是的,我已经测试过。我还阅读了函数的文档。@jalf是100%正确的。如果sleep()call使主线程睡眠,然后主线程直接调用它,或者在睡眠后等待仅由睡眠线程提供的其他信号。你能在这里发布测试程序吗?我在上面发布了我的测试程序,但我对结果感到困惑。我发现m Red Hat Enterprise Linux Workstation 6.6版系统有一个睡眠手册页(3),内容为“sleep()使调用进程睡眠到…”,因此可能上游手册页已更新。