c - 具有广播信号的线程和互斥量

标签 c linux multithreading

<分区>

在场景 2 中,为什么所有线程都没有被触发? 不同的互斥变量有什么问题?

场景 1:

我正在创建四个线程。每个线程都分配给不同的核心,所有四个线程都在等待具有相同条件和互斥变量的信号。在传递具有相同条件变量的广播信号时,所有线程都被触发。

   #define _GNU_SOURCE
    #include<stdio.h>
    #include<stdlib.h>
    #include<unistd.h>
    #include<pthread.h>
    #include<sched.h>    
pthread_cond_t condVar=PTHREAD_COND_INITIALIZER;        
pthread_mutex_t mutexVar=PTHREAD_MUTEX_INITIALIZER;        
void *thread1(void *ptr)        
{        
    int iVar=1;        
    printf("Thread %d\n",iVar);        
    pthread_mutex_lock(&mutexVar);        
    pthread_cond_wait(&condVar,&mutexVar);        
    pthread_mutex_unlock(&mutexVar);        
    printf("Thread %d is unblocked...\n",iVar);        
}        

void *thread2(void *ptr)        
{        
    int iVar=2;        
    printf("Thread %d\n",iVar);        
    pthread_mutex_lock(&mutexVar);        
    pthread_cond_wait(&condVar,&mutexVar);        
    pthread_mutex_unlock(&mutexVar);        
    printf("Thread %d is unblocked...\n",iVar);        

}        

void *thread3(void *ptr)        
{        
    int iVar=3;        
    printf("Thread %d\n",iVar);        
    pthread_mutex_lock(&mutexVar);        
    pthread_cond_wait(&condVar,&mutexVar);        
    pthread_mutex_unlock(&mutexVar);        
    printf("Thread %d is unblocked...\n",iVar);        

}        

void *thread4(void *ptr)        
{        
    int iVar=4;        
    printf("Thread %d\n",iVar);        
    pthread_mutex_lock(&mutexVar);        
    pthread_cond_wait(&condVar,&mutexVar);        
    pthread_mutex_unlock(&mutexVar);        
    printf("Thread %d is unblocked...\n",iVar);        

}        


int main()        
{        
    int i,j=2;        
    pthread_t t[4];        
    cpu_set_t cpuset;        

    pthread_create(&t[0],0,thread1,0);
    pthread_create(&t[1],0,thread2,0);
    pthread_create(&t[2],0,thread3,0);
    pthread_create(&t[3],0,thread4,0);

    for(i=0;i<4;i++)            //Assign dedicated core for each thread
    {
        CPU_ZERO(&cpuset);
        CPU_SET(j,&cpuset);
        if((pthread_setaffinity_np(t[i],sizeof(cpu_set_t),&cpuset))!=0)
            perror("Set_affinity");
        j+=2;
    }
    sleep(5);
    pthread_cond_broadcast(&condVar);
    while(1);
}    

输出:

线程 1

线程 4

线程 2

线程 3

线程 1 未被阻塞...

线程 4 未阻塞...

线程 2 未阻塞...

线程 3 未阻塞...

场景 2:

我为每个线程创建了不同的互斥变量,而不是相同的互斥变量。在四个线程之间传递广播信号时,只有一个线程被触发,其余线程正在等待信号。

场景 2 的输出:

线程 1

线程 2

线程 4

线程 3

线程 1 未被阻塞...

最佳答案

Instead of same mutex variable I have created different mutex to each thread.While passing broadcast signal among the four,only one thread is getting triggered remaining threads were waiting for signal.

这是对条件变量和未定义行为的误用。 Per the POSIX pthread_cond_wait() documentation :

When a thread waits on a condition variable, having specified a particular mutex to either the pthread_cond_timedwait() or the pthread_cond_wait() operation, a dynamic binding is formed between that mutex and condition variable that remains in effect as long as at least one thread is blocked on the condition variable. During this time, the effect of an attempt by any thread to wait on that condition variable using a different mutex is undefined.

关于c - 具有广播信号的线程和互斥量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49149214/

相关文章:

mysql - 如何调用此 shell 脚本将 mysqldump 转换为 SQLite 兼容语法

c# - 哪个 C# 程序集包含 Invoke?

java - 线程中的某些代码不重复

检查输入字符串与 C 中给定宏的匹配

c - 即使在向数组中添加更多具有值的元素后,数组的大小仍保持不变

linux - 如何在 VIM 中使用系统剪贴板

java同步多线程问题

c++ - if >= 条件更改全局数组中元素的值

c - 程序执行是否总是从 C 中的 main 开始?

python - Docker 容器 Linux 在使用 * 读取 Python 参数时的奇怪行为