LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-07-2018, 11:14 PM   #1
vcmohans
LQ Newbie
 
Registered: Mar 2018
Posts: 1

Rep: Reputation: Disabled
Threads and mutex with signal broadcast


Scenario 1 :

I am creating four threads.Each threads were assigned to different cores all four threads are waiting for signal with same conditional and mutex variable.While passing broadcast signal with same conditional variable all threads were getting triggered.

Code:
#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);
    }

Output:

Thread 1
Thread 4
Thread 2
Thread 3

Thread 1 is unblocked...
Thread 4 is unblocked...
Thread 2 is unblocked...
Thread 3 is unblocked...

Scenario 2:

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.

Output for scenario 2:

Thread 1
Thread 2
Thread 4
Thread 3

Thread 1 is unblocked...

Quote:
Here my question is what is the role of conditional and mutex variable in scenario 1 and scenario 2 ?
In scenario 2 why all threads are not getting triggered?

Last edited by vcmohans; 03-11-2018 at 11:56 PM.
 
Old 03-08-2018, 03:56 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 4,664
Blog Entries: 6

Rep: Reputation: 2524Reputation: 2524Reputation: 2524Reputation: 2524Reputation: 2524Reputation: 2524Reputation: 2524Reputation: 2524Reputation: 2524Reputation: 2524Reputation: 2524
Welcome to LQ!

Please place your code snippets inside [CODE]...[/CODE] tags for better readability. You may type those yourself or click the "#" button in the edit controls.

Quote:
Here my question is what is the role of conditional and mutex variable in scenario 1 and scenario 2 ?
In scenario 2 why all threads are not getting triggered?
If you wrote the code then you should tell us what the roles are, no?

We are happy to help, even with your homework, but please do not paste homework verbatim with little effort of your own, and expect others to provide the answers for you.

Last edited by astrogeek; 03-08-2018 at 03:56 PM. Reason: little change
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Why we cannot use global variable instead of mutex lock to synchronize threads shamjs Programming 8 09-30-2015 08:33 AM
A question about threads and mutex locks in C... trist007 Programming 13 01-01-2012 02:39 AM
[SOLVED] pthread threads and mutex naaman Programming 1 02-07-2011 02:50 AM
Locking mutex in a signal handler function lbdgwgt Programming 5 02-15-2010 09:58 PM
diff. between semaphores/mutex usage on threads skywalker27182 Programming 6 08-16-2004 11:34 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration