- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
如何在信号处理程序中正确销毁 pthread mutex 或 rwlock?例如 - 在下面的代码中,我有主线程和 3 个其他线程。所有线程都使用互斥体和锁在某个数组上无限循环地执行一些任务。因为主线程也在做一些任务,所以退出程序的唯一方法是使用信号处理程序。但是这样我就不能破坏我的互斥锁/rwlock 对象,因为不能保证对象被解锁。如果我尝试解锁它,当然其中一个线程会再次锁定它。当我试图再次启动我的程序时,打印出损坏的结果。那么我该如何解决这个问题呢?我的代码有 rwlocks 的例子:
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <signal.h>
#include <stdlib.h>
#include "thread_data.h"
#include "exchange_types.h"
pthread_rwlock_t rwlock;
unsigned int main_time = 500000;
unsigned int shift_time = 1000000;
unsigned int mirror_time = 2000000;
unsigned int count_time = 4000000;
void signal_handler(int signo) {
// Destroying locked lock or mutex is UB
pthread_rwlock_destroy(&rwlock);
exit(1);
}
void* shift_letter_case_async( void* array ) {
while(1) {
if( pthread_rwlock_rdlock(&rwlock) < 0 )
handle_error("rdlock[shift]");
carray_t* arr = (carray_t*) array;
shift_letter_case( arr->array, arr->size );
if( pthread_rwlock_unlock(&rwlock) < 0 )
handle_error("unlock[shift]");
usleep(shift_time);
}
return NULL;
}
void* mirror_array_async( void* array ) {
while(1) {
if( pthread_rwlock_rdlock(&rwlock) < 0 )
handle_error("rdlock[mirror]");
carray_t* arr = (carray_t*) array;
mirror_array( arr->array, arr->size );
if( pthread_rwlock_unlock(&rwlock) < 0 )
handle_error("unlock[mirror]");
usleep(mirror_time);
}
return NULL;
}
void* count_async( void* array ) {
while(1) {
if( pthread_rwlock_wrlock(&rwlock) < 0 )
handle_error("wrlock[count]");
carray_t* arr = (carray_t*) array;
count_upper_letters( arr->array, arr->size );
if( pthread_rwlock_unlock(&rwlock) < 0 )
handle_error("unlock[count]");
usleep(count_time);
}
return NULL;
}
int main( int argc, char** argv ) {
/* Common data */
char letters[ 'z' - 'a' + 1 ];
size_t letter_len;
carray_t transferer;
/* pthread data */
pthread_t threads[3];
/* Initializing array */
letter_len = sizeof(letters);
for( int i = 0; i < letter_len; i++ )
letters[i] = 'a' + i;
transferer.array = letters;
transferer.size = letter_len;
/* Initializing signal handlers */
if ( signal(SIGINT, signal_handler) == SIG_ERR )
handle_error("signal[SIGINT]");
if ( signal(SIGTERM, signal_handler) == SIG_ERR )
handle_error("signal[SIGTERM]");
/* Initializing locks */
if( pthread_rwlock_init(&rwlock, NULL) < 0 )
handle_error("pthread_rwlock_init");
/* Initializing threads */
if( pthread_create( &threads[0], NULL, shift_letter_case_async, &transferer ) != 0 )
handle_error("phtread_create[shift_letter_case]");
if( pthread_create( &threads[1], NULL, mirror_array_async, &transferer ) != 0 )
handle_error("phtread_create[mirror_array]");
if( pthread_create( &threads[2], NULL, count_async, &transferer ) != 0 )
handle_error("phtread_create[count]");
while(1) {
if( pthread_rwlock_wrlock(&rwlock) < 0 )
handle_error("wrlock[main]");
print_array(letters, letter_len);
if( pthread_rwlock_unlock(&rwlock) < 0 )
handle_error("unlock[main]");
usleep(main_time);
}
return 0;
}
最佳答案
不要采用您提议的听起来非常冒险的方法,而是考虑通过安排向每个线程 发送信号来关闭,每个线程都通过执行有序关闭来响应。在这种情况下让主线程加入其他线程,以便它知道它们何时完成;然后它可以干净地拆除任何持久同步结构、临时文件等。
或者,只向主线程发送信号,并让它——以一种适当的同步方式——发出一个标志,每个其他线程将识别为关闭的指令,然后像上面那样继续(加入工作线程,然后拆掉)。
关于c - 在信号处理程序中销毁 pthread mutex/rwlock,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31147587/
当你有一个对象 OBJ1 包含一个关键部分 CS 和一个指向另一个对象 OBJ2 的指针时,我无法弄清楚什么是正确的关闭过程 假设您有两个函数 A 和 B。 A进入临界区,修改OBJ2中的数据后离开临
我有一个成员变量声明为 CComPtr m_spXMLDoc; XML 文档是这样创建的 CoCreateInstance(CLSID_DOMDocument, NULL, CLSCTX_INPROC
在我的工作平台中,我遇到了 session_destroy 问题 function logout() { $_SESSION['id'] = ''; session_destroy(); } 在这
如何使用 destroy 删除 jScrollPane。请您给出以下代码的简单示例: $(document).ready(function() { $(".div1").jScrollPane
这是我在这里发表的第一篇文章,但我想对社区表示感谢,因为通过来到这里并在已经得到解答的问题中找到解决方案,我已经无数次找到了问题的解决方案。 话虽这么说,我想进入正题。我正在 Code Gear 的
我在导航 View 中工作。在此导航 View 中,您可以偶然发现个人资料页面。在此个人资料页面中,您可以看到与当前个人资料相关的其他个人资料(基本上是数据 View 中显示的图片)。您可以通过点击这
我想销毁项目中的 session ,这样当我单击“注销”时,它会转到页面“KillSession.jsp”,在该文件中我编写了“session.invalidate();”然后我将用户重定向到登录页面
我有关于 Thread 的 2 问题,我只是想澄清一些事情。使用以下代码: public class MyThread implements Runnable { Boolean StopTh
我遇到了内存泄漏,代码类似于下面的代码(这是每个循环都有不同输入的模拟)。 问题 对象 Object_XXX 非常复杂,它与数据库以及其他填充了数据库数据的对象有连接。 for(int i=0
当我在 Python 中启动一个类时,我给它一些值。然后我调用类中执行某些操作的方法。这是一个片段: class TestClass(): def __init__(self):
我想删除所有 div、类、属性和几乎所有 CKEDITOR 添加到 DOM 的内容。例如调用 jquery tabs("destroy");将删除所有由 jQuery 选项卡添加的 div。我怎样才能
我想清除析构函数中的一个映射,但我不知道它是否为空。如果我按如下方式清除它可以吗? for(std::map::iterator it = m_map.begin(); it != m_map.end
我正在尝试克隆 TikTok 应用。对于主屏幕,我制作了一个 VerticalViewPager(自定义 View 分页器),其中包含“点赞”按钮、标题和评论。我正在从 Firebase 检索视频。
我正在制作自己的游戏。目标之一是在世界中拥有尽可能多的物体。在这个游戏中,需要在一些不可预测的时间段内创建许多对象(比如武器开火会创建一个对象),一旦该弹丸击中某物,该对象也需要被摧毁(也许它击中的东
有没有办法在 JavaScript 中破坏 HTML5 WebWorker? 这是我的情况:我有一个 Web 应用程序生成相当数量的 WebWorker(在 16 到 32 之间的任何地方)来优化一些
如何销毁 php 中的 session ? 事情是当用户点击注销按钮时, session 将结束,他将被重定向到 index.php 这是我的代码 客户.php 这是来自用户想要再次登录的 ind
关于 GtkBuilder 的问题。 当我们取消引用构建器指针时,它是否会破坏构建器创建的所有屏幕/小部件? if( builder_ptr ) g_object_unref(G_OBJECT
有没有办法销毁 WebView 实例?如果页面加载,并说视频开始播放,我希望能够,当我隐藏 WebView 时,基本上可以销毁它,或者至少重置它。 我知道我可以听 visibleProperty 并执
我有一组可拖动的元素。如何删除可拖动功能? $('.draggable').draggable('disable') 在我的情况下不是一个选项 $('.draggable').draggable('d
下面的代码会抛出一个 EZDecompressionError 消息 'Invalid ZStream operation' 每当行 Reader.Free 被执行。有人可以告诉我这段代码有什么问题吗
我是一名优秀的程序员,十分优秀!