【问题标题】:Destroying pthread mutex/rwlock in signal handler在信号处理程序中销毁 pthread mutex/rwlock
【发布时间】:2015-06-30 20:09:54
【问题描述】:

如何正确销毁信号处理程序中的 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 multithreading pthreads mutex


    【解决方案1】:

    与您建议的听起来非常冒险的方法不同,考虑通过安排向每个线程发出信号来关闭,每个线程通过执行有序关闭来响应该信号。在这种情况下让主线程加入其他线程,以便知道它们何时完成;然后它可以干净地拆除任何持久同步结构、临时文件等。

    或者,只向主线程发出信号,并让它——以适当同步的方式——引发一个标志,每个其他线程都会将其识别为关闭指令,然后像上面那样继续(加入工作线程,然后拆掉)。

    【讨论】:

      【解决方案2】:

      我认为我对此的第一次天真尝试是将线程的 while(1) 和 main 更改为 while(running),其中“运行”定义为:

      volatile int running = 1;
      

      将 signal_handler 更改为:

      void signal_handler(int signo) {
          running = 0;
      }
      

      您可以在 main.return 之前正常加入线程。我没有运行这个,所以我可能完全错了。此外,您可能希望为 usleep 函数添加“if (running)”谓词。

      【讨论】:

        猜你喜欢
        • 2011-07-14
        • 1970-01-01
        • 2014-11-29
        • 1970-01-01
        • 2015-05-18
        • 2012-01-30
        • 2015-06-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多