gpt4 book ai didi

c - 线程永远不会获得锁(pthread_mutex_lock)

转载 作者:行者123 更新时间:2023-12-03 13:14:59 28 4
gpt4 key购买 nike

故事
根据手册页https://linux.die.net/man/3/pthread_mutex_lock

The mutex object referenced by mutex shall be locked by calling pthread_mutex_lock(). If the mutex is already locked, the calling thread shall block until the mutex becomes available.


我有一个带有线程的程序。这是程序流程:
  • 主进程线程始终在循环内调用pthread_mutex_lock
  • 主进程持有该锁时,要求锁定的线程会阻塞(等待授予锁)。
  • 主进程使用pthread_mutex_unlock释放锁时,线程应该突然获得该锁。
  • 主进程再次请求锁定时,主进程应等待线程释放锁定。

  • 问题在于,在第3点处,一旦 主进程释放了锁, 线程不会突然获得该锁。主进程在下一个循环周期(在第4点)调用 pthread_mutex_lock时首先获取它。
    如何处理这种情况?
    问题
    主进程释放锁定后,如何使 线程获得锁定?
    重现问题的简单代码
    #include <stdio.h>
    #include <unistd.h>
    #include <pthread.h>

    pthread_mutex_t my_mutex = PTHREAD_MUTEX_INITIALIZER;

    void *
    my_thread(void *p)
    {
    (void)p;

    while (1) {
    pthread_mutex_lock(&my_mutex);
    printf("The thread is holding the lock...\n");
    sleep(1);
    pthread_mutex_unlock(&my_mutex);
    }
    }

    int
    main()
    {
    pthread_t t;

    pthread_create(&t, NULL, my_thread, NULL);
    pthread_detach(t);

    while (1) {
    pthread_mutex_lock(&my_mutex);
    printf("The main process is holding the lock...\n");
    sleep(1);
    pthread_mutex_unlock(&my_mutex);
    }
    }
    编译并运行
    gcc test.c -o test -lpthread
    ./test
    预期结果
    The main process is holding the lock...
    The thread is holding the lock...
    The main process is holding the lock...
    The thread is holding the lock...
    The main process is holding the lock...
    The thread is holding the lock...
    The main process is holding the lock...
    ...
    实际结果
    The main process is holding the lock...
    The main process is holding the lock...
    The main process is holding the lock...
    The main process is holding the lock...
    The main process is holding the lock...
    The main process is holding the lock...
    The main process is holding the lock...
    ...
    顺序调用故事
    main   -> [1] call lock (get the lock)
    thread -> [2] call lock (waiting for main to unlock)
    main -> [3] call unlock
    thread -> [4] (still does not get the lock from [2], why? even though it has been unlocked?)
    main -> [5] lock (get the lock again)
    thread -> [6] (still does not get the lock from [2])
    main -> [7] call unlock
    thread -> [8] (still does not get the lock from [2], why? even though it has been unlocked?)
    main -> [9] lock (get the lock again)
    ... and so on ...
    概括 pthread_mutex_lock不保证锁定请求的顺序。

    最佳答案

    pthread_mutex_lock保证它将锁定,直到互斥锁可用为止。这并不意味着每个lock()调用都会进入一个队列,并且可以保证接下来获得互斥锁。这仅意味着没有其他人可以同时拥有该锁。
    如果您需要一定顺序,则可以选择使用条件变量。这样,您可以将一个标志设置为下一个应获取互斥体的成员。然后,您可以等待互斥锁,直到达到预期值为止。参见https://linux.die.net/man/3/pthread_cond_wait
    另外,如果您的示例仍然如上所述处于 sleep 状态,则可以在调用unlock()之后移动 sleep 状态。虽然这并不是严格意义上的保证,但它绝对可以通过简单的测试来解决问题。我不建议这种方法用于更严重/复杂的事情。
    编辑:正确地添加了Shawn,如果您不在乎其他线程,则还可以使用pthread_yield(1)允许另一个线程获取互斥量。 sched_yield(2)中描述了一些复杂的产量。
    PS:我会发表评论,但是我的代表现在已经足够高了:)

    关于c - 线程永远不会获得锁(pthread_mutex_lock),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65007137/

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