pthread_cond_wait to use carding and pthread_cond_signal

These two functions are multi-threaded operation is very important, and it is relatively difficult to understand. Here comb.

The first is the introduction function,

int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)

There are two parameters, cond and mutex. cond is the condition, this value can be used to make a macro statement:

pthread_cond_t  cond = PTHREAD_COND_INITIALIZER;

Sound may be used to function as: 

int pthread_cond_init(pthread_cond_t *cond, pthread_cond_attr *cattr); 
int pthread_cond_destroy(pthread_cond_t *cond);

The first function is the init, the second to destroy. The function is the condition variable cond. It should be said that the first is a function of the property declarations cattr, NULL is generally used with the default attribute, which when the function returns, the memory pointed writes cond.

Another protagonist is:

int pthread_cond_signal(pthread_cond_t *cond)

Both general with the use, pthread_cond_wait the current thread is blocked waiting for, but after calling pthread_cond_signal, will be activated again. Of course, there are other functions with the use, see the following example.

#include<pthread.h>
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;//init mutex
pthread_cond_t  cond = PTHREAD_COND_INITIALIZER;//init cond

void *thread1(void*);
void *thread2(void*);

int i = 1; //global

int main(void){
    pthread_t p1;
    pthread_t p2;//two thread

    pthread_create(&p2,NULL,thread2,(void*)NULL);
    pthread_create(&p1,NULL,thread1,(void*)NULL);//Create thread
    
    pthread_join(p2,NULL);//wait a_b thread end
    pthread_mutex_destroy(&mutex);
    pthread_cond_destroy(&cond);
   exit(0);
}

void *thread1(void *parameter){
    for(i = 1;i<= 9; i++){
        pthread_mutex_lock(&mutex); //互斥锁
        printf("\n\ncall thread1 \n");
        if(i%3 == 0)
        {
            pthread_cond_signal(&cond); //send sianal to p2
            printf("p1:%d step1\n", i);
       	}
        else
            printf("p1:%d step2\n",i);
        pthread_mutex_unlock(&mutex);

		printf("p1: sleep i=%d  step3\n", i);
        sleep(1);
		printf("p1: sleep i=%d  step4\n", i);
    }
}

void *thread2(void*parameter){
    while(i < 9)
    {
        pthread_mutex_lock(&mutex);
        printf("\n\ncall thread2 \n");
        if(i%3 != 0)
            pthread_cond_wait(&cond,&mutex); //wait signal from p1
        
		printf("p2: %d step1\n",i);
        pthread_mutex_unlock(&mutex);

		printf("p2: sleep i=%d step2\n", i);
        sleep(1);
		printf("p2: sleep i=%d step3\n", i);		
    }
}  

Its operating results for the

call thread1 
p1:1 step2
p1: sleep i=1  step3


call thread2 
p1: sleep i=1  step4


call thread1 
p1:2 step2
p1: sleep i=2  step3
p1: sleep i=2  step4


call thread1 
p1:3 step1
p1: sleep i=3  step3
p2: 3 step1
p2: sleep i=3 step2
p1: sleep i=3  step4


call thread1 
p1:4 step2
p1: sleep i=4  step3
p2: sleep i=4 step3


call thread2 
p1: sleep i=4  step4


call thread1 
p1:5 step2
p1: sleep i=5  step3
p1: sleep i=5  step4


call thread1 
p1:6 step1
p1: sleep i=6  step3
p2: 6 step1
p2: sleep i=6 step2
p1: sleep i=6  step4


call thread1 
p1:7 step2
p1: sleep i=7  step3
p2: sleep i=7 step3


call thread2 
p1: sleep i=7  step4


call thread1 
p1:8 step2
p1: sleep i=8  step3
p1: sleep i=8  step4


call thread1 
p1:9 step1
p1: sleep i=9  step3
p2: 9 step1
p2: sleep i=9 step2
p1: sleep i=9  step4
p2: sleep i=10 step3

Next, a little explanation.

The first stage, call thread1 => p1: 1 step2 => p1: sleep i = 1 step3

First, because of the high priority p1, so the first call thread1, then i% 3 = 0, so run p1:! 1 step2, the next step is p1: sleep i = 1 step3

It should be noted that, due before the third sentence, has pthread_mutex_unlock, lifted the mutex, so p2 can access the shared resources of the.

In sleep 1 seconds, the second phase took place.

The second stage: call thread2 => p1: sleep i = 1 sep4

Here because i% 3! = 0, so the implementation of the pthread_cond_wait, entered the waiting period, p2 does not run on the first follow-up.

It should be noted that, although p2 also was locked, but pthread_cond_wait played its role, the thread lock solved.

This is one of his role pthread_cond_wait, the first will lift the current thread's mutex, then hung threads waiting on the condition variable condition is met. Once the condition variable condition is met, the thread will be locked continue pthread_cond_wait.

The third stage: call thread1 => p1: 3 step1 .......

Back pthread1, only time i have been to 3 cycles, so the implementation of the pthread_cond_signal, but also meets the condition variable, so p2 is activated again.

Thus, a printed p2: 3 step1 => p2: sleep i = 3 step2. Since then p2 has lifted the lock, in sleep, the program then switched to the p1 thread.

Follow-up process is the case.

The final stage:

call thread1 
p1:9 step1
p1: sleep i=9  step3
p2: 9 step1
p2: sleep i=9 step2
p1: sleep i=9  step4
p2: sleep i=10 step3
 

Because i have to 9, it will not be recycled, so pthread1 last sentence is p1: sleep i = 9 step4.

However, i have to plus 1, so the time passed p2, the last sentence has become p2: sleep i = 10 step3.

 

Carding process again, the feeling of understanding between threads more clearly some.

 

 

 

 

Published 13 original articles · won praise 0 · Views 367

Guess you like

Origin blog.csdn.net/tjw316248269/article/details/104657490