#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
//通过互斥锁和条件变量的方式实现一个典型的生产消费问题求解
const int NUMBER = 20;
const int SECCESS = 0;
const int END = -1;
const int EXIT = 1;
typedef struct
{
int BufferDate[NUMBER];
pthread_mutex_t MyMutex;
pthread_cond_t WriteCondWait; //写未就绪状态
pthread_cond_t ReadCondWait; //读未就绪状态
int WritePos,ReadPos;
}MyBufferTag;
MyBufferTag MyBuffer;
void Init(MyBufferTag * pMyBuffer)
{
pthread_mutex_init(&(pMyBuffer->MyMutex),NULL);
pthread_cond_init(&(pMyBuffer->ReadCondWait),NULL);
pthread_cond_init(&(pMyBuffer->WriteCondWait),NULL);
pMyBuffer->WritePos = 0;
pMyBuffer->ReadPos = 0;
}
void Puts(MyBufferTag* pMyBuffer,int data)
{
//printf("==============now is WriteThread\n");
pthread_mutex_lock(&(pMyBuffer->MyMutex));
//写指针追上读指针 这时候写的线程需要被阻塞
if((pMyBuffer->WritePos + 1) % NUMBER == pMyBuffer->ReadPos)
{
pthread_cond_wait(&(pMyBuffer->WriteCondWait),&(pMyBuffer->MyMutex));
}
//写缓冲数据
pMyBuffer->BufferDate[pMyBuffer->WritePos] = data;
pMyBuffer->WritePos ++;
if(pMyBuffer->WritePos == NUMBER)
pMyBuffer->WritePos = 0;
//设置可读状态
pthread_cond_signal(&(pMyBuffer->ReadCondWait));
pthread_mutex_unlock(&(pMyBuffer->MyMutex));
}
void Reads(MyBufferTag* pMyBuffer,int * data)
{
//printf("==============now is Read Thread\n");
pthread_mutex_lock(&(pMyBuffer->MyMutex));
//读指针追上写指针时候 读数据的线程需要被阻塞
if(pMyBuffer->ReadPos == pMyBuffer->WritePos)
{
pthread_cond_wait(&(pMyBuffer->ReadCondWait),&(pMyBuffer->MyMutex));
}
//读缓冲数据
*data = pMyBuffer->BufferDate[pMyBuffer->ReadPos];
pMyBuffer->ReadPos ++ ;
if(pMyBuffer->ReadPos == NUMBER)
pMyBuffer->ReadPos = 0;
//设置可写状态
pthread_cond_signal(&(pMyBuffer->WriteCondWait));
pthread_mutex_unlock(&(pMyBuffer->MyMutex));
}
void * ProducerFunction(void *arg)
{
// sleep(5);
for(int i = 0;i< 1000;i++)
{
//pthread_mutex_lock(&(MyBuffer.MyMutex));
Puts(&MyBuffer,i);
printf("input is :%d\n",i);
}
Puts(&MyBuffer,END);
return NULL;
}
void *ConsumeFunction(void *arg)
{
int OutNumber = -2;
while(1)
{
Reads(&MyBuffer,&OutNumber);
printf("Output is : %d\n",OutNumber);
if(OutNumber == END)
break;
}
return NULL;
}
int main()
{
Init(&MyBuffer);
pthread_t ProducerThread; //生产者线程
pthread_t ConsumeThread; //消费者线程
int res = pthread_create(&ProducerThread,NULL,ProducerFunction,NULL);
if(res != SECCESS)
{
printf("create ProducerThread faild!");
exit(EXIT);
}
res = pthread_create(&ConsumeThread,NULL,ConsumeFunction,NULL);
if(res != SECCESS)
{
printf("create ConsumeThread faild!");
exit(EXIT);
}
void * Return;
pthread_join(ProducerThread,&Return);
pthread_join(ConsumeThread,&Return);
return true;
}
//线程的创建和退出
/*
const int EXIT = 1;
const int NUMMAX= 50;
char Sample[NUMMAX] ="Hello World";
void *MyChildThreadFunction(void *arg)
{
printf("The process is enter into MyChildThreadFunction!The ThreadID is %d\n",pthread_self());
printf("the inparameter is %s\n",(char*)arg);
sleep(3);
strcpy(Sample,"hello yuansong");
char * BackString = "MyChileThread Return message!";
pthread_exit((void *)BackString);
}
int main(int argc, char *argv[])
{
int MainThredID = pthread_self();
printf("the MainThredID is:%d\n",MainThredID);
pthread_t MyChildThread;
int ChildThreadID = pthread_create(&MyChildThread,NULL,MyChildThreadFunction,(void *)Sample);
if(ChildThreadID != 0)
{
printf("create Thread faild!,the error code is:%d\n",ChildThreadID);
exit(EXIT);
}
printf("wait for MychildThread exit....\n");
void *CharReturn;
int result = pthread_join(MyChildThread,&CharReturn);
if(result != 0)
{
printf("Thread joined faild,the error code is:%s\n",result);
exit(EXIT);
}
printf("the ChildThread returns :%s\n",(char *)CharReturn);
printf("now the Sample is :%s\n",Sample);
return 0;
}*/
//线程的互斥访问
/*
const int num = 100;
const int SUCCESS = 0;
char Message[num] ;
pthread_mutex_t MyMutex;
void * ChildThreadFunction(void *arg)
{
//sleep(2);
while(1)
{
pthread_mutex_lock(&MyMutex);
printf("you input is :%s\n",Message);
pthread_mutex_unlock(&MyMutex);
sleep(1);
}
}
int main()
{
pthread_t MyChildThread;
int res = pthread_create(&MyChildThread,NULL,ChildThreadFunction,NULL);
if(res != SUCCESS)
{
printf("create failed!");
exit(1);
}
res = pthread_mutex_init(&MyMutex,NULL);
if(res != SUCCESS)
{
printf("create failed!");
exit(1);
}
printf("input sth ,enter 'end' to finish\n");
while(1)
{
pthread_mutex_lock(&MyMutex);
// cin >> Message;
scanf("%s",Message);
pthread_mutex_unlock(&MyMutex);
if(strncmp("end",Message,3)==0)
{break;}
sleep(1);
}
pthread_cancel(MyChildThread);
pthread_mutex_destroy(&MyMutex);
//exit(1);
return true;
}*/
///学习unix环境高级编程线程一章编写程序
#include <iostream>
#include <pthread.h>
//测试线程的常规退出 return
void *fun1(void *)
{
printf("this is thread 1:%ld\n",pthread_self());
return (void *)1;
// exit(0); //若子线程调用exit,则整个进程都退出
}
//测试线程的thread_exit退出
void *fun2(void *)
{
printf("this is thread 2:%ld\n",pthread_self());
pthread_exit((void*)2);
}
int main(int argc,char *argv[])
{
pthread_t tid1,tid2;
int ret;
void *pRetVal;
ret = pthread_create(&tid1,NULL,fun1,NULL);
if(ret != 0) {
printf("create thread1 faild\n");
exit(0);
}
pthread_join(tid1,&pRetVal); //注意第二个参数传的是指向指针的指针,也就是线程的返回值会修改这个指针变量的值
std::cout << "thread1 is end,the ret code is:" << (long)pRetVal<<std::endl;
ret = pthread_create(&tid2,NULL,fun2,NULL);
if(ret != 0){
std::cout << "create thread2 failed" << std::endl;
exit(0);
}
pthread_join(tid2,&pRetVal);
std::cout << "thread2 is end,the ret code is:" << (long)pRetVal<<std::endl;
return 0;
}