本文目录一览:
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