- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试学习 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
调用/迭代计数器是同步的,证明每次达到条件时,“警报”线程都会被正确触发。
但是,如果我减少 sleep ,结果就会变得很奇怪。根本没有 sleep (已注释掉),我得到例如:
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 上编码。
感谢您的帮助。
最佳答案
这是预期的行为。一旦您向条件变量发出信号,等待线程最终将醒来并争夺互斥体,但不能保证发信号线程在此之前无法重新获取互斥体。
如果您希望计数器线程等待警报被消耗,您需要实际对其进行编程来执行此操作。您可以以相反的方式使用相同的条件变量 - 在计数器线程中:
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()
在其主循环的开始。计数器线程具有互斥体并且刚刚递增 myarg->Counter
为大于 20 的值。它会在警报线程有机会运行之前解锁互斥体并向条件变量发出信号。然后警报线程运行,获取互斥体并阻止 pthread_cond_wait()
- 它将永远在这里等待,因为我们现在已经确保计数器线程将等待警报被消耗后再继续。
警报线程刚刚将计数器递减至零,解锁互斥体,立即在循环顶部重新锁定它并调用 pthread_cond_wait()
。 pthread_cond_wait()
在计数器线程有机会获取互斥锁之前立即返回(由于“虚假唤醒”,这是允许的),并且警报线程现在将继续,即使计数器仍然为零。
关于c++ - pthread_cond_wait/signal 和 mutex 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35565946/
我对java有点陌生,所以如果我犯了一个简单的错误,请原谅我,但我不确定我哪里出错了,我收到的错误是“预期的.class,预期的标识符,而不是声明, ';'预期的。”我尝试了不同的方法,并从这些方法中
This question already has answers here: chai test array equality doesn't work as expected (3个答案) 3年前
我正在学习 Java(对不起,我的英语很差,这不是我的母语),当我在 Eclipse (JavaSE-1.7) 中在我输入的每个“try”中执行“try-finally” block 时,会出现以下消
我收到两个错误,指出 token 上的语法错误,ConstructorHeaderName expected instead & token “(”上的语法错误,< expected 在线: mTM.
我找不到错误。 Eclipse 给我这个错误。每个 { } 都是匹配的。请帮忙。 Multiple markers at this line - Syntax error on token “)”,
代码: import java.awt.*; import javax.swing.*; import java.awt.event.*; public class DoubleIt extends
我正在用 python(Vs 代码)编写代码,但出现此错误: Expected ")" Pylance 错误发生在:def main() 我试着运行我的 main 并将它打印到我的屏幕上。我用谷歌搜
我正在尝试按照 documentation 中的建议使用异步函数。但我收到此错误 意外的 token ,预期 ( async function getMoviesFromApi() { try
Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。 想改善这个问题吗?更新问题,以便将其作为on-topic
Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。 想改善这个问题吗?更新问题,以便将其作为on-topic
第一行包含一个表示数组长度的整数p。第二行包含用空格分隔的整数,这些整数描述数组中的每个元素。第三行打印一个整数,指示负数组的数量。 package asgn3; import java.util.*
好的,我是初学者,我必须修复此 java 表达式语言代码才能在我的系统 (Windchill) 中工作,但看起来我在语法中遗漏了一些内容: LWCNormalizedObject lwc =
我无法编译我的程序! 我想我缺少一个花括号,但我怎么也看不出在哪里! import javax.swing.*; import java.awt.*;
我的 jQuery 代码有问题,我的 Firebug 向我发出警告:需要选择器。 这是代码: $("img[id$='_tick']").each(function() { $(this).c
我的新类(class) Fountainofyouth 遇到了问题。尝试构建整个项目后,调试器显示 warning: extended initializer lists only available
我已经从 Java 转向 CPP,并且正在努力围绕构造构造函数链进行思考,我认为这是我的问题的根源。 我的头文件如下: public: GuidedTour(); GuidedTour(string
鉴于以下 for(var i=0; i< data.cats.length; i++) list += buildCategories(data.cats[i]); jsLint 告诉我 Expect
我有这个 json,但 Visual Studio Code 在标题中给了我警告。 [ { "title": "Book A", "imageUrl": "https:
我正在尝试编写一个有条件地禁用四个特殊成员函数(复制构造、移动构造、复制赋值和移动赋值)的包装类,下面是我用于测试目的的快速草稿: enum class special_member : uint8_
所以我用 F# 编写了一个非常简单的程序,它应该对 1000 以下的所有 3 和 5 的倍数求和: [1..999] |> List.filter (fun x -> x % 3 = 0 || x %
我是一名优秀的程序员,十分优秀!