linux下生产者消费者_多线程演示程序


linux下生产者消费者_多线程演示程序
 
基本API介绍
线程
 
线程创建: 
int pthread_create(pthread *thread, pthread_attr_t *attr, void* (*start_routine)(*void), void* arg); 
 
第一个参数为指向线程 
第二个参数用来设置线程属性。 
第三个参数是线程运行函数的起始地址 
第四个参数是运行函数的参数。 其中pthread_attr_t*和void* arg可设置为NULL;
 int pthread_join(pthread_t thread, void **retval);
第一个参数为被等待的线程标识符,
第二个参数为一个用户自定义指针,用来存储被等待线程的返回值。
这个函数是一个线程阻塞的函数,调用它的函数将一直等待到线程结束为止,当函数返回时,被等待线程的资源被收回。如果执行成功,将返回0,如果失败则返回一个错误号。
  www.2cto.com  
互斥量
 
初始化:
函数: int pthread_mutex_init(pthread_mutex_t *restrict mutex,const pthread_mutexattr_t *restrict attr);
该函数以动态方式创建互斥锁的,参数attr指定了新建互斥锁的属性。如果参数attr为空,则使用默认的互斥锁属性,默认属性为快速互斥锁 。互斥锁的属性在创建锁的时候指定,在LinuxThreads实现中仅有一个锁类型属性,不同的锁类型在试图对一个已经被锁定的互斥锁加锁时表现不同。成功完成之后会返回零,其他任何返回值都表示出现了错误。
 
上锁:
int pthread_mutex_lock(pthread_mutex_t *mutex);
 返回值:在成功完成之后会返回零。其他任何返回值表示出现错误。如果出现以下任一情况,该函数将失败并返回对应的值。
 
 解锁:
int pthread_mutex_unlock(pthread_mutex_t *mutex);
释放互斥锁,与pthread_mutex_lock成对存在。
 
信号量  www.2cto.com  
信号量的数据类型为结构sem_t,它本质上是一个长整型的数。
函数sem_init()用来初始化一个信号量。
int sem_init (sem_t *sem, int pshared, unsigned int value);
对由sem指定的信号量进行初始化,设置好它的共享选项,并指定一个整数类型的初始值。
pshared参数控制着信号量的类型, pshared的值若为0,表示是进程内共享信号量;否则,进程间可共享该信号量。
 
sem_post( sem_t *sem )用来增加信号量的值。当有线程阻塞在这个信号量上时,调用这个函数可唤醒被阻塞的线程。
 
sem_wait( sem_t *sem ),表示等待信号量,若该信号量不可获取,则阻塞,解除阻塞后将sem的值减一,表明公共资源经使用后减少。
 
sem_destroy(sem_t *sem)用来释放信号量sem。
 
上代码:
#include <iostream> 
#include <vector>
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
 
using namespace std;
 
#define MAX_SIZE 100
 
vector<int> buffer; //共享数据缓冲区
pthread_mutex_t mutex;        //缓冲区访问互斥量
sem_t full; //缓冲区有有数据信号量
sem_t empty; //缓冲区空闲数据信号量
 
void* producer(void*)    //生产者
{
for (int i = 0; i < MAX_SIZE; i++)
{
sem_wait(&empty); //缓冲区若有空闲空间,放数据,否则阻塞
pthread_mutex_lock(&mutex);
buffer.push_back(i);
cout<<"producer: "<<i<<endl;
pthread_mutex_unlock(&mutex);
sem_post(&full);
sleep(0.02);
}
};  www.2cto.com  
 
void* consumer(void*)    //消费者
{
int nMsgCount = 0;
 
while(nMsgCount < MAX_SIZE) 
                {
sem_wait(&full); //缓冲区有数据,取数据,否则阻塞
 
                        pthread_mutex_lock(&mutex);
vector<int>::iterator iter = buffer.begin();
int value = *iter;
buffer.erase(iter);
cout<<"consumer: "<<value<<endl;
pthread_mutex_unlock(&mutex);
 
                        sem_post(&empty);
nMsgCount++;
sleep(0.05);
}
}
 
int main()
{
        //互斥量、信号量初始化
pthread_mutex_init(&mutex,NULL);
sem_init(&full, 0, 0);
sem_init(&empty, 0, MAX_SIZE);
        www.2cto.com  
        //创建线程
pthread_t producerTd;
pthread_t consumerTd;
int nProducerId = pthread_create(&producerTd, NULL, producer, NULL);
int nConusmerId = pthread_create(&consumerTd, NULL, consumer, NULL);
 
sleep(100);
 
        //相关资源销毁
sem_destroy(&full);
sem_destroy(&empty);
 
pthread_join(producerTd,NULL);
pthread_join(consumerTd,NULL);
return 0;
}
结果演示
 
 
这是第一次在VI下写linux的程序,和VS2005+VISUAL ASSIST相比,没有高亮,没有智能提示,用VI好痛苦~
 
 
 
作者 西昆仑

相关内容

    暂无相关文章