1 #include 2 #include 3//#include "stdafx.h"
4 #include //
_beginthread && _endthread 5//
#include "baseoperation.h"
6#define n 10
78 typedef int semaphore; /*
訊號量是一種特殊的整型變數 */9
10 semaphore mutex=1; /*
互斥訪問
*/11 semaphore empty=n; /*
記錄緩衝池中空的緩衝區數
*/12 semaphore full=0; /*
記錄緩衝池中滿的緩衝區數
*/13
14 semaphore buf[n]; /*
有n個緩衝區數的緩衝池buf[n],並實現迴圈緩衝佇列
*/15 semaphore in=0, out=0
;
1617
void p(semaphore *x) /*
p操作
*/18
2122
void v(semaphore *y) /*
v操作
*/23
2627
void produce_item(int *item_ptr)
28
3233
void enter_item(int
x)
34
3940
void remove_item(int *yy)
41
48void consume_item(int
y)
49
5253
void producer(void
);
54void consumer(void
);
55int
item;
56dword winapi threadproc1( lpvoid lpparam )
5769}70
} 71
/*生產者
*/72
73dword winapi threadproc2( lpvoid lpparam )
7486}87
} 88
89void producer(void
)
90
116}
117118
/*消費者
*/119
void consumer(void
)
120
133}
134135
/*呼叫生產者-消費者程序實現程序間同步
*/136
main()
137
1 #include 2 #include 3//#include "stdafx.h"
4 #include //
_beginthread && _endthread 5//
#include "baseoperation.h"
6#define n 10
78 typedef int semaphore; /*
訊號量是一種特殊的整型變數 */9
10 semaphore mutex=1; /*
互斥訪問
*/11 semaphore empty=n; /*
記錄緩衝池中空的緩衝區數
*/12 semaphore full=0; /*
記錄緩衝池中滿的緩衝區數
*/13
14 semaphore buf[n]; /*
有n個緩衝區數的緩衝池buf[n],並實現迴圈緩衝佇列
*/15 semaphore in=0, out=0
;
1617
void p(semaphore *x) /*
p操作
*/18
2122
void v(semaphore *y) /*
v操作
*/23
2627
void produce_item(int *item_ptr)
28
3233
void enter_item(int
x)
34
3940
void remove_item(int *yy)
41
48void consume_item(int
y)
49
5253
void producer(void
);
54void consumer(void
);
55int
item;
56dword winapi threadproc1( lpvoid lpparam )
5767
} 68
/*生產者
*/69
70dword winapi threadproc2( lpvoid lpparam )
7181
} 82
83void producer(void
)
84
110}
111112
/*消費者
*/113
void consumer(void
)
114
127}
128129
/*呼叫生產者-消費者程序實現程序間同步
*/130
main()
131
PV 執行緒同步和多執行緒問題
基於訊號量的執行緒同步問題,主要用到p和v操作 訊號量s是具有非負整數值的全域性變數,它只能由兩類特殊的操作來處理。這兩種操作分別稱為p和v p s 如果當s是非0的,那麼p將s 1,並且立即返回。如果開始s就是0,那麼就掛起這個執行緒等待。v s 將s加1,如果有執行緒阻塞在p操作等待s變為非0,...
windows多執行緒(九) PV原語分析同步問題
pv原語通過操作訊號量來處理程序間的同步與互斥的問題。其核心就是一段不可分割不可中斷的程式。訊號量的概念1965年由著名的荷蘭計算機科學家dijkstra提出,其基本思路是用一種新的變數型別 semaphore 來記錄當前可用資源的數量。有兩種實現方式 1 semaphore的取值必須大於或等於0。...
多執行緒 多執行緒原理
我們首先要知道什麼是多執行緒,說白了就是多個執行緒,執行緒是什麼呢,其實就是程序執行的途徑,那麼說道這裡我們又引入了乙個新的名字,就是程序,那麼我們來看看什麼是程序,其實我們自己也能看到,啟動電腦的任務管理器,我們就可以看到程序選項,裡面是我們電腦所有的程序,我們會發現有很多的程序.簡單地說就是程序...