gpt4 book ai didi

c - Pthread同步查询

转载 作者:太空宇宙 更新时间:2023-11-04 04:47:57 26 4
gpt4 key购买 nike

我是 pthreads 的新手,我编写了一个示例程序来利用 pthread 中的条件事件..

Thread2 只是不退出...我猜从代码中可以很清楚地看出意图。

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

pthread_mutex_t mut;
pthread_cond_t con;

#define LOCK &mut
#define COND &con

#define HAPPY 1
#define SAD 0

int count = 0, response = SAD;

void func_t1();
void func_t2();

int main()
{
pthread_t tid1, tid2;

pthread_create(&tid1, NULL, &func_t1, NULL );
pthread_create(&tid2, NULL, &func_t2, NULL );

pthread_join(tid1, NULL );
pthread_join(tid2, NULL );

return 0;
}

void func_t1()
{
for (;;)
{
pthread_mutex_lock(LOCK);

pthread_cond_wait(COND, LOCK);
if (count == 10)
{
printf("Thread1: You did your job finally, get lost now");
response = HAPPY;
pthread_mutex_unlock(LOCK);
break;
}
else
{
printf("Thread1: You are not capable of making me happy");

pthread_mutex_unlock(LOCK);
}
}
}

void func_t2()
{
for (;;)
{
if (response == SAD)
{
pthread_mutex_lock(LOCK);
count++;
printf("thread2: count incremented to %d...pls check if you are happy \n",
count);
pthread_cond_signal(COND);
pthread_mutex_unlock(LOCK);
}
else
{
printf("thread2:Ha..I finally made her happy \n");
break;
}
}
}

输出:

thread2: count incremented to 106927...pls check if you are happy 
thread2: count incremented to 106928...pls check if you are happy
thread2: count incremented to 106929...pls check if you are happy
thread2: count incremented to 106930...pls check if you are happy
thread2: count incremented to 106931...pls check if you are happy
thread2: count incremented to 106932...pls check if you are happy
thread2: count incremented to 106933...pls check if you are happy
thread2: count incremented to 106934...pls check if you are happy
thread2: count incremented to 106935...pls check if you are happy
thread2: count incremented to 106936...pls check if you are happy
thread2: count incremented to 106937...pls check if you are happy
thread2: count incremented to 106938...pls check if you are happy
thread2: count incremented to 106939...pls check if you are happy
thread2: count incremented to 106940...pls check if you are happy
thread2: count incremented to 106941...pls check if you are happy
thread2: count incremented to 106942...pls check if you are happy
thread2: count incremented to 106943...pls check if you are happy
Thread1: You are not capable of making me happythread2: count incremented to 106944...pls check if you are happy
thread2: count incremented to 106945...pls check if you are happy
thread2: count incremented to 106946...pls check if you are happy
thread2: count incremented to 106947...pls check if you are happy
thread2: count incremented to 106948...pls check if you are happy
thread2: count incremented to 106949...pls check if you are happy
thread2: count incremented to 106950...pls check if you are happy
thread2: count incremented to 106951...pls check if you are happy
thread2: count incremented to 106952...pls check if you are happy
thread2: count incremented to 106953...pls check if you are happy
thread2: count incremented to 106954...pls check if you are happy
thread2: count incremented to 106955...pls check if you are happy
thread2: count incremented to 106956...pls check if you are happy
thread2: count incremented to 106957...pls check if you are happy
thread2: count incremented to 106958...pls check if you are happy
thread2: count incremented to 106959...pls check if you are happy

它永远不会结束......

如果您能指出上述代码中的缺陷,将会很有帮助。

最佳答案

所以你基本上有一个包含两个线程的程序:

第一个线程正在等待计数达到 10。它被线程 2 发出的信号唤醒。

线程 2 递增计数变量并发送信号,然后重复。

由于线程 2 不等待线程 1 的回复,因此无法保证线程 1 会看到所有要计数的更改。

相反,它可能看起来像:

Thread 2 increments count & signals
Thread 2 increments count & signals
Thread 2 increments count & signals
Thread 2 increments count & signals
Thread 1 sees signal - not 10
Thread 2 increments count & signals
Thread 2 increments count & signals
Thread 2 increments count & signals
Thread 1 sees signal - not 10
...
...

您需要添加一种机制,仅当线程 1 检查了计数的当前值并且处于 SAD 状态时才允许线程 2 递增计数。

关于c - Pthread同步查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19067254/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com