三種關系:互斥,同步,互斥和同步
兩類角色:生產者,消費者(線程)
一個交易場所:生產者消費者共享的區域
賣蘋果的模型
- dish上面只有一個蘋果
- 買家必須要等賣家把蘋果放到dish上才可以去買蘋果。
- 賣家必須等待買家把蘋果買走才可以生產蘋果
- pthread_mutex_lock(&mutex); 和pthread_mutex_unlock(&mutex); 成對出現,里面的操作為一個原子操作
- pthread_cond_wait(&empty,&mutex);內部有一個加鎖解鎖操作
- 過程詳解
- 蘋果為0
- 初始化時蘋果為0,買家在pthread_cond_wait(&empty,&mutex); 開始等待,賣家不進入while循環,生產蘋果后發出pthread_cond_signal(&empty);讓正在因dish上蘋果為0的買家停止等待pthread_cond_wait(&empty,&mutex);
- 蘋果為1
- 當一個賣家進入pthread_mutex_lock(&mutex); 但是發現蘋果為1時,就在while循環處等待pthread_cond_wait(&empty,&mutex);,直到買家發出信號 pthread_cond_signal(&full);賣家停止等待,此時蘋果為0,跳出while循環,開始生產蘋果。生產蘋果后發出pthread_cond_signal(&empty);讓正在因dish上蘋果為0的買家停止等待pthread_cond_wait(&empty,&mutex); 這樣這個過程就結束了
*
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>int dish = 0;
pthread_cond_t full;
pthread_cond_t empty;
pthread_mutex_t mutex;void * consumer(void * arg)
{int id = (int)arg;while(1){pthread_mutex_lock(&mutex);//上鎖while(dish == 0){//買家發現沒有蘋果開始等待pthread_cond_wait(&empty,&mutex);}usleep(100000);dish = 0;printf("consumer %d get an apple!!\n", id);//給賣家發送信號pthread_cond_signal(&full);pthread_mutex_unlock(&mutex);//解鎖}return NULL;
}void * product(void *arg)
{int id = (int)arg;while(1){pthread_mutex_lock(&mutex); while(dish == 1){pthread_cond_wait(&full,&mutex);}dish =1;printf("producer %d put an apple!!\n", id); pthread_cond_signal(&empty);pthread_mutex_unlock(&mutex);}return NULL;
}int main()
{pthread_t tid;int set ;//初始化pthread_cond_init(&full,NULL);pthread_cond_init(&empty,NULL);pthread_mutex_init(&mutex,NULL);//四個賣家生產蘋果for(int i = 0;i<4;i++){pthread_create(&tid,NULL,product,(void *)i);}//四個買家買蘋果for(int i = 0;i<4;i++){pthread_create(&tid,NULL,consumer,(void *)i);}//等待thread標識的線程終止,防止僵尸進程產生pthread_join(tid,NULL);//銷毀所有資源pthread_cond_destroy(&full);pthread_cond_destroy(&empty);pthread_mutex_destroy(&mutex);
}
如果對于互斥量含義尚不清楚,建議閱讀Linux/UNIX系統編程手冊
pthread_mutex_t
pthread_mutex_init
pthread_mutex_lock
pthread_mutex_unlock
pthread_mutex_destroy
模型采取Linux/UNIX系統編程手冊
這個模型很好,也把概念講清楚了,加了中文注釋
生產者
s = pthread_mutex_lock(&mtx);
if (s != 0)errExitEN(s, "pthread_mutex_lock");avail++; /*這是一個原子操作 */s = pthread_mutex_unlock(&mtx);
if (s != 0)errExitEN(s, "pthread_mutex_unlock");s = pthread_cond_signal(&cond); /* 喚醒消費者 */
if (s != 0)errExitEN(s, "pthread_cond_signal");
消費者
s = pthread_mutex_lock(&mtx);
if (s != 0)errExitEN(s, "pthread_mutex_lock");while (avail == 0){ s = pthread_cond_wait(&cond, &mtx);if (s != 0)errExitEN(s, "pthread_cond_wait");
}
完整代碼
#include <time.h>
#include <pthread.h>
#include "tlpi_hdr.h"static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;static int avail = 0;static void *
threadFunc(void *arg)
{int cnt = atoi((char *) arg);int s, j;for (j = 0; j < cnt; j++) {sleep(1);/* Code to produce a unit omitted */s = pthread_mutex_lock(&mtx);if (s != 0)errExitEN(s, "pthread_mutex_lock");avail++; /* Let consumer know another unit is available */s = pthread_mutex_unlock(&mtx);if (s != 0)errExitEN(s, "pthread_mutex_unlock");s = pthread_cond_signal(&cond); /* Wake sleeping consumer */if (s != 0)errExitEN(s, "pthread_cond_signal");}return NULL;
}int
main(int argc, char *argv[])
{pthread_t tid;int s, j;int totRequired; /* Total number of units that all threadswill produce */int numConsumed; /* Total units so far consumed */Boolean done;time_t t;t = time(NULL);/* Create all threads */totRequired = 0;for (j = 1; j < argc; j++) {totRequired += atoi(argv[j]);s = pthread_create(&tid, NULL, threadFunc, argv[j]);if (s != 0)errExitEN(s, "pthread_create");}/* Loop to consume available units */numConsumed = 0;done = FALSE;for (;;) {s = pthread_mutex_lock(&mtx);if (s != 0)errExitEN(s, "pthread_mutex_lock");while (avail == 0) { /* Wait for something to consume */s = pthread_cond_wait(&cond, &mtx);if (s != 0)errExitEN(s, "pthread_cond_wait");}/* At this point, 'mtx' is locked... */while (avail > 0) { /* Consume all available units *//* Do something with produced unit */numConsumed ++;avail--;printf("T=%ld: numConsumed=%d\n", (long) (time(NULL) - t),numConsumed);done = numConsumed >= totRequired;}s = pthread_mutex_unlock(&mtx);if (s != 0)errExitEN(s, "pthread_mutex_unlock");if (done)break;/* Perhaps do other work here that does not require mutex lock */}exit(EXIT_SUCCESS);
}