生产者,消费者互斥同步参考如下代码:
#include <stdio.h>#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>
#include <error.h>
#include <semaphore.h>
#define PRODUCER_NUM 10
#define CONSUMER_MUM 8
#define BUFFER_SIZE 20
#define SLEEP_TIME 1
#define error_exit( _msg_ ) error(EXIT_FAILURE, errno, _msg_)
int print()
void *consumer_thread(void *args)
void *producer_thread(void *args)
sem_t can_produce
sem_t can_consume
pthread_mutex_t mutex
int produce_index = 0
int consume_index = 0
int producer_id = 0
int consumer_id = 0
int buffer[BUFFER_SIZE] = {0}
int main()
{
int i
pthread_t producer[PRODUCER_NUM]
pthread_t consumer[CONSUMER_MUM]
int sinit1 = sem_init(&can_produce, 0, BUFFER_SIZE)
int sinit2 = sem_init(&can_consume, 0, 0)
if(sinit1 || sinit2)
error_exit("sem_init")
if(pthread_mutex_init(&mutex, NULL))
error_exit("pthread_mutex_init")
for(i=0 i<PRODUCER_NUM i++)
if(pthread_create(&producer[i], NULL, producer_thread, NULL))
error_exit("pthread_create")
for(i=0 i<CONSUMER_MUM i++)
if(pthread_create(&consumer[i], NULL, consumer_thread, NULL))
error_exit("pthread_create")
for(i=0 i<PRODUCER_NUM i++)
pthread_join(producer[i], NULL)
for(i=0 i<CONSUMER_MUM i++)
pthread_join(consumer[i], NULL)
}
void *producer_thread(void *args)
{
int id = producer_id++
while(1){
sleep(SLEEP_TIME)
pthread_mutex_lock(&mutex)
sem_wait(&can_produce)
printf("Producer id %d in %d.\n", id, produce_index)
buffer[produce_index] = 1
produce_index = (produce_index + 1) % BUFFER_SIZE
print()
sem_post(&can_consume)
pthread_mutex_unlock(&mutex)
}
return NULL
}
void *consumer_thread(void *args)
{
int id = consumer_id++
while(1){
sleep(SLEEP_TIME)
pthread_mutex_lock(&mutex)
sem_wait(&can_consume)
printf("Consumer id %d in %d.\n", id, consume_index)
buffer[consume_index] = 0
consume_index = (consume_index + 1) % BUFFER_SIZE
print()
sem_post(&can_produce)
pthread_mutex_unlock(&mutex)
}
return NULL
}
int print()
{
int i
printf("Buffer:\n")
for(i = 0 i < BUFFER_SIZE i++)
printf("___")
printf("\n")
for(i = 0 i < BUFFER_SIZE i++)
printf("|%d|", buffer[i])
printf("\n")
for(i = 0 i < BUFFER_SIZE i++)
printf("___")
printf("\n")
return 0
}
这么高的悬赏,实例放后面。信号量(sem),如同进程一样,线程也可以通过信号量来实现通信,虽然是轻量级的。信号量函数的名字都以"sem_"打头。线程使用的基本信号量函数有四个。
信号量初始化。int sem_init (sem_t *sem , int pshared, unsigned int value)
这是对由sem指定的信号量进行初始化,设置好它的共享选项(linux 只支持为0,即表示它是当前进程的局部信号量),然后给它一个初始值VALUE。
等待信号量。给信号量减1,然后等待直到信号量的值大于0。
int sem_wait(sem_t *sem)
释放信号量。信号量值加1。并通知其他等待线程。
int sem_post(sem_t *sem)
销毁信号量。我们用完信号量后都它进行清理。归还占有的一切资源。
int sem_destroy(sem_t *sem) #include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include <errno.h>
#define return_if_fail(p) if((p) == 0){printf ("[%s]:func error!/n", __func__)return}
typedef struct _PrivInfo
{
sem_t s1
sem_t s2
time_t end_time
}PrivInfo
static void info_init (PrivInfo* thiz)
static void info_destroy (PrivInfo* thiz)
static void* pthread_func_1 (PrivInfo* thiz)
static void* pthread_func_2 (PrivInfo* thiz)
int main (int argc, char** argv)
{
pthread_t pt_1 = 0
pthread_t pt_2 = 0
int ret = 0
PrivInfo* thiz = NULL
thiz = (PrivInfo* )malloc (sizeof (PrivInfo))
if (thiz == NULL)
{
printf ("[%s]: Failed to malloc priv./n")
return -1
}
info_init (thiz)
ret = pthread_create (&pt_1, NULL, (void*)pthread_func_1, thiz)
if (ret != 0)
{
perror ("pthread_1_create:")
}
ret = pthread_create (&pt_2, NULL, (void*)pthread_func_2, thiz)
if (ret != 0)
{
perror ("pthread_2_create:")
}
pthread_join (pt_1, NULL)
pthread_join (pt_2, NULL)
info_destroy (thiz)
return 0
}
static void info_init (PrivInfo* thiz)
{
return_if_fail (thiz != NULL)
thiz->end_time = time(NULL) + 10
sem_init (&thiz->s1, 0, 1)
sem_init (&thiz->s2, 0, 0)
return
}
static void info_destroy (PrivInfo* thiz)
{
return_if_fail (thiz != NULL)
sem_destroy (&thiz->s1)
sem_destroy (&thiz->s2)
free (thiz)
thiz = NULL
return
}
static void* pthread_func_1 (PrivInfo* thiz)
{
return_if_fail(thiz != NULL)
while (time(NULL) < thiz->end_time)
{
sem_wait (&thiz->s2)
printf ("pthread1: pthread1 get the lock./n")
sem_post (&thiz->s1)
printf ("pthread1: pthread1 unlock/n")
sleep (1)
}
return
}
static void* pthread_func_2 (PrivInfo* thiz)
{
return_if_fail (thiz != NULL)
while (time (NULL) < thiz->end_time)
{
sem_wait (&thiz->s1)
printf ("pthread2: pthread2 get the unlock./n")
sem_post (&thiz->s2)
printf ("pthread2: pthread2 unlock./n")
sleep (1)
}
return
}
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)