c - 当 pthread_mutex 在线程 A 中被锁定和解锁时,为什么另一个线程 B 正在运行?

标签 c linux multithreading pthreads mutex

根据手册页

pthread_mutex_lock locks the given mutex. If the mutex is currently unlocked, it becomes locked and owned by the calling thread, and pthread_mutex_lock returns immediately. If the mutex is already locked by another thread, pthread_mutex_lock suspends the calling thread until the mutex is unlocked.

我的理解是当 line 3 执行时 main thread 拥有 mtx 的所有权。然后它执行其临界区操作,然后到达 第 4 行 并解锁 mtx。我的问题是 -

  1. mtx被锁定时,另一个线程能否并发运行?
  2. line 2 有什么用,因为 newThread 只有在 line 4 执行后才能解锁 mtx ,从而使 第 2 行 变得多余?
  3. 如果取消注释 第 1 行 会发生什么情况?

    #include<stdio.h>
    #include<pthread.h>
    #include<semaphore.h>
    #include<unistd.h>
    
    
    sem_t bin_sem;
    pthread_mutex_t mtx;
    char message[100];
    
    
    void * thread_function(void * arg)
    {   
        int x;
        char message2[10];
        while(1)
        {   
    
    //      pthread_mutex_lock(&mtx); //line 1
            printf("thread2 : waiting..\n\n");
            sem_wait(&bin_sem);     
            printf("hi i am the new thread waiting inside critical..\n");
            scanf("%s",message);
            printf("You entered in newthread:%s\n\n",message);
            sem_post(&bin_sem);
            pthread_mutex_unlock(&mtx); //line 2
    
        }
    
    }
    
    int main(void)
    {
        pthread_t athread;
        pthread_attr_t ta;
        char message2[10];
        int x;
    
        sem_init(&bin_sem,0,1);
    
        pthread_mutex_init(&mtx,NULL);
    
        pthread_attr_init(&ta);
        pthread_attr_setschedpolicy(&ta,SCHED_RR);                                                                                                                                                                                                       
    
        pthread_create(&athread,&ta,thread_function,NULL);
        while(1)
        {   
            pthread_mutex_lock(&mtx); //line 3
            printf("main waiting..\n\n");
            sem_wait(&bin_sem); 
            printf("hi i am the main thread waiting inside critical..\n");
            scanf("%s",message);
            printf("You entered in main:%s\n\n",message);
            sem_post(&bin_sem);
            pthread_mutex_unlock(&mtx); //line 4
        }
        sleep(5);       
    }
    

最佳答案

互斥体一种实现临界区的机制。

pthread_mutex_lock(x)pthread_mutex_unlock(x) 调用之间的任何代码在任何给定时间都只会在一个线程中执行。就这样。

所以...

1. Can the other thread concurrently run when mtx is locked?

如果它没有锁定 mtx,那么当然可以。

2. What is the use of line 2 since newThread can only unlock mtx when line 4 has been executed, and thus makes line 2 redundant?

互斥体变得无用,你也得到UB因为你在没有锁定它的线程中解锁它:

If the mutex type is PTHREAD_MUTEX_DEFAULT...
Attempting to unlock the mutex if it was not locked by the calling thread results in undefined behavior.

(默认情况下,您获得互斥类型 PTHREAD_MUTEX_DEFAULT)

3. What would happen if line 1 is uncommented?

你得到 thread starvation ,因为互斥量几乎一直处于锁定状态,并且在解锁后立即重新锁定(POSIX 不保证互斥量的公平性)。

POSIX semaphore在某些情况下确实提供了公平性(当您使用 SCHED_FIFOSCHED_RR 调度程序时),但是 heavier .

我不太明白您要实现的目标(该应用程序看起来做作)。在实际应用程序中,任一线程需要执行的操作可能有一些逻辑顺序。因此,如果信号量适合您,我会保留它并删除互斥体。

关于c - 当 pthread_mutex 在线程 A 中被锁定和解锁时,为什么另一个线程 B 正在运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53527328/

相关文章:

c - C 程序在查找整数三元组 (x,y,z) 时出错,使得 n^x + n^y = n^z 对于给定的 n 范围

c - 在 GDB 中,您可以将内存设置为 char 数组吗?

c - 解锁时向多个线程发出信号的一把锁

java - hibernate 一个线程,直到另一个线程参加了一个事件

c++ - 将 while 循环转换为数学公式

c# - 有人可以破译 timeGetTime() 或 QueryPerformanceCounter/QueryPerformanceFrequency 是否具有较低的开销或/和准确性?

python - 学习 Python/Apache/Linux

linux - 初学者 bash : How can I use the output of echo in a string comparison test?

c - 在 python 中使用 keras+tensorflow 进行训练并在 C/C++ 中进行预测

android - 在后台更新位置