Pthreads 和信号 C++
Pthreads and signals C++
我是线程的新手,似乎遇到了计时问题。当我 运行 我的代码输出是...
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;
}
如果没有 谓词,条件变量几乎毫无用处,条件状态指示什么已经改变。条件变量只不过是一种信号机制,用于宣布 "something" 可能已更改,但您仍然需要管理 "what"。那个互斥量是保护 "what".
的东西
例如:
#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) 是启动进程的那个,然后静默结束。
请注意,谓词数据(状态)在任何时候都不会更改 或检查 而不受互斥锁的保护,无论线程在做什么 check/changing .
具有互斥锁存器状态的三个时间线以及拥有它的线程将有助于理解其工作原理,我强烈建议您在这方面提供铅笔和纸来提供帮助。
祝你好运。
我是线程的新手,似乎遇到了计时问题。当我 运行 我的代码输出是...
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;
}
如果没有 谓词,条件变量几乎毫无用处,条件状态指示什么已经改变。条件变量只不过是一种信号机制,用于宣布 "something" 可能已更改,但您仍然需要管理 "what"。那个互斥量是保护 "what".
的东西例如:
#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) 是启动进程的那个,然后静默结束。
请注意,谓词数据(状态)在任何时候都不会更改 或检查 而不受互斥锁的保护,无论线程在做什么 check/changing .
具有互斥锁存器状态的三个时间线以及拥有它的线程将有助于理解其工作原理,我强烈建议您在这方面提供铅笔和纸来提供帮助。
祝你好运。