创建线程和结束线程
2010-07-26 00:18
239 查看
#include <stddef.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#define FALSE 0
#define TRUE 1
void readfun();
void writefun();
char buffer[256];
int buffer_has_item=0;
int retflag=FALSE,i=0;
pthread_mutex_t mutex;
int main()
{
void *retval;
pthread_t reader;
pthread_mutex_init(&mutex,NULL);
pthread_create(&reader,NULL,(void *)&readfun,NULL);
writefun();
pthread_join(reader,&retval);
}
void readfun()
{
while(1)
{
if(retflag)
return;
pthread_mutex_lock(&mutex);
if(buffer_has_item==1)
{
printf("%s",buffer);
buffer_has_item=0;
}
pthread_mutex_unlock(&mutex);
}
}
void writefun()
{
int i=0;
while(1)
{
if(i==10)
{
retflag=TRUE;
return;
}
pthread_mutex_lock(&mutex);
if(buffer_has_item==0)
{
sprintf(buffer,"This is %d/n",i++);
buffer_has_item=1;
}
pthread_mutex_unlock(&mutex);
}
}
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#define FALSE 0
#define TRUE 1
void readfun();
void writefun();
char buffer[256];
int buffer_has_item=0;
int retflag=FALSE,i=0;
pthread_mutex_t mutex;
int main()
{
void *retval;
pthread_t reader;
pthread_mutex_init(&mutex,NULL);
pthread_create(&reader,NULL,(void *)&readfun,NULL);
writefun();
pthread_join(reader,&retval);
}
void readfun()
{
while(1)
{
if(retflag)
return;
pthread_mutex_lock(&mutex);
if(buffer_has_item==1)
{
printf("%s",buffer);
buffer_has_item=0;
}
pthread_mutex_unlock(&mutex);
}
}
void writefun()
{
int i=0;
while(1)
{
if(i==10)
{
retflag=TRUE;
return;
}
pthread_mutex_lock(&mutex);
if(buffer_has_item==0)
{
sprintf(buffer,"This is %d/n",i++);
buffer_has_item=1;
}
pthread_mutex_unlock(&mutex);
}
}
相关文章推荐
- win32多线程 (一) 线程创建与结束等待
- 初识pthread(一)-线程管理之创建/结束线程
- vc 线程的创建,暂停和结束
- vc 线程的创建、暂停和结束
- 在你的服务器端代码中使用线程和创建异步处理(结束)
- vc 线程的创建,暂停和结束
- 在你的服务器端代码中使用线程和创建异步处理(结束)
- 创建线程,互斥线程,非互斥线程及线程的结束
- 一起talk C栗子吧(第一百零八回:C语言实例--线程创建与结束一)
- linux c 线程的创建与结束 (传参和返回值)
- Linux 线程的创建与结束
- CompletionService 创建多线程,并返回全部线程处理结束后的结果
- iOS 如何创建一个线程,要求可以一直工作,不会执行一次就结束
- 线程的创建和结束的方法
- 线程创建和结束理解
- WIN32多线程一 用WIN32 API创建和结束线程(转)
- 线程初步(1)—— 线程的创建、参数和返回值、结束、状态、取消
- AfxBeginThread创建和事件对象结束线程的方法
- 线程的基本操作,创建线程,结束线程,新线程跟原线程共享变量
- Windows核心编程学习笔记(9)--线程创建与结束线程