互斥锁

NOTE: 以上内容摘自《UNIX网络编程》(卷2),做下记录,方便以后查阅;

互斥锁是最基本的同步方式,它用来保护临界区,任何时刻只有一个线程或者进程在执行其中的代码。
通常用法:

lock_the_mutex(...);
critical region
unlock_the_mutex(...);

相关接口汇总如下:

#include<pthread.h>
/* init and destroy. */
int pthread_mutex_int(pthread_mutex_t *mptr, const pthread_mutexattr_t *attr);
int pthread_mutex_destroy(pthread_mutex_t *mptr);
/* lock and unlock. */
int pthread_mutex_lock(pthread_mutex_t *mptr);
int pthread_mutex_trylock(pthread_mutex_t *mptr);
int pthread_mutex_unlock(pthread_mutex_t *mptr);
/* attr used. */
/* value = PTHREAD_PROCESS_PRIVATE | PTHREAD_PROCESS_SHARED; */
int pthread_mutexattr_init(pthread_mutexattr_t *attr);
int pthread_mutexattr_destroy(pthread_mutexattr_t *attr);
int pthread_mutexattr_getpshared(const pthread_mutexattr_t *attr, int *valptr);
int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr);

/* 上述接口若执行成功都返回0, 若失败则返回为正的Exxx值。 */

如果互斥锁是静态分配的,还可以使用宏PTHREAD_MUTEX_INITIALIZER来初始化,如:

static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;

生产者-消费者问题(producer-consumer)
源码(多个生产者,单个消费者)如下:

#ifndef __COMMON_H
#define __COMMON_H

#include <stdio.h>

void err_quit(char *str_err)
{
    printf(str_err);
}

int min(int a, int b)
{
    return (a < b ? a : b);
}

int max(int a, int b)
{
    return (a > b ? a : b); 
}

#endif /* __COMMON_H */
#include "common.h"
#include <pthread.h>

#define MAXNITEMS 1000000
#define MAXNTHREADS 100

int nitems;
struct {
    pthread_mutex_t mutex;
    int buff[MAXNITEMS];
    int nput;
    int nval;
} shared = {
    PTHREAD_MUTEX_INITIALIZER
};

void *produce(void*);
void *consume(void*);

int 
main(int argc, char **argv)
{
    int i;
    int nthreads;
    int count[MAXNTHREADS];

    pthread_t tid_produce[MAXNTHREADS];
    pthread_t tid_consume;

    if (argc != 3)
    {
        err_quit("usage: prodcons2 <#items> <#threads>");
    }

    nitems = min(atoi(argv[1]), MAXNITEMS);
    nthreads = min(atoi(argv[2]), MAXNTHREADS);

    // Set_concurrency(nthreads + 1);

    for (i = 0; i < nthreads; i++)
    {
        count[i] = 0;
        pthread_create(&tid_produce[i], NULL, produce, &count[i]);
    }

    pthread_create(&tid_consume, NULL, consume, NULL);
    /* wait for all the producer threads and comsumer. */
    for (i = 0; i < nthreads; i++)
    {
        pthread_join(tid_produce[i], NULL);
        printf("count[%d] = %d\n", i, count[i]);
    }
    pthread_join(tid_consume, NULL);

    return 0;
}

void *
produce(void *arg)
{
    for (;;)
    {
        pthread_mutex_lock(&shared.mutex);
        /* array is full, we're done. */
        if (shared.nput >= nitems)
        {
            pthread_mutex_unlock(&shared.mutex);
            return NULL;
        }

        shared.buff[shared.nput] = shared.nval;
        shared.nput++;
        shared.nval++;
        pthread_mutex_unlock(&shared.mutex);
        *((int *)arg) += 1;
    }
}

void
consume_wait(int i)
{
    for ( ; ; )
    {
        pthread_mutex_lock(&shared.mutex);
        if (i < shared.nput)
        {
            pthread_mutex_unlock(&shared.mutex);
            return;
        }
        pthread_mutex_unlock(&shared.mutex);
    }
}

void *
consume(void *arg)
{
    int i;

    for(i = 0; i < nitems; i++)
    {
        consume_wait(i);
        if (shared.buff[i] != i)
        {
            printf ("buff[%d] = %d\n", i , shared.buff[i]);
        }
    }

    return NULL;
}

编译运行:

linux$ gcc main1.c -lpthread
linux$ ./a.out 1000000 5
count[0] = 178974
count[1] = 155261
count[2] = 199276
count[3] = 271866
count[4] = 194623
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值