pthread pthread_mutex pthread_cond相关

pthread这块api有点多,没有精力去每个都弄清楚。。。。。

这里只说下pthread_mutex 和pthread_cond用法。

1,pthread_cond_wait用于阻塞当前线程,等待别的线程调用pthread_cond_signal或者pthread_cond_broadcast()来唤醒自己。
2,pthread_cond_wait与pthread_mutex_  配合使用,pthread_cond_wait进入wait状态会释放mutex信号量,并等待其他线程释放自己等待的cond信号量,pthread_cond_wait返回时又会重新获得mutex。
3,pthread_cond_signal函数发送信号给另外一个正在处于阻塞状态的线程,使其脱离阻塞状态继续执行,如果没有阻塞,该函数也会返回成功。
4,pthread_cond_signal发送的信号只能被一个阻塞的地方使用,获取该信号要根据阻塞线程的优先级决定,优先级相同的或根据阻塞时间的长度决定。
5,pthread_cond_broadcast会唤醒所有阻塞的线程。
6,pthread_cond_wait一般使用while(1)做判断。

自己先写了个小程序:


#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>


static pthread_cond_t cond1;
static pthread_cond_t cond2;
static pthread_mutex_t test_lock = PTHREAD_MUTEX_INITIALIZER;

static void *
thread1_func(void* arg)
{
	while(1)
	{
		pthread_mutex_lock(&test_lock);
		pthread_cond_wait(&cond1, &test_lock );
		printf("Thread 1 done.\n");
		pthread_mutex_unlock(&test_lock);
		sleep(1);
	}
	return 0;
}

static void *
thread2_func(void* arg)
{
	while(1)
	{
		pthread_mutex_lock(&test_lock);
		pthread_cond_wait(&cond2, &test_lock );
		printf("Thread 2 done.\n");
		pthread_mutex_unlock(&test_lock);
		sleep(1);
	}
	return 0;
}

static void *
thread3_func(void* arg)
{
	while(1)
	{
		pthread_mutex_lock(&test_lock);
		pthread_cond_wait(&cond1, &test_lock );
		printf("Thread 3 done.\n");
		pthread_mutex_unlock(&test_lock);
		pthread_cond_signal(&cond2);
		sleep(1);
	}
	return 0;
}

static void *
thread4_func(void* arg)
{
	while(1)
	{
		pthread_mutex_lock(&test_lock);
		printf("Thread 4 done.\n");
		pthread_mutex_unlock(&test_lock);
		pthread_cond_broadcast(&cond1);
		sleep(1);
	}
	return 0;
}

int main(int argc, const char *argv[])
{
    pthread_t t[4];

    pthread_cond_init(&cond1, NULL);
    pthread_cond_init(&cond2, NULL);
    pthread_create( &t[0], NULL, thread1_func, (void *)1 );
    pthread_create( &t[1], NULL, thread2_func, (void *)2 );
    pthread_create( &t[2], NULL, thread3_func, (void *)3 );
    pthread_create( &t[3], NULL, thread4_func, (void *)4 );

    pthread_join(t[0], NULL);
    pthread_join(t[1], NULL);
    pthread_join(t[2], NULL);
    pthread_join(t[3], NULL);
    return 0;
}

运行结果:
Thread 4 done.

Thread 1 done.

Thread 3 done.

Thread 2 done.

Thread 4 done.

Thread 3 done.

Thread 2 done.

Thread 4 done.

Thread 1 done.

Thread 3 done.

Thread 4 done.

Thread 3 done.

Thread 2 done.

Thread 4 done.

Thread 1 done.

Thread 4 done.

Thread 3 done.

Thread 2 done.

Thread 4 done.

Thread 1 done.

Thread 4 done.

Thread 3 done........

4线程唤醒了1,3线程,3线程又唤醒了2线程。。。

熟悉了api功能后然后自己写了一个生产者消费者的代码:

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>

#define FIFO_SIZE 10

typedef struct 
{
	int Fifo[FIFO_SIZE];
	int ReadPos;
	int writePos;
	pthread_mutex_t locke;
	pthread_cond_t Nfull_Signal;
	pthread_cond_t Nemp_Signal;
}Product;

 Product product;

//判断Fifo为空?
int FifoIsEmpty(Product* Object)
{
	if(Object->ReadPos == Object->writePos)
	{
		return 1;
	}
	return 0;
}

//判断Fifo已满?
int FifoIsFull(Product* Object)
{
	if((Object->writePos+1)%FIFO_SIZE == Object->ReadPos)
	{
		return 1;
	}
	return 0;
}

static void creat_product(Product* Object)
{
	while(FifoIsFull(Object))
	{
		printf("1 wait.......... \n");
		pthread_cond_wait(&(Object->Nfull_Signal),&(Object->locke));
	}

	int product = 1;
	
	Object->Fifo[Object->writePos] = product;
	Object->writePos = (Object->writePos+1)%FIFO_SIZE;
	printf("1 done \n");
	pthread_cond_signal(&(Object->Nemp_Signal));  
}

static void consume_product(Product* Object)
{
	while(FifoIsEmpty(Object))
	{
		printf("2 wait..........\n");
		pthread_cond_wait(&(Object->Nemp_Signal), &(Object->locke));
	}

	int product;
	product = Object->Fifo[Object->ReadPos];
	Object->ReadPos = (Object->ReadPos+1)%FIFO_SIZE;
	printf("2 done \n");
	pthread_cond_signal(&(Object->Nfull_Signal));
}

static void*thread_produce_func(void* arg)
{
	Product* Object = &product;
	while(1)
	{
		pthread_mutex_lock(&(Object->locke));
		printf("1............\n");
		creat_product(Object);
		pthread_mutex_unlock(&(Object->locke));
		sleep(1);
	}
	return 0;
}


static void*thread_consume_func(void* arg)
{
	Product* Object = &product;
	while(1)
	{
		pthread_mutex_lock(&(Object->locke));
		printf("2............ \n");
		consume_product(Object);
		pthread_mutex_unlock(&(Object->locke));
		sleep(3);
	}
	return 0;
}

int main(int argc, const char *argv[])
{
    pthread_t t[2];

    Product *Object = &product;
    
    memset(Object,0x00,sizeof(Product));

    pthread_mutex_init(&(Object->locke), NULL);
    pthread_cond_init(&(Object->Nfull_Signal), NULL);
    pthread_cond_init(&(Object->Nemp_Signal), NULL);
    
    pthread_create( &t[0], NULL, thread_produce_func, NULL );
    pthread_create( &t[1], NULL, thread_consume_func, NULL );

    pthread_join(t[0], NULL);
    pthread_join(t[1], NULL);
    
    return 0;
}

1............

1 done 

2............ 

2 done 

1............

1 done 

1............

1 done 

2............ 

2 done 

1............

1 done 

1............

1 done 

1............

1 done

 2............ 

2 done 

1............

1 done 

1............

1 done 

1............

1 done 

2............ 

2 done 

1............

1 done 

1............

1 done 

1............

1 done 

2............ 

2 done 

1............

1 done 

1............

1 done 

1............

1 wait.......... 

2............ 

2 done 

1 done 

1............

1 wait.......... 

2............

 2 done 

1 done 

1............

1 wait...


由于消费者线程执行会sleep(3)而生产者sleep(1)所以会出现buffer慢的时候,此时生产者等待消费者消费(1 wait),当消费者消费buffer由满变为不满时有会唤醒生产者线程。。。。。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值