gpt4 book ai didi

c - 在信号处理程序中销毁 pthread mutex/rwlock

转载 作者:太空宇宙 更新时间:2023-11-04 02:41:47 26 4
gpt4 key购买 nike

如何在信号处理程序中正确销毁 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com