本文目錄一覽:
c語言如何編寫一個簡單的多線程程序?
這是一個多線程例子,裡面只有兩個線程,是生產者/消費者模式,已編譯通過,注釋很詳細,
如下:
/* 以生產者和消費者模型問題來闡述Linux線程的控制和通信你
生產者線程將生產的產品送入緩衝區,消費者線程則從中取出產品。
緩衝區有N個,是一個環形的緩衝池。
*/
#include stdio.h
#include pthread.h
#define BUFFER_SIZE 16
struct prodcons
{
int buffer[BUFFER_SIZE];/*實際存放數據的數組*/
pthread_mutex_t lock;/*互斥體lock,用於對緩衝區的互斥操作*/
int readpos,writepos; /*讀寫指針*/
pthread_cond_t notempty;/*緩衝區非空的條件變量*/
pthread_cond_t notfull;/*緩衝區未滿 的條件變量*/
};
/*初始化緩衝區*/
void pthread_init( struct prodcons *p)
{
pthread_mutex_init(p-lock,NULL);
pthread_cond_init(p-notempty,NULL);
pthread_cond_init(p-notfull,NULL);
p-readpos = 0;
p-writepos = 0;
}
/*將產品放入緩衝區,這裡是存入一個整數*/
void put(struct prodcons *p,int data)
{
pthread_mutex_lock(p-lock);
/*等待緩衝區未滿*/
if((p-writepos +1)%BUFFER_SIZE ==p-readpos)
{
pthread_cond_wait(p-notfull,p-lock);
}
p-buffer[p-writepos] =data;
p-writepos++;
if(p-writepos = BUFFER_SIZE)
p-writepos = 0;
pthread_cond_signal(p-notempty);
pthread_mutex_unlock(p-lock);
}
/*從緩衝區取出整數*/
int get(struct prodcons *p)
{
int data;
pthread_mutex_lock(p-lock);
/*等待緩衝區非空*/
if(p-writepos == p-readpos)
{
pthread_cond_wait(p-notempty ,p-lock);//非空就設置條件變量notempty
}
/*讀書據,移動讀指針*/
data = p-buffer[p-readpos];
p-readpos++;
if(p-readpos == BUFFER_SIZE)
p-readpos = 0;
/*設置緩衝區未滿的條件變量*/
pthread_cond_signal(p-notfull);
pthread_mutex_unlock(p-lock);
return data;
}
/*測試:生產站線程將1 到1000的整數送入緩衝區,消費者線程從緩衝區中獲取整數,兩者都打印信息*/
#define OVER (-1)
struct prodcons buffer;
void *producer(void *data)
{
int n;
for( n=0;n1000;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;
else
printf(“—–%d\n”,d);
}
return NULL;
}
int main()
{
pthread_t th_p,th_c;
void *retval;
pthread_init(buffer);
pthread_create(th_p,NULL,producer,0);
pthread_create(th_c,NULL,consumer,0);
/*等待兩個線程結束*/
pthread_join(th_p, retval);
pthread_join(th_c,retval);
return 0;
}
c語言中多線程讀寫同一個環形緩衝區的實現
#include stdio.h
#include windows.h
#include process.h
unsigned __stdcall ThreadWrite(void* param);
unsigned __stdcall ThreadRead(void* param);
int WriteSeque = 0;
int ReadSeque = 0;
int RingBuf[4];
void main()
{
HANDLE htw = (HANDLE)_beginthreadex(NULL, 0, ThreadWrite, NULL, 0, 0);
HANDLE htr = (HANDLE)_beginthreadex(NULL, 0, ThreadRead, NULL, 0, 0);
CloseHandle(htw);
CloseHandle(htr);
while (1)
{
if (WriteSeque = 100)
{
break;
}
}
printf(“Quit\n”);
}
unsigned __stdcall ThreadWrite(void* param)
{
int i = 0;
while (1)
{
if (WriteSeque = ReadSeque WriteSeque- ReadSeque 4)
{
RingBuf[WriteSeque%4] = i;
printf(“Write:%d\n”, i);
i++;
WriteSeque++;
Sleep(50);
}
}
}
unsigned __stdcall ThreadRead(void* param)
{
while (1)
{
if (ReadSeque WriteSeque)
{
printf(“Read:%d\n”, RingBuf[ReadSeque%4]);
ReadSeque++;
Sleep(100);
}
}
}
為了讓你看到效果,讀寫線程的休眠時間略有不同。
怎樣用c語言實現一個環形緩存區!
定義個數組如a[10];
用兩個head tail 指針
存入數據後tail++ 讀取數據後head++
為了循環利用此塊空間 做以下處理:
存跟讀數據時指針處理 tail%10 head%10
判斷緩存空?tail == head+1
判斷緩存滿?tail == head+9
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/306087.html