pthread互斥锁两个线程可互换锁定/解锁是有效的方法吗?

Who*_*ami 1 c linux pthreads

我读到互斥锁应该被锁定它的同一个线程解锁.让我们考虑以下场景.

我有一个互斥体变量说myMute,T1,T2是两个线程.

  1. T1锁定myMute.

  2. T2解锁myMute.

  3. T2myMute.

  4. T1解锁myMute.

这种从不同线程锁定/解锁的有效方法是什么?

hmj*_*mjd 5

不,这是不正确的.从pthread_mutex_lock手册页:

如果某个线程尝试解锁未锁定的互斥锁或解锁的互斥锁,则会导致未定义的行为.

正确排序示例:

  • T1锁定myMutex
  • T2锁定myMutex(阻止等待T1解锁互斥锁)
  • T1解锁myMutex(T2现在锁定互斥锁)
  • T2解锁myMutex

编辑:

pthread_cond_wait()为简洁起见,省略了错误检查的小例子:

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

volatile int value = 0;
pthread_mutex_t mymutex;
pthread_t thread_one;
pthread_t thread_two;
pthread_cond_t cond;

void* main_one(void* ignored)
{
    while (value < 10)
    {
        pthread_mutex_lock(&mymutex);
        pthread_cond_wait(&cond, &mymutex);
        fprintf(stderr, "T1: value=%d\n", value);
        pthread_mutex_unlock(&mymutex);
    }
    return (void*)0;
}

void* main_two(void* ignored)
{
    int i;

    for (i = 0; i < 10; i++)
    {
        pthread_mutex_lock(&mymutex);
        value++;
        fprintf(stderr, "T2: value=%d\n", value);
        pthread_cond_broadcast(&cond);
        fprintf(stderr, "Broadcasted but T1 cannot continue for 1 second\n");
        sleep(1);
        pthread_mutex_unlock(&mymutex);
        pthread_yield();
    }

    return (void*)0;
}

void start_thread(void* (*a_entry_point)(void*),
                  pthread_t* a_handle)
{
    pthread_attr_t thread_attributes;

    pthread_attr_init(&thread_attributes);
    pthread_attr_setdetachstate(&thread_attributes, PTHREAD_CREATE_JOINABLE);
    pthread_create(a_handle, &thread_attributes, a_entry_point, 0);
}

int main()
{
    pthread_mutexattr_t attr;
    pthread_t thread_one_handle;
    pthread_t thread_two_handle;

    /* Init mutex. */
    pthread_mutexattr_init(&attr);
    pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
    pthread_mutex_init(&mymutex, &attr);

    /* Init condition. */
    pthread_cond_init(&cond, 0);

    /* Start threads. */
    start_thread(main_one, &thread_one_handle);
    start_thread(main_two, &thread_two_handle);

    /* Wait for threads. */
    pthread_join(thread_one_handle, 0);
    pthread_join(thread_two_handle, 0);

    /* Clean up. */
    pthread_cond_destroy(&cond);
    pthread_mutex_destroy(&mymutex);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译gcc -Wall -Werror -D_GNU_SOURCE main.c -o main -pthread.

  • 必须在锁定互斥锁的情况下调用`pthread_cond_wait`,或者像以前一样调用未定义的行为. (2认同)