pthread_cond_waitのカーディングとpthread_cond_signalを使用します

これらの2つの機能は、マルチスレッド操作は非常に重要であり、それを理解することは比較的困難です。櫛をここに。

まず、導入機能です

int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)

二つのパラメータは、condとミューテックスがあります。condが条件である、この値は、マクロ文を作るために使用することができます。

pthread_cond_t COND = PTHREAD_COND_INITIALIZER。

サウンドとして機能するために使用することができます。 

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

最初の機能は、init、破壊するための第二です。関数は、条件変数condにあります。プロパティ宣言がcattrの関数である第一、NULLが、一般的に、関数が戻ると、メモリはCOND書き込みを指摘し、デフォルトの属性とともに使用されていることを指摘しておかなければ。

もう一つの主人公は、次のとおりです。

int pthread_cond_signal(pthread_cond_t *cond)

使用した一般の両方は、pthread_cond_waitの現在のスレッドを待ってブロックされているが、pthread_cond_signalを呼び出した後、再び有効になります。もちろん、そこに使用して他の関数であり、以下の例を参照してください。

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

以下のためのその業績

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

次に、少し説明。

第一段階、呼び出しスレッド1 => P1:1 STEP2 => P1:睡眠、私は1つのSTEP3を=

最初の理由は、高優先度P1の、最初の呼び出しのスレッド1、それから%3 = 0、そう実行P1 :! 1つのSTEP2ので、次のステップは、P1である:睡眠iは1つのSTEP3を=

3番目の文の前に、pthread_mutex_unlockのを持っているため、P2は、共有リソースにアクセスできるように、ミューテックスを持ち上げることに留意すべきです。

睡眠1秒で、第二段階が行われました。

第二段階:呼び出しスレッド2 => P1:睡眠、私は1 SEP4を=

私%3!= 0は、pthread_cond_waitの実装のため、待機期間に入ったので、ここで、p2は最初のフォローアップでは動作しません。

それは、P2はまた、ロックされたが、ことに留意すべきであるが、pthread_cond_waitのがその役割を果たし、スレッドロックが解決しました。

これは、満たされた後、条件変数の状態で待機しているハングスレッドを現在のスレッドのミューテックスを持ち上げます最初の彼の役割のpthread_cond_waitのの一つです。条件変数の条件が満たされると、ロックされたスレッドがpthread_cond_waitの継続します。

第三段階:呼び出しスレッド1 => P1:3 STEP1 .......

戻るpthread1は、唯一の時間は、私はpthread_cond_signalを実装のため、3回に行ったことがあるが、p2が再び作動されるようにも、条件変数を満たしています。

したがって、印刷されたP2:3 STEP1 => P2:睡眠私は3 STEP2を=。その後、p2は睡眠中に、ロックを解除しているので、プログラムは、P1のスレッドに切り替えます。

フォローアッププロセスがケースです。

最終段階:

呼び出しスレッド1 
P1:9 STEP1
P1:睡眠I = 9 STEP3
P2:9 STEP1
P2:睡眠I = 9 STEP2
P1:睡眠I = 9 STEP4
P2:睡眠、私は10 STEP3を=
 

私は9に持っているので、それが再利用されないので、pthread1最後の文は、P1である:睡眠私は9 STEP4を=。

しかし、私は時間がP2を通過したので、最後の文は、P2となっている、プラス1にあります。睡眠は私が10 STEP3を=。

 

再度処理をカーディング、スレッド間のより明確にいくつかを理解感。

 

 

 

 

公開された13元の記事 ウォンの賞賛0 ビュー367

おすすめ

転載: blog.csdn.net/tjw316248269/article/details/104657490