C语言三个线程通信的问题(发生死锁)
Problem in communication of three threads in C language (deadlock occurs)
我正在创建一个程序,其中有 3 个链表,我正在尝试更新或删除这三个线程中这些链表的节点。但是死锁正在发生
插入和删除工作正常。这里的三个变量var1InUse,var2InUse,var3InUse分别表示3个链表是否被使用(不是所有线程都使用这三个)。正如您在代码中看到的那样,我根据 var1InUse、var2InUse 和 var3InUse 将线程置于等待状态。有时这工作正常但有时会发生死锁。我已经在 Internet 上搜索了解决方案,但可以找到。
我是否正确使用了等待和信号方法?
pthread variables declaration
pthread_mutex_t myMutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t t1cond = PTHREAD_COND_INITIALIZER;
pthread_cond_t t2cond = PTHREAD_COND_INITIALIZER;
pthread_cond_t t3cond = PTHREAD_COND_INITIALIZER;
int var1InUse=0,var2InUse=0,var3InUse=0;
线程 1
void* thread1(void* args){
while(var1InUse || var2InUse ) {
pthread_cond_wait(&t1cond,&myMutex);}
var1InUse=1,var2InUse=1;
while(1){
pthread_mutex_lock(&myMutex);
/*
some other code about adding and removing from
linkedlist
*/
var1InUse=0,var2InUse=0;
pthread_cond_signal(&t2cond);
pthread_cond_signal(&t3cond);
pthread_mutex_unlock(&myMutex);}
}
线程 2
void* thread2(void* args){
while(var1InUse || var2InUse || var3InUse) {
pthread_cond_wait(&t2cond,&myMutex);}
var1InUse=1,var2InUse=1,var3InUse=1;
while(1){
pthread_mutex_lock(&myMutex);
/*
some other code adding and removing from linkedlist
*/
var1InUse=0,var2InUse=0,var3InUse=0;
pthread_cond_signal(&t1cond);
pthread_cond_signal(&t3cond);
pthread_mutex_unlock(&myMutex);}
}
线程 3
void* thread3(void* args){
while(var1InUse || var3InUse ) {
pthread_cond_wait(&t3cond,&myMutex);}
var1InUse=1,var3InUse=1;
while(1){
pthread_mutex_lock(&myMutex);
/*
some other code adding and removing from linkedlist
*/
var1InUse=0,var3InUse=0;
pthread_cond_signal(&t1cond);
pthread_cond_signal(&t2cond);
pthread_mutex_unlock(&myMutex);}
}
主要方法
int main(){
pthread_t t1,t2,t3,t4;
pthread_mutex_init(&myMutex,0);
pthread_create(&t1,NULL,thread1,NULL);
pthread_create(&t2,NULL,thread2,NULL);
pthread_create(&t3,NULL,thread3,NULL);
pthread_join(t1,NULL);
pthread_join(t2,NULL);
pthread_join(t3,NULL);
pthread_mutex_destroy(&myMutex);
return 0
}
我希望解除死锁。
pthread_cond_wait()
使用的互斥量需要在函数调用前锁定。这是手册页的摘录:
The pthread_cond_timedwait()
and pthread_cond_wait()
functions shall block on a condition variable. The application shall ensure that these functions are called with mutex locked by the calling thread; otherwise, an error (for PTHREAD_MUTEX_ERRORCHECK and robust mutexes) or undefined behavior (for other mutexes) results.
虽然pthread_cond_wait()
在内部解锁互斥锁,但在函数returns成功之前又被锁定了:
Upon successful return, the mutex shall have been locked and shall be owned by the calling thread.
此外,您应该在锁定互斥锁的情况下访问共享变量 var1InUse
、var2InUse
和 var3InUse
。
这是您的 thread1()
的修改版本,它遵循这些规则。对其他线程启动例程的修改应该类似:
void* thread1(void* args){
pthread_mutex_lock(&myMutex);
while(var1InUse || var2InUse ) {
pthread_cond_wait(&t1cond,&myMutex);
}
var1InUse=1,var2InUse=1;
pthread_mutex_unlock(&myMutex);
while(1){
pthread_mutex_lock(&myMutex);
/*
some other code about adding and removing from linkedlist
*/
var1InUse=0,var2InUse=0;
pthread_cond_signal(&t2cond);
pthread_cond_signal(&t3cond);
pthread_mutex_unlock(&myMutex);
}
return NULL;
}
(我不完全确定上面的内容是否正确,因为从原始代码中还不完全清楚 while(1)
循环应该做什么。)
我正在创建一个程序,其中有 3 个链表,我正在尝试更新或删除这三个线程中这些链表的节点。但是死锁正在发生
插入和删除工作正常。这里的三个变量var1InUse,var2InUse,var3InUse分别表示3个链表是否被使用(不是所有线程都使用这三个)。正如您在代码中看到的那样,我根据 var1InUse、var2InUse 和 var3InUse 将线程置于等待状态。有时这工作正常但有时会发生死锁。我已经在 Internet 上搜索了解决方案,但可以找到。 我是否正确使用了等待和信号方法?
pthread variables declaration
pthread_mutex_t myMutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t t1cond = PTHREAD_COND_INITIALIZER;
pthread_cond_t t2cond = PTHREAD_COND_INITIALIZER;
pthread_cond_t t3cond = PTHREAD_COND_INITIALIZER;
int var1InUse=0,var2InUse=0,var3InUse=0;
线程 1
void* thread1(void* args){
while(var1InUse || var2InUse ) {
pthread_cond_wait(&t1cond,&myMutex);}
var1InUse=1,var2InUse=1;
while(1){
pthread_mutex_lock(&myMutex);
/*
some other code about adding and removing from
linkedlist
*/
var1InUse=0,var2InUse=0;
pthread_cond_signal(&t2cond);
pthread_cond_signal(&t3cond);
pthread_mutex_unlock(&myMutex);}
}
线程 2
void* thread2(void* args){
while(var1InUse || var2InUse || var3InUse) {
pthread_cond_wait(&t2cond,&myMutex);}
var1InUse=1,var2InUse=1,var3InUse=1;
while(1){
pthread_mutex_lock(&myMutex);
/*
some other code adding and removing from linkedlist
*/
var1InUse=0,var2InUse=0,var3InUse=0;
pthread_cond_signal(&t1cond);
pthread_cond_signal(&t3cond);
pthread_mutex_unlock(&myMutex);}
}
线程 3
void* thread3(void* args){
while(var1InUse || var3InUse ) {
pthread_cond_wait(&t3cond,&myMutex);}
var1InUse=1,var3InUse=1;
while(1){
pthread_mutex_lock(&myMutex);
/*
some other code adding and removing from linkedlist
*/
var1InUse=0,var3InUse=0;
pthread_cond_signal(&t1cond);
pthread_cond_signal(&t2cond);
pthread_mutex_unlock(&myMutex);}
}
主要方法
int main(){
pthread_t t1,t2,t3,t4;
pthread_mutex_init(&myMutex,0);
pthread_create(&t1,NULL,thread1,NULL);
pthread_create(&t2,NULL,thread2,NULL);
pthread_create(&t3,NULL,thread3,NULL);
pthread_join(t1,NULL);
pthread_join(t2,NULL);
pthread_join(t3,NULL);
pthread_mutex_destroy(&myMutex);
return 0
}
我希望解除死锁。
pthread_cond_wait()
使用的互斥量需要在函数调用前锁定。这是手册页的摘录:
The
pthread_cond_timedwait()
andpthread_cond_wait()
functions shall block on a condition variable. The application shall ensure that these functions are called with mutex locked by the calling thread; otherwise, an error (for PTHREAD_MUTEX_ERRORCHECK and robust mutexes) or undefined behavior (for other mutexes) results.
虽然pthread_cond_wait()
在内部解锁互斥锁,但在函数returns成功之前又被锁定了:
Upon successful return, the mutex shall have been locked and shall be owned by the calling thread.
此外,您应该在锁定互斥锁的情况下访问共享变量 var1InUse
、var2InUse
和 var3InUse
。
这是您的 thread1()
的修改版本,它遵循这些规则。对其他线程启动例程的修改应该类似:
void* thread1(void* args){
pthread_mutex_lock(&myMutex);
while(var1InUse || var2InUse ) {
pthread_cond_wait(&t1cond,&myMutex);
}
var1InUse=1,var2InUse=1;
pthread_mutex_unlock(&myMutex);
while(1){
pthread_mutex_lock(&myMutex);
/*
some other code about adding and removing from linkedlist
*/
var1InUse=0,var2InUse=0;
pthread_cond_signal(&t2cond);
pthread_cond_signal(&t3cond);
pthread_mutex_unlock(&myMutex);
}
return NULL;
}
(我不完全确定上面的内容是否正确,因为从原始代码中还不完全清楚 while(1)
循环应该做什么。)