做睡睡眠功能的所有线程或只是一个谁打电话吗? [英] Do sleep functions sleep all threads or just the one who call it?

查看:136
本文介绍了做睡睡眠功能的所有线程或只是一个谁打电话吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与pthread的在Linux(gzip)编程?我想线程睡眠很短的时间等待着什么。我想使用睡眠()了nanosleep()或usleep()函式或者也可以是可以做到这一点。我想问的是:做睡睡眠功能的所有线程或只是一个谁打电话吗?任何建议或引用是AP preciate。

 无效*的start_routine(){
    / *我只是叫睡眠功能,在这里* /
    睡眠(1); / *睡眠所有线程或只是谁把它称为一个?
                  怎么样了nanosleep(),usleep()函式,其实我
                  想谁打电话睡眠功能可以在螺纹
                  微秒或毫秒入睡。
               * /
    ...
}INT主(INT ARGC,字符** argv的){
    / *我刚刚创建的线程在这里* /
    在pthread_create(......);
    ...
    返回0;
}


我的测试程序:

 的#define _GNU_SOURCE
#包括LT&;&pthreads.h中GT;
#包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&sched.h中GT;
#包括LT&;&unistd.h中GT;无效*的start_routine(无效* j)条{    无符号长总和;
    INT I;
    INT JJ;
    JJ =(INT)焦耳;
    做{
        总和= 1;
        对于(i = 0; I<千万,我++){
            总和=总和*(总和+ I);
        }
        如果(JJ == 0){
            的printf(\\ 033 [22;33米[JJ%D%LD] \\ t的,JJ,总和);
            睡眠(1);
        }
        其他{
            的printf(\\ 033 [22;34米[JJ%D%LD] \\ t的,JJ,总和);
        }    }而(1);    了pthread_exit((无效*)0);
}
INT主(INT ARGC,CHAR *的argv [])
{
    cpu_set_t cpuset;
    的pthread_t螺纹[2];
    INT I;
    I = 0;
    CPU_ZERO(安培; cpuset);
    CPU_SET(I,&放大器; cpuset);    在pthread_create(安培;螺纹[0],NULL,的start_routine,(无效*)ⅰ);
    pthread_setaffinity_np(线程[0],sizeof的(cpu_set_t),放大器; cpuset);
    I = 1;
    CPU_ZERO(安培; cpuset);
    CPU_SET(I,&放大器; cpuset);
    在pthread_create(安培;螺纹[1],NULL的start_routine,(无效*)ⅰ);
    pthread_setaffinity_np(线程[1],sizeof的(cpu_set_t),放大器; cpuset);
    了pthread_exit(NULL);
}


解决方案

标准法术它:


  

睡眠()函数须安排的调用线程
  从执行暂停,直到....


借助 Linux的一个是同样明显:


  

睡眠()使调用线程休眠状态,直到...


但以下保持否则几个错误的引用。 linux.die.net使用状态睡眠起因过程的等待。

I am programming with pthread on linux(Centos)? I wanna to threads sleep a short time to wait for something. I am trying to use sleep(), nanosleep(), or usleep() or maybe something can do that. I want to ask that: Do sleep functions sleep all threads or just the one who call it? Any advices or references would be appreciate.

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;
}


My test program:

#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);
}

解决方案

The standard spells it:

The sleep() function shall cause the calling thread to be suspended from execution until ....

The linux one is just as clear:

sleep() makes the calling thread sleep until...

There are however a few erroneous references which maintain otherwise. linux.die.net used to state sleep causes the process to wait.

这篇关于做睡睡眠功能的所有线程或只是一个谁打电话吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆