[英]How Multi-threaded program works in C?
我是一個完整的新手,我一直在用固定緩沖區大小編寫生產者-消費者問題,我觀察到的緩沖區大小 1 與我的預期非常不同。 我在產生 x 時打印“x-->”,在消耗 x 時打印“-->x”。 我得到的output是這樣的:\
0 --->
1 --->
2 --->
---> 0
---> 1
---> 2
3 --->
4 --->
5 --->
---> 3
---> 4
---> 5
我很困惑 1,2,3 如何一次復制然后 1,2,3 一次消耗,有人可以解釋一下嗎?
這是我的代碼
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#define BUFFER_SIZE 1
#define OVER (-1)
int filled = 0;
struct prodcons {
int buffer[BUFFER_SIZE]; /* the actual data */
pthread_mutex_t lock; /* mutex ensuring exclusive access to buffer */
int readpos, writepos; /* positions for reading and writing */
pthread_cond_t notempty; /* signaled when buffer is not empty */
pthread_cond_t notfull; /* signaled when buffer is not full */
};
void init(struct prodcons * b)
{
pthread_mutex_init(&b->lock, NULL);
pthread_cond_init(&b->notempty, NULL);
pthread_cond_init(&b->notfull, NULL);
b->readpos = 0;
b->writepos = 0;
}
void put(struct prodcons * b, int data)
{
pthread_mutex_lock(&b->lock);
if(BUFFER_SIZE == 1) {
if(filled) {
pthread_cond_wait(&b->notfull, &b->lock);
}
b->buffer[0] = data;
filled = 1;
}
else {
/* Wait until buffer is not full */
while ((b->writepos + 1) % BUFFER_SIZE == b->readpos) {
pthread_cond_wait(&b->notfull, &b->lock);
/* pthread_cond_wait reacquired b->lock before returning */
}
/* Write the data and advance write pointer */
b->buffer[b->writepos] = data;
b->writepos++;
if (b->writepos >= BUFFER_SIZE) b->writepos = 0;
/* Signal that the buffer is now not empty */
}
pthread_cond_signal(&b->notempty);
pthread_mutex_unlock(&b->lock);
}
int get(struct prodcons * b)
{
int data = 0;
pthread_mutex_lock(&b->lock);
if(BUFFER_SIZE == 1) {
if(!filled) {
pthread_cond_wait(&b->notempty, &b->lock);
}
data = b->buffer[0];
filled = 0;
}
else {
/* Wait until buffer is not empty */
while (b->writepos == b->readpos) {
pthread_cond_wait(&b->notempty, &b->lock);
}
/* Read the data and advance read pointer */
data = b->buffer[b->readpos];
b->readpos++;
if (b->readpos >= BUFFER_SIZE) b->readpos = 0;
/* Signal that the buffer is now not full */
}
pthread_cond_signal(&b->notfull);
pthread_mutex_unlock(&b->lock);
return data;
}
struct prodcons buffer;
void * producer(void * data)
{
int n;
for (n = 0; n < 10000; n++) {
printf("%d --->\n", n);
put(&buffer, n);
}
put(&buffer, OVER);
return NULL;
}
void * consumer(void * data)
{
int d;
while (1) {
d = get(&buffer);
if (d == OVER) break;
printf("---> %d\n", d);
}
return NULL;
}
int main(void)
{
pthread_t th_a, th_b;
void * retval;
init(&buffer);
/* Create the threads */
pthread_create(&th_a, NULL, producer, 0);
pthread_create(&th_b, NULL, consumer, 0);
/* Wait until producer and consumer finish. */
pthread_join(th_a, &retval);
pthread_join(th_b, &retval);
return 0;
}
output 無法保證如您所願。 我實現了一個時間戳,它顯示了調用put
和get
方法的時間(時間與調用init
的時間相關):
[ 44491 ns] 0 --->
[115427 ns] 1 --->
[153189 ns] ---> 0
[178376 ns] 2 --->
[182891 ns] 3 --->
[183518 ns] ---> 1
[188542 ns] ---> 2
[198661 ns] 4 --->
[203461 ns] ---> 3
[206636 ns] ---> 4
[204180 ns] 5 --->
[212146 ns] 6 --->
[221019 ns] ---> 5
[224146 ns] ---> 6
[221877 ns] 7 --->
[230976 ns] 8 --->
[232130 ns] 9 --->
[232347 ns] ---> 7
[237920 ns] ---> 8
[239051 ns] ---> 9
[239312 ns] 10 --->
[244770 ns] 11 --->
[245918 ns] 12 --->
[246106 ns] ---> 10
...
你應該知道, stdout
是由兩個線程共享的,所以那里的鎖定是如何實現的,誰知道呢。
如果要同步 output,則必須將printf
放入同步部分(在put
和get
方法中訪問緩沖區之后)。
然后你得到 output:
[ 31900 ns] 0 --->
[ 77228 ns] ---> 0
[ 85722 ns] 1 --->
[ 90959 ns] ---> 1
[ 95490 ns] 2 --->
[ 99591 ns] ---> 2
[103447 ns] 3 --->
[107385 ns] ---> 3
[111325 ns] 4 --->
[115215 ns] ---> 4
[119143 ns] 5 --->
[123066 ns] ---> 5
[126962 ns] 6 --->
[130860 ns] ---> 6
[134561 ns] 7 --->
[138427 ns] ---> 7
[142188 ns] 8 --->
[146115 ns] ---> 8
[149797 ns] 9 --->
[153534 ns] ---> 9
[157856 ns] 10 --->
[161631 ns] ---> 10
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.