C++ 并行与分布式编程 chapter5 任务间并发的同步(2)
如果線程只是讀取共享存儲器,那么允許多個線程進入臨界區。任意數量的線程可以擁有一個讀-寫鎖來進行讀操作,但是如果要寫存儲器,就只允許一個線程進行訪問。讀寫鎖常用于讀數據比寫數據多的應用中。POSIX定義pthread_rwlock_t為讀寫鎖。它與互斥鎖有相同的操作,只是有一個pthread_rwlock_rdlock讀封鎖操作()和pthread_rwlock_wrlock()寫封鎖操作。如果一個線程請求讀封鎖,只要沒有任何線程擁有寫封鎖,就可以授權。如果線程請求寫封鎖,那么只要沒有任何線程擁有讀封鎖或寫封鎖,就可以授權。讀寫鎖可以實現CREW并發讀互斥寫
pthread_t ThreadA,ThreadB,ThreadC,ThreadD;
pthread_rwlock_t RWLock;
void *producer1(void *X)
{
?? pthread_rwlock_wrlock(&RWLock);
?? //critical section
?? pthread_rwlock_unlock(&RWLock);
?? return(0);
}
void *producer2(void *X)
{
?? pthread_rwlock_wrlock(&RWLock);
?? //critical section
?? pthread_rwlock_unlock(&RWLock);
}
?void *consumer1(void *X)
{
?? pthread_rwlock_rdlock(&RWLock);
?? //critical section
?? pthread_rwlock_unlock(&RWLock);
?? return(0);
}
?
void *consumer2(void *X)
{
?? pthread_rwlock_rdlock(&RWLock);
?? //critical section
?? pthread_rwlock_unlock(&RWLock);
?? return(0);
}
?
int main(void)
{
?? pthread_rwlock_init(&RWLock,NULL);
?? //set mutex attributes
?? pthread_create(&ThreadA,NULL,producer1,NULL);
?? pthread_create(&ThreadB,NULL,consumer1,NULL);
?? pthread_create(&ThreadC,NULL,producer2,NULL);
?? pthread_create(&ThreadD,NULL,consumer2,NULL);
?? //...
?? return(0);
}
ThreadB and ThreadD can enter their critical sections concurrently or serially but neither thread can enter their critical sections if either ThreadA or ThreadC is in theirs. ThreadA and ThreadC cannot enter their critical sections concurrently條件變量Condition Variable用來發送事件發生信號的信號量。一旦事件發生,或多個進程或者線程就等待其他進程或線程發送信號。生產者線程發送信號通知消費者線程對象已經加入到隊列中。消費者線程在接收到信號之前處于等待狀態,接收到信號后就繼續處理隊列。
條件變量可以和互斥鎖一起使用:當一個任務試圖加鎖,如果互斥鎖已經加鎖了,這個任務就會被阻塞。如果使用一個條件變量,該條件變量必須與一個互斥鎖相關聯。一旦不被阻塞,任務就會釋放互斥鎖Mutex,同時等待條件變量EventMutex。發送信號操作在時間按發生時是任務向另一個線程或進程發送信號,如果一個任務在等待那個條件變量,那么該任務就不再被阻塞,并獲得互斥鎖。條件變量可以實現4種基本同步關系FF、FS、SS、SF
#pragma comment(lib, "pthreadVC2.lib")
#include <pthread.h>
#include <iostream>
using namespace std;
float Number;
pthread_t ThreadA,ThreadB;
pthread_mutex_t Mutex,EventMutex;
pthread_cond_t Event;
void *worker1(void *X)
{
???????? for(int Count = 1;Count < 100;Count++){
?????????????????? pthread_mutex_lock(&Mutex);
?????????????????? Number++;
?????????????????? pthread_mutex_unlock(&Mutex);
?????????????????? cout << "worker1: number is " << Number << endl;
?????????????????? if(Number == 50){
??????????????????????????? pthread_cond_signal(&Event);
?????????????????? }
???????? }
???????? cout << "worker 1 done" << endl;
???????? return(0);
}
?void *worker2(void *X)
{
???????? pthread_mutex_lock(&EventMutex);
???????? pthread_cond_wait(&Event,&EventMutex);
???????? pthread_mutex_unlock(&EventMutex);
???????? cout<<"work2 is started! Now, Number is:"<<Number<<endl;
???????? for(int Count = 1;Count < 50;Count++){
?????????????????? pthread_mutex_lock(&Mutex);
?????????????????? Number = Number + 20;
?????????????????? pthread_mutex_unlock(&Mutex);
?????????????????? cout << "worker2: number is " << Number << endl;
???????? }
???????? cout << "worker 2 done" << endl;
???????? return(0);
}
int main(int argc, char *argv[])
{
???????? pthread_mutex_init(&Mutex,NULL);
???????? pthread_mutex_init(&EventMutex,NULL);
???????? pthread_cond_init(&Event,NULL);
???????? cout<<"start thread A & B"<<endl;
???????? pthread_create(&ThreadA,NULL,worker1,NULL);
???????? pthread_create(&ThreadB,NULL,worker2,NULL);
???????? //...
???????? pthread_join(ThreadA,NULL);//wait for threads
???????? pthread_join(ThreadB,NULL);
???????? return(0);
}
實現了FS同步關系。在線程B開始之前線程A不能結束。一旦Number==50,ThreadA就發送信號給ThreadB,接著線程A繼續執行直到結束。在接收到A的信號后,B才開始計算。線程B使用EventMutex與條件變量Event.Mutex來同步共享數據Number的寫訪問。一個任務可以使用幾個互斥鎖來同步不同的臨界區以及不同的事件。
本文使用Blog_Backup未注冊版本導出,請到soft.pt42.com注冊。
轉載于:https://www.cnblogs.com/aquar/archive/2010/11/19/3451395.html
總結
以上是生活随笔為你收集整理的C++ 并行与分布式编程 chapter5 任务间并发的同步(2)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: head first html with
- 下一篇: GridView:根据单元格的值给单元格