描述:
(1) 两个线程,一个生产者,一个消费者
(2)一个生产者线程将int类型的数入列,一个消费者线程将int类型的数出列
在win32环境下实现:参考网址:http://topic.csdn.net/t/20051109/19/4382819.html
#include <iostream>
#include <queue>
#include <Windows.h>
using namespace std;
#define SIZE_BUFFER 10
queue<int> g_queue;
HANDLE bufferFullSemaphore; //缓冲区满信号量
HANDLE bufferEmptySemaphore;//缓冲区空信号量
HANDLE mutex; //线程之间互斥
bool flag = true;
static int num = 0;
DWORD WINAPI producerFunc(LPVOID n);
DWORD WINAPI consumerFunc(LPVOID n);
int main()
{
HANDLE handle[2];
DWORD thrdId;
bufferFullSemaphore = CreateSemaphore(NULL, SIZE_BUFFER, SIZE_BUFFER, NULL);
bufferEmptySemaphore = CreateSemaphore(NULL, 0, SIZE_BUFFER, NULL);
mutex = CreateMutex(NULL, false, NULL);
handle[0] = CreateThread(NULL, 0, producerFunc, 0, 0, &thrdId);
handle[1] = CreateThread(NULL, 0, consumerFunc, 0, 0, &thrdId);
while (flag)
{
if (getchar())
{
flag = false;
}
}
int rc = WaitForMultipleObjects(2, handle, TRUE, INFINITE);
CloseHandle(handle[0]);
CloseHandle(handle[1]);
CloseHandle(bufferEmptySemaphore);
CloseHandle(bufferFullSemaphore);
CloseHandle(mutex);
return 0;
}
DWORD WINAPI producerFunc(LPVOID n)
{
while (flag)
{
WaitForSingleObject(bufferFullSemaphore, INFINITE);
WaitForSingleObject(mutex, INFINITE);
g_queue.push(num);
cout << "produce: " << num%10 << endl;
num++;
ReleaseMutex(mutex);
ReleaseSemaphore(bufferEmptySemaphore, 1, NULL);
Sleep(500);
}
return 0;
}
DWORD WINAPI consumerFunc(LPVOID n)
{
while (flag)
{
WaitForSingleObject(bufferEmptySemaphore, INFINITE);
WaitForSingleObject(mutex, INFINITE);
cout << "consumer: " << g_queue.front()%10 << endl;
g_queue.pop();
ReleaseMutex(mutex);
ReleaseSemaphore(bufferFullSemaphore, 1, NULL);
Sleep(1500);
}
return 0;
}
在linux环境下实现:
//编译命令: g++ thread_cons_prod.cpp -o thread -pthread
//编译命令: ./thread
#include <iostream>
#include <queue>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h> //sleep();
#include <stdlib.h> //exit();
#include <stdio.h> //getchar()
using namespace std;
#define SIZE_BUFFER 10
bool flag = true;
static int num = 0;
queue<int> g_queue;
pthread_mutex_t mutex;
sem_t bufferFullSemaphore;
sem_t bufferEmptySemaphore;
void *producerFunc(void *arg);
void *consumerFunc(void *arg);
int main()
{
int ret;
ret = pthread_mutex_init(&mutex, NULL); //互斥量,用于线程之间
if (ret) { cout << "mutex init faild!" << endl; exit(1);}
ret = sem_init(&bufferFullSemaphore, 0, SIZE_BUFFER); //缓冲区满信号量
if (ret) { cout << "bufferFullSemaphore init faild!" << endl; exit(1); }
ret = sem_init(&bufferEmptySemaphore, 0, 0);
if (ret) { cout << "bufferEmptySemaphore init faild!" << endl; exit(1); } //缓冲区空信号量
pthread_t thrIdProd, thrIdCons;
pthread_create(&thrIdProd, NULL, producerFunc, NULL);
pthread_create(&thrIdCons, NULL, consumerFunc, NULL);
while (flag)
{
if (getchar())
{
flag = false;
}
}
//回收资源
pthread_mutex_destroy(&mutex);
sem_destroy(&bufferFullSemaphore);
sem_destroy(&bufferEmptySemaphore);
//等待线程结束
pthread_join(thrIdProd, NULL);
pthread_join(thrIdCons, NULL);
return 0;
}
void *producerFunc(void *arg)
{
while (flag)
{
sem_wait(&bufferFullSemaphore);
pthread_mutex_lock(&mutex);
g_queue.push(num);
cout << "produce: " << num%10 << endl;
num++;
pthread_mutex_unlock(&mutex);
sem_post(&bufferEmptySemaphore);
sleep(1); //休眠1秒 注:和win32环境的Sleep()不一样,后者是以毫秒为单位
}
}
void *consumerFunc(void *arg)
{
while (flag)
{
sem_wait(&bufferEmptySemaphore);
pthread_mutex_lock(&mutex);
cout << "consumer: " << g_queue.front()%10 << endl;
g_queue.pop();
pthread_mutex_unlock(&mutex);
sem_post(&bufferFullSemaphore);
sleep(3); //休眠3秒
}
}