销毁信号处理程序中的 pthread mutex/rwlock
Destroying pthread mutex/rwlock in signal handler
如何正确销毁信号处理程序中的 pthread mutex 或 rwlock?例如 - 在下面的代码中,我有主线程和 3 个其他线程。所有线程都使用互斥体和锁在某个数组上无限循环地执行一些任务。因为主线程也在做一些任务,所以退出程序的唯一方法是使用信号处理程序。但是这样我就不能破坏我的 mutex/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;
}
不要采用您提议的听起来非常危险的方法,而是考虑通过安排向每个线程发出信号 来关闭,每个线程都通过执行有序关闭来响应。在这种情况下让主线程加入其他线程,以便它知道它们何时完成;然后它可以干净地拆除任何持久同步结构、临时文件等。
或者,只向主线程发送信号,并让它——以一种适当同步的方式——发出一个标志,每个其他线程将识别为关闭指令,然后像上面那样继续(加入工作线程, 然后拆掉).
我认为我对此的第一个幼稚尝试是将线程的 while(1) 和 main 更改为 while(运行ning),"running" 定义为:
volatile int running = 1;
将 signal_handler 更改为:
void signal_handler(int signo) {
running = 0;
}
您可以在 main 中的 return 之前正常加入线程。我没有 运行 这个所以我可能完全错了。此外,您可能希望为 usleep 函数提供 "if (running)" 谓词。
如何正确销毁信号处理程序中的 pthread mutex 或 rwlock?例如 - 在下面的代码中,我有主线程和 3 个其他线程。所有线程都使用互斥体和锁在某个数组上无限循环地执行一些任务。因为主线程也在做一些任务,所以退出程序的唯一方法是使用信号处理程序。但是这样我就不能破坏我的 mutex/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;
}
不要采用您提议的听起来非常危险的方法,而是考虑通过安排向每个线程发出信号 来关闭,每个线程都通过执行有序关闭来响应。在这种情况下让主线程加入其他线程,以便它知道它们何时完成;然后它可以干净地拆除任何持久同步结构、临时文件等。
或者,只向主线程发送信号,并让它——以一种适当同步的方式——发出一个标志,每个其他线程将识别为关闭指令,然后像上面那样继续(加入工作线程, 然后拆掉).
我认为我对此的第一个幼稚尝试是将线程的 while(1) 和 main 更改为 while(运行ning),"running" 定义为:
volatile int running = 1;
将 signal_handler 更改为:
void signal_handler(int signo) {
running = 0;
}
您可以在 main 中的 return 之前正常加入线程。我没有 运行 这个所以我可能完全错了。此外,您可能希望为 usleep 函数提供 "if (running)" 谓词。