pthread_cond_signal 会导致崩溃吗?

发布于 2024-12-12 06:13:46 字数 378 浏览 0 评论 0原文

我有几个线程相互传递数据并对其进行一些处理。一旦我在最后两个线程之间进行同步,程序就开始崩溃。我对线程没有太多经验,所以我没有调试,而是评论了最后一个线程的全部内容,因此它只是在 while 循环中运行,另一个线程保持不变,除了同步部分,这只是

pthread_mutex_lock(&mutex);
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);

我随后也评论的这使得应用程序可以正常运行而不会崩溃。

程序的其余部分绝对不依赖于互斥体或条件变量。如果我只评论 pthread_cond_signal(&cond); 它也有效。对正在发生的事情有什么想法吗?

I got a couple threads which pass data to each other and process them a little. Once I put synchronization between the last two threads the program started crashing. I don't have much experience with threads so instead of debugging i just commented the whole content of the last thread so it's just running while cycle, the other thread remains the same except for the synchronization part which is just

pthread_mutex_lock(&mutex);
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);

and which i then also commented which made the app work without crash.

There is absolutely nothing dependent on the mutex or the conditional variable in the rest of the program. It also works if I only comment the pthread_cond_signal(&cond);. Any ideas on what is going on?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

万人眼中万个我 2024-12-19 06:13:47

好吧,该代码实际上没有任何意义。如果您没有更改受互斥锁保护的任何变量的值,那么发出条件变量信号有何意义?此外,您可以在持有或不持有互斥锁的情况下发出信号,因此不需要锁定/解锁。 (如果您不了解条件变量的基础知识,请参阅条件变量条件变量。)

但它崩溃的唯一可能的方式是如果 cond 未正确初始化、cond 被破坏或 cond > 是被覆盖其内存的东西损坏。

Well, that code doesn't really make any sense. If you didn't change the value of any variables protected by the mutex, what was the sense in signalling the condition variable? Also, you can signal with or without holding the mutex, so the lock/unlock is not needed. (See Condition Variables if you don't understand the basics of what condition variables are for.)

But the only likely ways for it to crash is if cond isn't properly initialized, cond was destroyed, or cond was corrupted by something overwriting its memory.

后知后觉 2024-12-19 06:13:47

发生崩溃的唯一原因是调用未定义的行为。这可能发生在 pthread_cond_signal 调用之前的程序中的任何位置,或者 UB 可能位于调用本身中(通过传递未初始化的条件变量的地址)。如果没有更多信息,就无法知道。

The only way for a crash to ever happen is when you've invoked undefined behavior. This could have happened anywhere in the program prior to the pthread_cond_signal call, or the UB could be in the call itself (by passing the address of an uninitialized condition variable). Without more information, there's no way to know.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文