gpt4 book ai didi

c++ - Pthreads 和信号 C++

转载 作者:行者123 更新时间:2023-11-28 02:32:38 26 4
gpt4 key购买 nike

我是线程的新手,似乎遇到了计时问题。当我运行我的代码时,我的输出是...

Data received!
Data processed!

然后就挂了。我假设我使用的等待和信号不正确。将不胜感激。

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

char buffer [100];
pthread_mutex_t buffer_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t buffer_cond = PTHREAD_COND_INITIALIZER;

void* threadFunc(void *proc){
if((int)proc == 0){
pthread_mutex_lock(&buffer_mutex);
sprintf(buffer, "Data received!\n");
printf("%s", buffer);
pthread_cond_signal(&buffer_cond);
pthread_mutex_unlock(&buffer_mutex);
}
else if((int)proc == 1){
pthread_mutex_lock(&buffer_mutex);
pthread_cond_wait(&buffer_cond, &buffer_mutex);
sprintf(buffer, "Data processed!\n");
printf("%s", buffer);
pthread_cond_signal(&buffer_cond);
pthread_mutex_unlock(&buffer_mutex);
}
else{
sleep(1);
pthread_mutex_lock(&buffer_mutex);
pthread_cond_wait(&buffer_cond, &buffer_mutex);
sprintf(buffer, "Data sent!\n");
printf("%s", buffer);
pthread_mutex_unlock(&buffer_mutex);
}

return(NULL);
}

int main()
{
pthread_t threads[3];

for(int i = 0; i < 3; i++){
pthread_create(&threads[i], NULL, threadFunc, (void *) i);
}

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

return 0;
}

最佳答案

如果没有谓词,条件变量几乎毫无用处,谓词 是一种条件状态,指示什么 发生了变化。条件变量只不过是一种信号机制,用于宣布“某事”可能已经改变,但您仍然需要管理“什么”。那个互斥量就是保护那个“什么”的东西。

例如:

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

char buffer [100];
int state = 0;

pthread_mutex_t buffer_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t buffer_cond = PTHREAD_COND_INITIALIZER;

void* threadFunc(void *proc)
{
intptr_t iptr = (intptr_t)proc;
pthread_mutex_lock(&buffer_mutex);

switch(iptr)
{
case 0:
{
while (state != 1) // 1 means data available
pthread_cond_wait(&buffer_cond, &buffer_mutex);

// mutex is locked. access predicate data
printf("Data received: %s\n", buffer);
state = 2; // advance state
pthread_mutex_unlock(&buffer_mutex);
pthread_cond_broadcast(&buffer_cond);
}
break;

case 1:
{
while (state != 2)
pthread_cond_wait(&buffer_cond, &buffer_mutex);

// mutex is locked. access predicate data
printf("Data processed: %s\n", buffer);
pthread_mutex_unlock(&buffer_mutex);
}
break;

default:
{
// setup initial send-state
printf("Data sent: %s\n", buffer);
state = 1;
pthread_mutex_unlock(&buffer_mutex);
pthread_cond_broadcast(&buffer_cond);
}
}
return NULL;
}

int main()
{
pthread_t threads[3];
intptr_t i = 0;

strcpy(buffer, "Some message");

for(i = 0; i < 3; i++){
pthread_create(&threads[i], NULL, threadFunc, (void *) i);
}

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

return 0;
}

输出

Data sent: Some message
Data received: Some message
Data processed: Some message

这只是启动了三个线程。一个 (id=0) 将等待状态变为 1,然后继续其宣布发生的事情、推进状态并发出条件信号的工作。第二个 (id=1) 等待那个状态,以类似的方式完成它的工作,然后简单地退出,因为没有其他事情要做。第三个 (id=2) 是启动进程的那个,然后静默结束。

请注意,谓词数据(状态)在任何时候都不会更改或检查,而不受互斥锁的保护,不管线程在做什么检查/更改。

具有互斥锁存器状态的三个时间线以及拥有它的线程将有助于理解其工作原理,我强烈建议您在这方面提供铅笔和纸来提供帮助。

祝你好运。

关于c++ - Pthreads 和信号 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28523218/

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