C++ pthread_cond_wait/signal和mutex未按预期工作
我正在尝试学习pthread/mutex,但尽管在web上进行了大量的研究/阅读,我还是无法理解这段代码到底出了什么问题:C++ pthread_cond_wait/signal和mutex未按预期工作,c++,linux,multithreading,pthreads,mutex,C++,Linux,Multithreading,Pthreads,Mutex,我正在尝试学习pthread/mutex,但尽管在web上进行了大量的研究/阅读,我还是无法理解这段代码到底出了什么问题: #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <unistd.h> struct data { int Counter = 0; int calls = -1; int iteration = -1;
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
struct data
{
int Counter = 0;
int calls = -1;
int iteration = -1;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t condition = PTHREAD_COND_INITIALIZER;
};
void* threadAlarm (void* arg);
void* threadCounter (void* arg);
int main (void)
{
pthread_t monThreadCounter;
pthread_t monThreadAlarm;
struct data mydata;
if (pthread_create (&monThreadAlarm, NULL, threadAlarm,(void*)&mydata)>0)
printf("Pthread Alarme error\n");
if (pthread_create (&monThreadCounter, NULL, threadCounter, (void*)&mydata)>0)
printf("Pthread Counter error\n");
pthread_join (monThreadCounter, NULL);
pthread_join (monThreadAlarm, NULL);
return 0;
}
void* threadCounter (void *arg)
{
struct data *myarg = (struct data *)arg;
srand(time(NULL));
pthread_mutex_lock (&myarg->mutex);
while(1)
{
myarg->Counter += rand()%10; /* We add a random number to the counter */
if(myarg->Counter > 20) /* If Counter is greater than 20, we should trigger the alarm*/
{
myarg->iteration += 1; /* Iteration counter, to check any shift between expected triggers and reality */
printf("Counter = %i(%i)-->",myarg->Counter,myarg->iteration);
pthread_mutex_unlock (&myarg->mutex); /* Unlock mutex before sending signal */
if (pthread_cond_signal (&myarg->condition) >0)
{
printf("COND SIGNAL ERROR\n");
pthread_exit(NULL);
}
usleep(10000); /* The shorter the sleep is, the weirder the output is */
pthread_mutex_lock (&myarg->mutex); /* We should get the lock again before testing/modifying any shared variable */
}
}
}
void* threadAlarm (void* arg)
{
struct data *myarg = (struct data *)arg;
while(1)
{
pthread_mutex_lock(&myarg->mutex);
//while(myarg->Counter<21) // Uneeded? Since we'll never get the lock before the Counter thread detects condition and release it
{
printf("\nWAITING for trigger...\n",myarg->Counter);
if (pthread_cond_wait (&myarg->condition, &myarg->mutex)>0)
{
printf("ERROR COND WAIT\n");
pthread_exit(NULL);
}
}
myarg->calls+=1; // Calls counter, should be equal to iteration counter, overwise calls have been missed
printf("ALARM TRIGGERED! Call #%i/Iteration #%i -> COUNTER RESET\n",myarg->calls, myarg->iteration);
// Counter reset
myarg->Counter = 0;
pthread_mutex_unlock(&myarg->mutex);
}
}
调用/迭代计数器是同步的,保证每次达到条件时,都能正确触发“报警”线程
但是,如果我减少睡眠,结果会变得很奇怪。在完全没有睡眠的情况下(被注释掉),我得到例如:
WAITING for trigger...
Counter = 21(57916)-->Counter = 23(57917)-->Counter = 29(57918)-->Counter = 38(57919)-->Counter = 45(57920)-->Counter = 45(57921)-->Counter = 45(57922)-->Counter = 49(57923)-->Counter = 52(57924)-->Counter = 55(57925)-->Counter = 61(57926)-->Counter = 65(57927)-->Counter = 70(57928)-->Counter = 77(57929)-->Counter = 83(57930)-->Counter = 86(57931)-->Counter = 92(57932)-->Counter = 95(57933)-->Counter = 99(57934)-->Counter = 107(57935)-->ALARM TRIGGERED! Call #4665/Iteration #57935 -> COUNTER RESET
WAITING for trigger...
Counter = 24(57936)-->Counter = 28(57937)-->Counter = 31(57938)-->Counter = 31(57939)-->Counter = 36(57940)-->Counter = 41(57941)-->Counter = 45(57942)-->Counter = 47(57943)-->Counter = 54(57944)-->Counter = 54(57945)-->Counter = 56(57946)-->Counter = 62(57947)-->Counter = 64(57948)-->Counter = 66(57949)-->Counter = 66
尽管计数器已达到触发状态,但它似乎不会触发报警线程,并继续增加,并且调用/迭代计数器完全不同步,从而证明错过了许多调用
我如何确保每次发出pthread_cond_信号时,等待的线程都被真正触发,而调用线程将等待直到被触发的线程释放互斥锁
为了以防万一,我目前正在Linux Ubuntu上编写代码
谢谢你的帮助。这是意料之中的行为。一旦您向条件变量发送信号,等待的线程最终将唤醒并争夺互斥体,但不能保证在这之前发送信号的线程将无法重新获取互斥体 如果您想让计数器线程等待报警被消耗,您需要对其进行实际编程。您可以通过另一种方式使用相同的条件变量-在计数器线程中:
if (pthread_cond_signal (&myarg->condition) >0)
{
printf("COND SIGNAL ERROR\n");
pthread_exit(NULL);
}
pthread_mutex_lock (&myarg->mutex); /* We should get the lock again before testing/modifying any shared variable */
/* Wait for alarm to happen */
while (myarg->calls < myarg->iteration)
{
pthread_cond_wait(&myarg->condition, &myarg->mutex);
}
if(pthread\u cond\u信号(&myarg->condition)>0)
{
printf(“COND信号错误\n”);
pthread_exit(NULL);
}
pthread_mutex_lock(&myarg->mutex);/*在测试/修改任何共享变量之前,我们应该再次获得锁*/
/*等待警报发生*/
while(myarg->调用迭代)
{
pthread_cond_wait(&myarg->condition,&myarg->mutex);
}
在报警线程中,在递增myarg->calls
后的某个时间点调用pthread\u cond\u signal(&myarg->condition)
顺便说一下,您确实需要将
while(myarg->CounterCounter
的值设置为大于20。它在报警线程有机会运行之前解锁互斥锁并向条件变量发送信号。然后报警线程运行,获取互斥锁并在pthread\u cond\u wait()中阻塞
-它将永远在这里等待,因为我们现在已经确保计数器线程将在继续之前等待警报被消耗
pthread\u cond\u wait()
pthread\u cond\u wait()
立即返回(由于允许“虚假唤醒”)在计数器线程有机会获取互斥锁之前,报警线程现在将继续,即使计数器仍然为零这是工作版本,以防对其他人有用:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
struct data
{
int Counter = 0;
int calls = -1;
int iteration = -1;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t condition = PTHREAD_COND_INITIALIZER;
};
void* threadAlarm (void* arg);
void* threadCounter (void* arg);
int main (void)
{
pthread_t monThreadCounter;
pthread_t monThreadAlarm;
struct data mydata;
if (pthread_create (&monThreadAlarm, NULL, threadAlarm, (void*)&mydata)>0)
printf("Pthread Alarme error\n");
if (pthread_create (&monThreadCounter, NULL, threadCounter, (void*)&mydata)>0)
printf("Pthread Counter error\n");
pthread_join (monThreadCounter, NULL);
pthread_join (monThreadAlarm, NULL);
return 0;
}
void* threadCounter (void *arg)
{
struct data *myarg = (struct data *)arg;
srand(time(NULL));
if (pthread_mutex_lock(&myarg->mutex) > 0)
{
printf("ERROR Mutex lock1 Counter\n");
pthread_exit(NULL);
}
while(1)
{
myarg->Counter += rand()%10; /* We add a random number to the counter */
if(myarg->Counter > 20) /* If Counter is greater than 20, we should trigger the alarm*/
{
myarg->iteration += 1; /* Iteration counter, to check any shift between expected triggers and reality */
printf("Counter = %i(%i)-->",myarg->Counter,myarg->iteration);
if (pthread_mutex_unlock(&myarg->mutex) > 0) /* Unlock mutex before sending signal */
{
printf("ERROR Mutex Unlock Counter\n");
pthread_exit(NULL);
}
if (pthread_cond_signal (&myarg->condition) >0)
{
printf("COND SIGNAL ERROR\n");
pthread_exit(NULL);
}
if (pthread_mutex_lock(&myarg->mutex) > 0) /* We should get the lock again before testing/modifying any shared variable */
{
printf("ERROR Mutex lock2 Counter\n");
pthread_exit(NULL);
}
/* Wait for alarm to happen */
while (myarg->calls < myarg->iteration)
{
pthread_cond_wait(&myarg->condition, &myarg->mutex);
}
}
}
}
void* threadAlarm (void* arg)
{
struct data *myarg = (struct data *)arg;
while(1)
{
if (pthread_mutex_lock(&myarg->mutex) > 0)
{
printf("ERROR Mutex lock Alarm\n");
pthread_exit(NULL);
}
while(myarg->Counter<21)
{
printf("\nWAITING for trigger...\n");
if (pthread_cond_wait (&myarg->condition, &myarg->mutex)>0)
{
printf("ERROR COND WAIT\n");
pthread_exit(NULL);
}
}
myarg->calls+=1; // Calls counter, should be equal to iteration counter, overwise calls have been missed
printf("ALARM TRIGGERED! Call #%i/Iteration #%i -> COUNTER RESET\n",myarg->calls, myarg->iteration);
// Counter reset
myarg->Counter = 0;
if (pthread_mutex_unlock(&myarg->mutex) > 0)
{
printf("ERROR Mutex Unlock Alarm\n");
pthread_exit(NULL);
}
if (pthread_cond_signal (&myarg->condition) >0) //Signal back to Counter thread
{
printf("COND SIGNAL ERROR\n");
pthread_exit(NULL);
}
}
}
#包括
#包括
#包括
#包括
结构数据
{
int计数器=0;
int调用=-1;
int迭代=-1;
pthread\u mutex\u t mutex=pthread\u mutex\u初始值设定项;
pthread_cond_t condition=pthread_cond_初始值设定项;
};
无效*螺纹报警(无效*参数);
void*螺纹计数器(void*arg);
内部主(空)
{
pthread\u t monthread计数器;
pthread\u t monthread报警;
结构数据mydata;
if(pthread_create(&monThreadAlarm,NULL,threadAlarm,(void*)&mydata)>0)
printf(“Pthread Alarme error\n”);
if(pthread_create(&monThreadCounter,NULL,threadCounter,(void*)&mydata)>0)
printf(“Pthread Counter error\n”);
pthread_join(monThreadCounter,NULL);
pthread_join(monThreadAlarm,NULL);
返回0;
}
void*threadCounter(void*arg)
{
结构数据*myarg=(结构数据*)arg;
srand(时间(空));
如果(pthread\u mutex\u lock(&myarg->mutex)>0)
{
printf(“错误互斥锁1计数器”);
pthread_exit(NULL);
}
而(1)
{
myarg->Counter+=rand()%10;/*我们向计数器添加一个随机数*/
如果(myarg->Counter>20)/*如果计数器大于20,我们应该触发警报*/
{
myarg->iteration+=1;/*迭代计数器,用于检查预期触发器和现实之间的任何转换*/
printf(“计数器=%i(%i)-->”,myarg->Counter,myarg->iteration);
如果(pthread\u mutex\u unlock(&myarg->mutex)>0)/*在发送信号之前解锁mutex*/
{
printf(“错误互斥解锁计数器\n”);
pthread_exit(NULL);
}
if(pthread\u cond\u信号(&myarg->condition)>0)
{
printf(“COND信号错误\n”);
pthread_exit(NULL);
}
如果(pthread\u mutex\u lock(&myarg->mutex)>0)/*我们应该在测试/修改任何共享变量之前再次获得锁*/
{
printf(“错误互斥锁2计数器”);
pthread_exit(NULL);
}
/*等待警报发生*/
while(myarg->调用迭代)
{
pthread_cond_wait(&myarg->condition,&myarg->mutex);
}
}
}
}
void*threadAlarm(void*arg)
{
结构数据*myarg=(结构数据*)arg;
而(1)
{
如果(pthread\u mutex\u lock(&myarg->mutex)>0)
{
printf(“错误互斥锁报警\n”);
pthread_exit(NULL);
}
while(myarg->Countercondition,&myarg->mutex)>0)
{
printf(“错误条件等待\n”);
pthread_exit(NULL);
}
}
myarg->calls+=1;//调用计数器,应等于迭代计数器,已错过过度调用
printf(“报警触发!调用#%i/迭代#%i->计数器重置\n”,myarg->调用,myarg->迭代);
//计数器复位
myarg->计数器=0;
如果(pthread_mutex_unlock(&
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
struct data
{
int Counter = 0;
int calls = -1;
int iteration = -1;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t condition = PTHREAD_COND_INITIALIZER;
};
void* threadAlarm (void* arg);
void* threadCounter (void* arg);
int main (void)
{
pthread_t monThreadCounter;
pthread_t monThreadAlarm;
struct data mydata;
if (pthread_create (&monThreadAlarm, NULL, threadAlarm, (void*)&mydata)>0)
printf("Pthread Alarme error\n");
if (pthread_create (&monThreadCounter, NULL, threadCounter, (void*)&mydata)>0)
printf("Pthread Counter error\n");
pthread_join (monThreadCounter, NULL);
pthread_join (monThreadAlarm, NULL);
return 0;
}
void* threadCounter (void *arg)
{
struct data *myarg = (struct data *)arg;
srand(time(NULL));
if (pthread_mutex_lock(&myarg->mutex) > 0)
{
printf("ERROR Mutex lock1 Counter\n");
pthread_exit(NULL);
}
while(1)
{
myarg->Counter += rand()%10; /* We add a random number to the counter */
if(myarg->Counter > 20) /* If Counter is greater than 20, we should trigger the alarm*/
{
myarg->iteration += 1; /* Iteration counter, to check any shift between expected triggers and reality */
printf("Counter = %i(%i)-->",myarg->Counter,myarg->iteration);
if (pthread_mutex_unlock(&myarg->mutex) > 0) /* Unlock mutex before sending signal */
{
printf("ERROR Mutex Unlock Counter\n");
pthread_exit(NULL);
}
if (pthread_cond_signal (&myarg->condition) >0)
{
printf("COND SIGNAL ERROR\n");
pthread_exit(NULL);
}
if (pthread_mutex_lock(&myarg->mutex) > 0) /* We should get the lock again before testing/modifying any shared variable */
{
printf("ERROR Mutex lock2 Counter\n");
pthread_exit(NULL);
}
/* Wait for alarm to happen */
while (myarg->calls < myarg->iteration)
{
pthread_cond_wait(&myarg->condition, &myarg->mutex);
}
}
}
}
void* threadAlarm (void* arg)
{
struct data *myarg = (struct data *)arg;
while(1)
{
if (pthread_mutex_lock(&myarg->mutex) > 0)
{
printf("ERROR Mutex lock Alarm\n");
pthread_exit(NULL);
}
while(myarg->Counter<21)
{
printf("\nWAITING for trigger...\n");
if (pthread_cond_wait (&myarg->condition, &myarg->mutex)>0)
{
printf("ERROR COND WAIT\n");
pthread_exit(NULL);
}
}
myarg->calls+=1; // Calls counter, should be equal to iteration counter, overwise calls have been missed
printf("ALARM TRIGGERED! Call #%i/Iteration #%i -> COUNTER RESET\n",myarg->calls, myarg->iteration);
// Counter reset
myarg->Counter = 0;
if (pthread_mutex_unlock(&myarg->mutex) > 0)
{
printf("ERROR Mutex Unlock Alarm\n");
pthread_exit(NULL);
}
if (pthread_cond_signal (&myarg->condition) >0) //Signal back to Counter thread
{
printf("COND SIGNAL ERROR\n");
pthread_exit(NULL);
}
}
}