我正在尝试学习pthread / mutex,但是尽管在网上进行了大量的研究/阅读,我仍然无法理解这段代码出了什么问题:
#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);
}
}
这段代码应该有一个线程递增计数器一个随机值,直到它变得大于20,然后什么将触发另一个应该显示一条消息并重置计数器的等待线程的条件。等等。
我不明白的是,尽管我认为我正在使用互联网和pthread_cond_wait以及pthread_cond_signal,如网络中的各种示例所述,如果我不引入usleep来减慢它,它就不会像预期的那样运行下来。
使用usleep(10000)
,我得到了预期的输出:
WAITING for trigger...
Counter = 23(59)-->ALARM TRIGGERED! Call #59/Iteration #59 -> COUNTER RESET
WAITING for trigger...
Counter = 23(60)-->ALARM TRIGGERED! Call #60/Iteration #60 -> COUNTER RESET
WAITING for trigger...
Counter = 21(61)-->ALARM TRIGGERED! Call #61/Iteration #61 -> COUNTER RESET
呼叫/迭代计数器处于同步状态,证明每次达到条件时,都会正确触发“报警”线程。
但是,如果我减少睡眠,结果会变得奇怪。没有睡觉(注释掉),我得到了例如:
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_signal时都会触发等待线程,并且调用线程会等到被触发的线程释放互斥锁?
如果重要,我目前正在Linux Ubuntu上编码。
感谢您的帮助。
答案 0 :(得分:1)
这是预期的行为。一旦发出条件变量的信号,等待的线程最终会唤醒并争用互斥锁,但是不能保证信令线程在此之前无法重新获取互斥锁。 / p>
如果您希望计数器线程等待消耗警报,您需要对其进行实际编程才能执行此操作。你可以反过来使用相同的条件变量 - 在计数器线程中:
::
并在警报线程中,在递增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);
}
后的某个时刻致电pthread_cond_signal(&myarg->condition)
。
顺便说一下,你做需要你在警报线程中注释掉的myarg->calls
。考虑以下两种情况:
警报线程在其主循环开始时在while(myarg->Counter<21)
被阻止。计数器线程具有互斥锁,并且刚刚将pthread_mutex_lock()
递增到大于20的值。它解锁互斥锁并在警报线程有机会运行之前发出条件变量。警报线程然后运行,获取myarg->Counter
中的互斥锁和块 - 它将永远等待,因为我们现在确保计数器线程将等待警报消耗,然后再继续。
警报线程刚刚将计数器递减到零,解锁互斥锁,立即将其重新锁定在循环顶部并调用pthread_cond_wait()
。在计数器线程有机会获取互斥锁之前,pthread_cond_wait()
立即返回(由于&#34;虚假唤醒&#34;,这是允许的),即使计数器仍然存在,警报线程现在仍将继续零。
答案 1 :(得分:0)
这是工作版本,以防它可能对其他人有用:
#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);
}
}
}