- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我的程序使用 pthread_cond_wait
和 pthread_cond_signal
执行“线程同步”。它似乎大约有 20 次失败。
我有一个初始化为 0 的共享变量 count
。一个线程递增 count
直到 count
为 20 ,然后向条件变量发出信号。
代码如下。
void* inc_count(void *parm)
{
int i;
for(i = 0 ; i <25; i ++)// adds count to 25
{
pthread_mutex_lock(&mutex1);
count ++;
printf("Thread %lld , count = %d\n",(long long int)pthread_self(),count);
if(count == 20)
{
pthread_cond_signal(&cond);
printf("Thread %lld sends a signal!\n",(long long int)pthread_self());
}
pthread_mutex_unlock(&mutex1);
}
pthread_exit(NULL);
}
void* watch_count(void *parm)
{
while(count < 20)
{
pthread_mutex_lock(&mutex1);
pthread_cond_wait(&cond,&mutex1);
printf("Thread %lld receives the signal!\n",(long long int)pthread_self());
}
pthread_mutex_unlock(&mutex1);
pthread_exit(NULL);
}
int main()
{
pthread_t pt1,pt2,pt3,pt4;
pthread_mutex_init(&mutex1,NULL);
pthread_mutex_init(&mutex2,NULL);
pthread_cond_init(&cond,NULL);
pthread_create(&pt1,NULL,inc_count,NULL);
pthread_create(&pt2,NULL,watch_count,NULL);
pthread_join(pt1,NULL);
pthread_join(pt2,NULL);
}
从图中可以看出线程pt2没有收到信号,为什么?
最佳答案
你的 watch_count()
功能有问题:
while
中循环并仅在其外部解锁(可以尝试多次锁定而不解锁)count < 20
时没有持有互斥锁要修复它,您需要在循环之前而不是在循环内部锁定互斥量:
pthread_mutex_lock(&mutex1);
while(count < 20)
{
pthread_cond_wait(&cond,&mutex1);
printf("Thread %lld receives the signal!\n",(long long int)pthread_self());
}
pthread_mutex_unlock(&mutex1);
这将防止您在解锁前多次锁定互斥锁,并确保 count
在while
中的测试之间无法修改和发送信号条件和 pthread_cond_wait
( inc_count()
更改为 count
并且发送信号必须发生在 while 循环之前或 pthread_cond_wait()
期间)。
即使进行了这些更改,如果count
,仍然有可能根本不会发生等待。到时候已经20了watch_count()
不过,检查一下。
关于c - 为什么 pthread_cond_signal 有时不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27191641/
我正在研究 Pthread 的条件变量。当我阅读 pthread_cond_signal 的解释时,我看到以下内容。 The pthread_cond_signal() function shall
我有几个线程,它们相互传递数据并对其进行一些处理。一旦我在最后两个线程之间进行同步,程序就开始崩溃。我对线程没有太多经验,所以我没有调试,而是评论了最后一个线程的全部内容,因此它只是在 while 循
我有以下代码为 N 个线程运行,计数 = 0 最初作为共享变量。每个变量都在线程工作之前初始化。我正在尝试仅为 MAX 线程数执行代码的关键部分。 void *tmain(){ while(1){
pthread_cond_signal 是否正好解除阻塞一个线程?如果不是,它会释放一个以上的线程是什么情况?规范是这样说的: The pthread_cond_signal() function s
我试图通过线程之间的同步(目前从 2 开始)来模拟上下文切换,以便每个线程充当一个进程,并且进程切换发生在每个 TIMESLICE 间隔(目前为 3 毫秒)。通过一次仅执行一个线程进行的线程切换运行得
嗨,我是 C 新手,正在尝试了解互斥体、条件和线程。我了解线程如何工作的基础知识。如果我错了,请纠正我,据我所知,一个线程正在等待另外两个线程发送信号以唤醒他。由于互斥体的原因,代码一次只能由一个线程
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
如果调用 pthread_cond_signal 死锁,可能是什么原因导致的? 据我了解(man page),它是在内部使用互斥锁实现的,但是什么会导致此内部互斥锁操作死锁? 编辑:我正在调试一个在某
调用 pthread_cond_signal 的线程在释放信号线程之前重新获取互斥体。 下面的代码显示了手头问题的一个简单示例。主线程将持有锁,创建工作线程,然后进入一个循环,在数据进入时打印数据。它
让我们假设有一个线程调用 pthread_cond_wait 并等待信号: pthread_mutex_lock(&m); ..... while(run) {
我需要启动一堆线程,并想优雅地将它们关闭。 我正在尝试使用 pthread_cond_signal/pthread_cond_wait 来实现此目的,但遇到了问题。 这是我的代码。首先是 thread
我有一个程序在其中一个线程调用 pthread_cond_siganl(或广播)时死锁。该问题在主程序中可 100% 重现。我无法弄清楚它有什么问题,因此提取了调用 wait 和 signal 的代码
当我在我的线程函数中调用“pthread_cond_signal”时,这个调用会解锁我当前使用的互斥量吗? (不是 pthread_cond_wait 正在等待的那个)。 最佳答案 只有给 pthre
当线程调用 pthread_cond_signal() 时,Unix 网络编程说 pthread_cond_signal() 只会通知一个线程,因为它不是 pthread_cond_broadcast
首先让我提供一些背景知识。生产代码中有两个线程,同步是通过等待和信号完成的。下面给出代码的基本结构。 main.c 创建线程。 main.c 还调用 funca() 向另一个线程发出信号。互斥量和条件
如果有多个线程在等待一个条件变量,那么在 pthread_cond_signal 上唤醒的顺序是什么。我读过 pthread_cond_signal 不一定按 sleep 顺序醒来。所以这可能会导致饥
我在某处读到我们应该在调用 pthread_cond_signal 之前锁定 mutex 并在调用之后解锁互斥锁: The pthread_cond_signal() routine isused t
关于这个: How To Use Condition Variable 假设我们有多个执行此类代码的消费者线程(从引用的页面复制): while (TRUE) { s = pthread_mu
我目前正在学习 POSIX 线程 (pthread)。 我现在创建了一个简单的程序,它将共享值增加 7 直到超过 10000,然后它应该向下一个线程发出条件信号,将其减少 3 直到低于 1000。最后
干杯, 我有 2 个线程导致逻辑死锁 => d_santa 和 d_patuljak(抱歉有些文章是用克罗地亚语写的,我没有时间翻译) d_santa 这样做 void d_santa(){ //do
我是一名优秀的程序员,十分优秀!