码迷,mamicode.com
首页 > 其他好文 > 详细

POSIX信号量与互斥锁

时间:2014-06-05 12:39:14      阅读:412      评论:0      收藏:0      [点我收藏+]

标签:des   c   style   a   color   int   

POSIX信号量相关函数:
sem_open
sem_close
sem_unlink

sem_init
sem_destroy

sem_wait
sem_post

POSIX互斥锁
pthread_mutex_init
pthreaad_mutex_lock
pthread_mutex_unlock
pthread_mutex_destroy

自旋锁
自旋锁类似于互斥锁,它的性能比互斥锁更高。
自旋锁与互斥锁很重要的一个区别在于,线程在申请自旋锁的时候,线程不会被挂起,它处于忙等待的状态。

pthread_spin_init
pthread_spin_destroy
pthread_spin_lock
pthread_spin_unlock

读写锁
只要没有线程持有给定的读写锁用于写,那么任意数目的线程可以持有读写锁用于读
仅当没有线程持有某个给定的读写锁用于读或用于写时,才能分配读写锁用于写
读写锁用于读称为共享锁,读写锁用于写称为排它锁

pthread_rwlock_init
pthread_rwlock_destroy
int pthread_rwlock_rdlock
int pthread_rwlock_wrlock
int pthread_rwlock_unlock

pctest.c
#include <unistd.h>
#include <sys/types.h>
#include <pthread.h>
#include <semaphore.h>


#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>


#define ERR_EXIT(m) \
        do \
        { \
                perror(m); \
                exit(EXIT_FAILURE); \
        } while(0)


#define CONSUMERS_COUNT 1
#define PRODUCERS_COUNT 1
#define BUFFSIZE 10


int g_buffer[BUFFSIZE];


unsigned short in = 0;
unsigned short out = 0;
unsigned short produce_id = 0;
unsigned short consume_id = 0;


sem_t g_sem_full;
sem_t g_sem_empty;
pthread_mutex_t g_mutex;


pthread_t g_thread[CONSUMERS_COUNT+PRODUCERS_COUNT];


void* consume(void *arg)
{
int i;
int num = (int)arg;
while (1)
{
printf("%d wait buffer not empty\n", num);
sem_wait(&g_sem_empty);
pthread_mutex_lock(&g_mutex);


for (i=0; i<BUFFSIZE; i++)
{
printf("%02d ", i);
if (g_buffer[i] == -1)
printf("%s", "null");
else
printf("%d", g_buffer[i]);


if (i == out)
printf("\t<--consume");


printf("\n");
}
consume_id = g_buffer[out];
printf("%d begin consume product %d\n", num, consume_id);
g_buffer[out] = -1;
out = (out + 1) % BUFFSIZE;
printf("%d end consume product %d\n", num, consume_id);
pthread_mutex_unlock(&g_mutex);
sem_post(&g_sem_full);
sleep(1);
}
return NULL;
}


void* produce(void *arg)
{
int num = (int)arg;
int i;
while (1)
{
printf("%d wait buffer not full\n", num);
sem_wait(&g_sem_full);
pthread_mutex_lock(&g_mutex);
for (i=0; i<BUFFSIZE; i++)
{
printf("%02d ", i);
if (g_buffer[i] == -1)
printf("%s", "null");
else
printf("%d", g_buffer[i]);


if (i == in)
printf("\t<--produce");


printf("\n");
}


printf("%d begin produce product %d\n", num, produce_id);
g_buffer[in] = produce_id;
in = (in + 1) % BUFFSIZE;
printf("%d end produce product %d\n", num, produce_id++);
pthread_mutex_unlock(&g_mutex);
sem_post(&g_sem_empty);
sleep(5);
}
return NULL;
}


int main(void)
{
int i;
for (i=0; i<BUFFSIZE; i++)
g_buffer[i] = -1;

sem_init(&g_sem_full, 0, BUFFSIZE);
sem_init(&g_sem_empty, 0, 0);


pthread_mutex_init(&g_mutex, NULL);




for (i=0; i<CONSUMERS_COUNT; i++)
pthread_create(&g_thread[i], NULL, consume, (void*)i);


for (i=0; i<PRODUCERS_COUNT; i++)
pthread_create(&g_thread[CONSUMERS_COUNT+i], NULL, produce, (void*)i);

for (i=0; i<CONSUMERS_COUNT+PRODUCERS_COUNT; i++)
pthread_join(g_thread[i], NULL);


sem_destroy(&g_sem_full);
sem_destroy(&g_sem_empty);
pthread_mutex_destroy(&g_mutex);


return 0;
}

makefile:
.PHONY:clean all
CC=gcc
CFLAGS=-Wall -g
BIN=pctest
all:$(BIN)
%.o:%.c
$(CC) $(CFLAGS) -c $< -o $@
pctest:pctest.o
$(CC) $(CFLAGS) $^ -o $@ -lpthread
clean:
rm -f *.o $(BIN)




POSIX信号量与互斥锁,布布扣,bubuko.com

POSIX信号量与互斥锁

标签:des   c   style   a   color   int   

原文地址:http://blog.csdn.net/fuyuehua22/article/details/27231765

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!