Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.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_Linux_Ubuntu_Mutex - Fatal编程技术网

如何修改线程以打印;再次向世界问好?在C中

如何修改线程以打印;再次向世界问好?在C中,c,linux,ubuntu,mutex,C,Linux,Ubuntu,Mutex,如何强制三个线程使用lock_mutex或sleep函数打印“hello world Reach”?我受够了 /* t2.c synchronize threads through mutex and conditional variable To compile use: gcc -o t2 t2.c -lpthread */ #include <stdio.h> #include <pthread.h> void hello

如何强制三个线程使用lock_mutex或sleep函数打印“hello world Reach”?我受够了

/* t2.c
       synchronize threads through mutex and conditional variable 
       To compile use: gcc -o t2 t2.c -lpthread 
    */

#include <stdio.h>
#include <pthread.h>

void hello(); // define three routines called by threads
void world();
void again(); /*new statment*/

/* global variable shared by threads */
pthread_mutex_t mutex; // mutex
pthread_cond_t done_hello; // conditional variable
int done = 0; // testing variable

int main(int argc, char* argv[])
{
    pthread_t tid_hello, // thread id
        tid_world, tid_again;

    /*  initialization on mutex and cond variable  */
    pthread_mutex_init(&mutex, NULL);

    pthread_cond_init(&done_hello, NULL);

    pthread_create(&tid_hello, NULL, (void*)&hello, NULL); //thread creation
    pthread_create(&tid_world, NULL, (void*)&world, NULL); //thread creation
    pthread_create(&tid_again, NULL, (void*)&again, NULL); //thread creation/*new statment*/
    /* main waits for the three threads to finish by order */
    pthread_join(tid_hello, NULL);

    pthread_join(tid_world, NULL);

    pthread_join(tid_again, NULL); /*new statment*/

    printf("\n");

    return 0;
}

void hello()
{
    pthread_mutex_lock(&mutex);

    printf(" hello");

    fflush(stdout); // flush buffer to allow instant print out
    done = 2;

    pthread_cond_signal(&done_hello); // signal world() thread
    pthread_mutex_unlock(&mutex); // unlocks mutex to allow world to print
    return;
}

void world()
{
    pthread_mutex_lock(&mutex);

    /* world thread waits until done == 1. */
    while (done == 1)
        pthread_cond_wait(&done_hello, &mutex);

    printf(" world");
    fflush(stdout);
    pthread_mutex_unlock(&mutex); // unlocks mutex
    return;
}

void again() /*new function*/
{
    pthread_mutex_lock(&mutex);

    /* again thread waits until done == 0. */
    while (done == 0)
        pthread_cond_wait(&done_hello, &mutex);

    printf(" again");
    fflush(stdout);
    pthread_mutex_unlock(&mutex); // unlocks mutex
    return;
}
/*t2.c
通过互斥和条件变量同步线程
要编译,请使用:gcc-ot2.c-lpthread
*/
#包括
#包括
void hello();//定义线程调用的三个例程
空虚的世界();
再次无效()/*新站*/
/*线程共享的全局变量*/
pthread_mutex_t mutex;//互斥
pthread_cond_t done_hello;//条件变量
int done=0;//测试变量
int main(int argc,char*argv[])
{
pthread\u t tid\u hello,//线程id
tid_世界,tid_再次;
/*互斥和cond变量上的初始化*/
pthread_mutex_init(&mutex,NULL);
pthread_cond_init(&done_hello,NULL);
pthread_create(&tid_hello,NULL,(void*)&hello,NULL);//线程创建
pthread_create(&tid_world,NULL,(void*)&world,NULL);//线程创建
pthread_create(&tid_再次,NULL,(void*)&再次,NULL);//线程创建/*新状态*/
/*main等待三个线程按顺序完成*/
pthread_join(tid_hello,NULL);
pthread_join(tid_world,NULL);
pthread_join(再次为tid_,NULL);/*新语句*/
printf(“\n”);
返回0;
}
void hello()
{
pthread_mutex_lock(&mutex);
printf(“你好”);
fflush(stdout);//刷新缓冲区以允许即时打印输出
完成=2;
pthread_cond_signal(&done_hello);//signal world()线程
pthread_mutex_unlock(&mutex);//解锁mutex以允许世界打印
返回;
}
虚空世界()
{
pthread_mutex_lock(&mutex);
/*世界线程等待完成==1*/
while(完成==1)
pthread_cond_wait(&done_hello,&mutex);
printf(“世界”);
fflush(stdout);
pthread_mutex_unlock(&mutex);//解锁mutex
返回;
}
再次作废()/*新函数*/
{
pthread_mutex_lock(&mutex);
/*线程再次等待,直到完成==0*/
while(完成==0)
pthread_cond_wait(&done_hello,&mutex);
printf(“再次”);
fflush(stdout);
pthread_mutex_unlock(&mutex);//解锁mutex
返回;
}

是的,你可以。但是您应该使用几个
pthread\u cond\t
信号:

  • 当打印了
    “hello”
    时为一个
  • 打印了
    “世界”
    时的一个
实际上,来自:

对同一条件变量的并发pthread_cond_timedwait()或pthread_cond_wait()操作使用多个互斥锁的效果未定义;也就是说,当线程等待条件变量时,条件变量将绑定到唯一的互斥体,而此(动态)绑定将在等待返回时结束

每个
world
功能都必须等待自己的信号

#include <stdio.h>
#include <pthread.h>

void * hello(void*);                   
void * world(void*);
void * again(void*);                   

/* global variable shared by threads */
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;          
pthread_cond_t done_hello = PTHREAD_COND_INITIALIZER;      
pthread_cond_t done_world = PTHREAD_COND_INITIALIZER;      

int main(void)
{
    pthread_t threads[3];

    pthread_create(&threads[0], NULL, hello, NULL);    
    pthread_create(&threads[1], NULL, world, NULL);    
    pthread_create(&threads[2], NULL, again, NULL);    

    for(int i = 0; i < 3; ++i)
        pthread_join(threads[i], NULL);

    printf("\n");
    return 0;
}

void * hello(void* foo)
{
    pthread_mutex_lock(&mutex);
        printf(" hello");
        fflush(stdout);             
        pthread_cond_signal(&done_hello);
    pthread_mutex_unlock(&mutex);       
    return NULL;
}

void * world(void* foo)
{
    pthread_mutex_lock(&mutex);
    pthread_cond_wait(&done_hello, &mutex);
        printf(" world");
        fflush(stdout);
        pthread_cond_signal(&done_world);
    pthread_mutex_unlock(&mutex);       
    return NULL;
}

void * again(void* foo)
{                               
    pthread_mutex_lock(&mutex);
    pthread_cond_wait(&done_world, &mutex);
        printf(" again");
        fflush(stdout);
    pthread_mutex_unlock(&mutex);
    return NULL;
}

#包括
#包括
void*你好(void*);
void*世界(void*);
再次作废*(作废*);
/*线程共享的全局变量*/
pthread\u mutex\u t mutex=pthread\u mutex\u初始值设定项;
pthread_cond_t done_hello=pthread_cond_初始值设定项;
pthread_cond_t done_world=pthread_cond_初始值设定项;
内部主(空)
{
pthread_t线程[3];
pthread_create(&threads[0],NULL,hello,NULL);
pthread_create(&threads[1],NULL,world,NULL);
pthread_create(线程[2],NULL,再次,NULL);
对于(int i=0;i<3;++i)
pthread_join(线程[i],NULL);
printf(“\n”);
返回0;
}
void*hello(void*foo)
{
pthread_mutex_lock(&mutex);
printf(“你好”);
fflush(stdout);
pthread_cond_信号(&done_hello);
pthread_mutex_unlock(&mutex);
返回NULL;
}
void*world(void*foo)
{
pthread_mutex_lock(&mutex);
pthread_cond_wait(&done_hello,&mutex);
printf(“世界”);
fflush(stdout);
pthread_cond_信号(&done_world);
pthread_mutex_unlock(&mutex);
返回NULL;
}
再次无效(无效*foo)
{                               
pthread_mutex_lock(&mutex);
pthread_cond_wait(&done_world,&mutex);
printf(“再次”);
fflush(stdout);
pthread_mutex_unlock(&mutex);
返回NULL;
}

<代码> > P>使用适当的辅币,考虑如下:

void *thread(void *arg)
{
        static char str[] = { "hello", "world", "again" };
        int value = (int)(intptr_t)arg;
        /* wait for the condition (done == value) to be true */
        pthread_mutex_lock(&mutex);
        while (done != value) {
                pthread_cond_wait(&done_hello, &mutex);
        }
        pthread_mutex_unlock(&mutex);
        /* release all locks to decrease latency during our work */
        printf(" %s", str[value]);
        /* reacquire the lock to update done */
        pthread_mutex_lock(&mutex);
        done++;
        /* notify anybody that might be waiting for done */
        pthread_cond_broadcast(&done_hello);
        /* permit others to continue */
        pthread_mutex_unlock(&mutex);
        return NULL;
}

它与您的有点不同,但是让所有线程运行相同的代码可以建立代码正确性的信心,并减少在错误时调试代码的工作量。注意如何使用条件:
lock();while(!condition)wait();解锁()
锁();setcondition();广播();解锁()广播()的顺序;解锁()不重要,只要你在他们之间什么都不做。选择一个惯例,并坚持它;当你需要破坏它时,它会作为发生了什么事情的线索。

不要将函数指针投射到
void*
,只要使用正确的原型
void*hello(void*)
当我使用你的原型时,这个错误会发生t2。c:31:37:错误:预期表达式pthread\u create(&tid\u hello,NULL,void*hello(void*))
pthread\u create
调用应该如下所示:
pthread\u create(&tid\u hello,NULL,hello,NULL)
和函数
void*hello(void*){pthread\u mutex\u lock(&mutex)…}
。吹毛求疵:“但是您必须使用几个pthread\u cond\t信号”并不完全正确。可以用几种方法解决这个问题(包括使用单个pthread条件)@4386427:当我阅读
pthread_cond_wait
手册页时,我不确定。两个不同的线程可以毫无问题地等待相同的
pthread_cond_t
?虽然有一些时间分割,但是,从您那里得到的回答和解释很好,两个线程可以在同一个页面上等待,但是在这种情况下
pthread_cond_wait
可能是neededThat不是如何使用condvars。原来的更接近。一个condvar没有内存,因此如果一个线程在没有其他线程等待时发出信号,没有人会知道。