c - 在读写器问题中什么时候唤醒作者?释放互斥体之前还是之后?

标签 c multithreading pthreads readerwriterlock

我想知道在我的情况下最好的做法是什么。 在下面的代码中,我在解锁互斥锁之前向编写者发出信号

void* Reader(void *context)
{
    while(TRUE)
    {
        LOCK(&g_mutex);
        ++g_activeReaders;
        UNLOCK(&g_mutex);

        printf("reader: %ld counter val: %d ", (long)context, g_counter);

        LOCK(&g_mutex);
        --g_activeReaders;
        printf("g_activeReaders: %d \n", g_activeReaders);
        if(0 == g_activeReaders)
        {
            SIGNAL(&g_cv);
        }
        UNLOCK(&g_mutex);
    }

    return NULL;
}

我想知道这是否出于某种原因更好,以防止死锁

void* Reader(void *context)
{
    int signalFlag;
        while(TRUE)
    {
                signalFlag = 0;
        LOCK(&g_mutex);
        ++g_activeReaders;
        UNLOCK(&g_mutex);

        printf("reader: %ld counter val: %d ", (long)context, g_counter);

        LOCK(&g_mutex);
        --g_activeReaders;
        printf("g_activeReaders: %d \n", g_activeReaders);
        if(0 == g_activeReaders)
        {
                        signalFlag = 1;
        }
        UNLOCK(&g_mutex);

                if(signalFlag)
                SIGNAL(&g_cv);
    }


    return NULL;
}

我的完整程序是:

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>

pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t g_cv = PTHREAD_COND_INITIALIZER;
int g_activeReaders = 0;
int g_counter;

#define LOCK(X) pthread_mutex_lock(X)
#define UNLOCK(X) pthread_mutex_unlock(X)
#define WAIT pthread_cond_wait
#define WAKE_ALL pthread_cond_broadcast
#define SIGNAL pthread_cond_signal
#define JOIN pthread_join
#define TRUE 1
#define N 3

void* Writer(void * context)
{
    while(TRUE)
    {
        LOCK(&g_mutex);
        while(g_activeReaders)
        {
            WAIT(&g_cv, &g_mutex);
        }

        ++g_counter;
        UNLOCK(&g_mutex);
    }

    return NULL;
}

void* Reader(void *context)
{
    while(TRUE)
    {
        LOCK(&g_mutex);
        ++g_activeReaders;
        UNLOCK(&g_mutex);

        printf("reader: %ld counter val: %d ", (long)context, g_counter);

        LOCK(&g_mutex);
        --g_activeReaders;
        printf("g_activeReaders: %d \n", g_activeReaders);
        if(0 == g_activeReaders)
        {
            SIGNAL(&g_cv);
        }
        UNLOCK(&g_mutex);
    }

    return NULL;
}

void InitWriters(pthread_t* writers, int count)
{
    int status;
    int i;

    for(i = 0; i < count; ++i)
    {
        status = pthread_create(&writers[i], NULL, Writer, NULL);

        if(status)
        {
            fprintf(stderr, "Writer create fail\n");
            exit(-1);
        }
    }
}

void InitReaders(pthread_t* readers, int count)
{
    int status;
    int i;

    for(i = 0; i < count; ++i)
    {
        long e = i;
        status = pthread_create(&readers[i], NULL, Reader, (void*) e);

        if(status)
        {
            fprintf(stderr, "readers create fail\n");
            exit(-1);
        }
    }
}

void JoinThreads(pthread_t* threads, int count)
{
    int status;
    int i;

    for(i = 0; i < count; ++i)
    {
        status = pthread_join(threads[i], NULL);

        if(status)
        {
            fprintf(stderr, "readers create fail\n");
            exit(-1);
        }
    }
}

int main(void)
{
    pthread_t writer;
    pthread_t readers[N];

    InitWriters(&writer, 1);
    InitReaders(readers, N);

    JoinThreads(&writer, 1);
    JoinThreads(readers, N);

    pthread_cond_destroy(&g_cv);
    pthread_mutex_destroy(&g_mutex);

    return 0;
}

我的实现基于(此处)找到的伪代码[ Reader/Writer implementation in C

最佳答案

正确性并不重要 - 两者都可以,并且不会出现死锁。

在使用不同线程优先级的情况下,在解锁互斥锁之前发出条件变量信号的实现可以确保正在等待条件变量的较高优先级线程将优先于较低优先级线程获得互斥锁。 - 正在等待互斥锁的优先级线程。

关于c - 在读写器问题中什么时候唤醒作者?释放互斥体之前还是之后?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57027915/

相关文章:

来自两个不同数组的字符匹配,尽管它们不同

multithreading - 在不支持 OpenMP 的情况下构建 BLAS、ATLAS 和 LAPACK

c++ - recursive_mutex 的最大所有权级别的下限?

c - 避免 POSIX 线程中的内存泄漏

将c和lua程序编译成单个可执行文件

编译 systrace 给出 "two or more data types in declaration specifiers"

c - "creat"Unix系统调用

multithreading - 常规-线程池中的线程数

Java多线程用于模拟数据

c - 如何修复我的 pthread 代码遇到的此错误?