ProDeveloperTutorial.com

Tutorials and Programming Solutions
Menu
  • Shell Scripting
  • System Design
  • Linux System Programming
  • 4g LTE
  • Coding questions
  • C
  • C++
  • DSA
  • GIT
  • 450 DSA Cracker
  • 5G NR
  • O-RAN

Linux System Programming: Threads synchronization using condition variable functions

prodevelopertutorial May 22, 2020

In this chapter we shall learn about:

1. API’s used in pthread condition variable

2. Example 1

Below are the API’s used for pthread condition variables

1. To initialize a condition:

int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr):

Here,

cond: It is the condition variable
cond_attr: They are the attributes to be applied for “cond”. Use NULL for default attributes.

 

2. To wait for condition:

int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)

It will put current thread to sleep, waiting for “cond” for the mutex to be released.

 

3. API to wake up one thread

int pthread_cond_signal(pthread_cond_t *cond)

 

4. API to wakeup all the threads:

int pthread_cond_broadcast(pthread_cond_t *cond)

 

Note:
1. As we know that, the current thread will wait if the condition is not met and proceed once the condition is met.
2. There might be multiple threads waiting for a condition.
3. Hence there will be chances of race condition.
4. Hence it is always important to use mutex lock along with condition variables.

Now let us understand thread synchronization using condition variables.

Example:

In the below example, thread_1 will call the fun_1. Then it will wait for the condition to be met.

When the thread_2 is created, it will call fun_2 and set the value to 100 and then signal the waiting thread.

Then the thread_1 waiting thread will wake up and start its execution.

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <pthread.h>

//for more tutorials on C, C++, STL, DS, Linux visit www.ProDeveloperTutorial.com
  
pthread_mutex_t mutex_id;  
pthread_cond_t  cond_id;  

int number = 0;  

void* fun_1(void *nothing)  
{  
    pthread_mutex_lock(&mutex_id); 

    // wait till the "number is >= 0"
    pthread_cond_wait(&cond_id,&mutex_id);  
    
    printf("In fun_1\n");  
    pthread_mutex_unlock(&mutex_id);  
  
}  
  
void *fun_2(void *nothing)  
{  
      
    pthread_mutex_lock(&mutex_id); 

    //assign the value 100 to number 
    number = 100;  
    if(number >= 100)
    { 
        pthread_cond_signal(&cond_id); 
        printf("cond send singal\n"); 
    } 
    sleep(3);  
    pthread_mutex_unlock(&mutex_id);  
  
}  
  
int main(int argc,char *argv[])  
{  
    pthread_mutex_init(&mutex_id,NULL);  
    pthread_cond_init(&cond_id,NULL); 

    pthread_t pthread1_id;
    pthread_t pthread2_id;  

    pthread_create(&pthread1_id,NULL,fun_1,NULL);  
    sleep(1);  

    pthread_create(&pthread2_id,NULL,fun_2,NULL);  
    sleep(5);  
    return 0;  
}

 

Output:

cond send singal
In fun_1

List Of Tutorials available in this website:

C Programming 20+ ChaptersC++ Programming 80+ Chapters
100+ Solved Coding QuestionsData Structures and Algorithms 85+ Chapters
System design 20+ ChaptersShell Scripting 12 Chapters
4g LTE 60+ ChaptersMost Frequently asked Coding questions
5G NR 50+ ChaptersLinux System Programming 20+ chapters
Share
Email
Tweet
Linkedin
Reddit
Stumble
Pinterest
Prev Article
Next Article

About The Author

prodevelopertutorial

Follow this blog to learn more about C, C++, Linux, Competitive Programming concepts, Data Structures.

ProDeveloperTutorial.com

Tutorials and Programming Solutions
Copyright © 2022 ProDeveloperTutorial.com