Linux 条件变量的使用pthread_cond
2017-02-18 17:40
1426 查看
使用单个条件变量时,必须cond_wait首先达到wait状态 用sleep
/******************************************************
/*: Object detection algrithm running in Linux
/*: Update: Naive version(线程实现简单的生产者消费者模式)
/*: 1:Vehicle algrithm
/******************************************************/
#include <iostream>
#include <vector>
#include <queue>
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
#include "Network.h"
#include "VehicleAlgrithm.h"
//#include "global.h"
#define MAX_TASKS 10
pthread_mutex_t the_mutex;
pthread_cond_t condc, condp;
int iStatus = 0;
void* producer(void *ptr)
{
int iNum;
while(true)
{
std::cout<<"produce...."<<std::endl;
Server* ps = Server::GetInstance();
ps->ReceiveCommand();
pthread_mutex_lock(&the_mutex);
//while(iStatus !=0) pthread_cond_wait(&condp, &the_mutex);
iStatus++;
std::cout<<iStatus<<":task Added..."<<std::endl;
pthread_cond_signal(&condc);
pthread_mutex_unlock(&the_mutex);
}
pthread_exit(0);
}
void* consumer(void *ptr)
{
int iNum;
std::cout<<"TEST Process Task..."<<std::endl;
std::string VideoPath("");
//VehicleAlgrithm* vehicleAlg = VehicleAlgrithm::GetAlgInstance();
std::cout<<"Create a vehicleAlg instance."<<std::endl;
while(true)
{
std::cout<<"waitting the Task !"<<std::endl;
pthread_mutex_lock(&the_mutex);
while(iStatus == 0) pthread_cond_wait(&condc, &the_mutex);
//Get a task from the queue
//vehicleAlg->ProcessTask(VideoPath);
iStatus--;
std::cout<<iStatus<<":task reduce..."<<std::endl;
std::cout<<"Start Vehicle process video task ... :"<<VideoPath<<std::endl;
//Transfer the result.txt to client...
//pthread_cond_signal(&condp);
pthread_mutex_unlock(&the_mutex);
sleep(2);
std::cout<<"Video Task done !"<<std::endl;
std::cout<<"Transfer the result.txt to client..."<<std::endl;
}
pthread_exit(0);
}
int main(int argc, char *argv[])
{
io_service ios;
Server serv(ios);
//Server* pS = Server::GetInstance();
//std::cout<<"test read cmd !..."<<std::endl;
//pS->ReceiveCommand();
//std::cout<<"test read cmd done!"<<std::endl;
pthread_t pro, con;
pthread_mutex_init(&the_mutex, 0);
pthread_cond_init(&condc,0);
//pthread_cond_init(&condp,0);
pthread_create(&con, 0, consumer, 0);
sleep(2);
pthread_create(&pro, 0, producer, 0);
pthread_join(pro,0);
pthread_join(con,0);
pthread_cond_destroy(&condc);
pthread_cond_destroy(&condp);
pthread_mutex_destroy(&the_mutex);
return 0;
}
/******************************************************
/*: Object detection algrithm running in Linux
/*: Update: Naive version(线程实现简单的生产者消费者模式)
/*: 1:Vehicle algrithm
/******************************************************/
#include <iostream>
#include <vector>
#include <queue>
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
#include "Network.h"
#include "VehicleAlgrithm.h"
//#include "global.h"
#define MAX_TASKS 10
pthread_mutex_t the_mutex;
pthread_cond_t condc, condp;
int iStatus = 0;
void* producer(void *ptr)
{
int iNum;
while(true)
{
std::cout<<"produce...."<<std::endl;
Server* ps = Server::GetInstance();
ps->ReceiveCommand();
pthread_mutex_lock(&the_mutex);
//while(iStatus !=0) pthread_cond_wait(&condp, &the_mutex);
iStatus++;
std::cout<<iStatus<<":task Added..."<<std::endl;
pthread_cond_signal(&condc);
pthread_mutex_unlock(&the_mutex);
}
pthread_exit(0);
}
void* consumer(void *ptr)
{
int iNum;
std::cout<<"TEST Process Task..."<<std::endl;
std::string VideoPath("");
//VehicleAlgrithm* vehicleAlg = VehicleAlgrithm::GetAlgInstance();
std::cout<<"Create a vehicleAlg instance."<<std::endl;
while(true)
{
std::cout<<"waitting the Task !"<<std::endl;
pthread_mutex_lock(&the_mutex);
while(iStatus == 0) pthread_cond_wait(&condc, &the_mutex);
//Get a task from the queue
//vehicleAlg->ProcessTask(VideoPath);
iStatus--;
std::cout<<iStatus<<":task reduce..."<<std::endl;
std::cout<<"Start Vehicle process video task ... :"<<VideoPath<<std::endl;
//Transfer the result.txt to client...
//pthread_cond_signal(&condp);
pthread_mutex_unlock(&the_mutex);
sleep(2);
std::cout<<"Video Task done !"<<std::endl;
std::cout<<"Transfer the result.txt to client..."<<std::endl;
}
pthread_exit(0);
}
int main(int argc, char *argv[])
{
io_service ios;
Server serv(ios);
//Server* pS = Server::GetInstance();
//std::cout<<"test read cmd !..."<<std::endl;
//pS->ReceiveCommand();
//std::cout<<"test read cmd done!"<<std::endl;
pthread_t pro, con;
pthread_mutex_init(&the_mutex, 0);
pthread_cond_init(&condc,0);
//pthread_cond_init(&condp,0);
pthread_create(&con, 0, consumer, 0);
sleep(2);
pthread_create(&pro, 0, producer, 0);
pthread_join(pro,0);
pthread_join(con,0);
pthread_cond_destroy(&condc);
pthread_cond_destroy(&condp);
pthread_mutex_destroy(&the_mutex);
return 0;
}
相关文章推荐
- Linux中,pthread_cond_t, 条件变量的使用举例
- 一段使用 mutex 和 条件变量 pthread_cond_wait 的例子
- 转载_Linux多线程编程详细解析----条件变量 pthread_cond_t
- linux条件锁pthread_cond_t使用示例
- 互斥量、条件变量与pthread_cond_wait()函数的使用
- 互斥量、条件变量与pthread_cond_wait()函数的使用,详解
- 互斥量、条件变量与pthread_cond_wait()函数的使用
- 详解linux互斥锁 pthread_mutex和条件变量pthread_cond
- 互斥量、条件变量与pthread_cond_wait()函数的使用,详解(一)
- Linux多线程编程详细解析----条件变量 pthread_cond_t
- Linux 条件变量 pthread_cond_wait
- Linux 条件变量 pthread_cond_wait
- Linux 线程通信与pthread_cond_wait()的使用
- 互斥量、条件变量与pthread_cond_wait()函数的使用,详解
- 互斥量、条件变量与pthread_cond_wait()函数的使用,详解
- Linux 条件变量 pthread_cond_wait
- linux学习之十六---条件变量pthread_cond_wait
- Linux下c开发 之 线程通信与pthread_cond_wait()的使用
- Linux多线程编程详解----条件变量pthread_cond_t
- 条件变量pthread_cond_t使用示例