第十二章 POSIX 线程(三)

用互斥量进行同步

       另一种用在多线程程序中的同步访问方法是使用互斥量。它允许程序员锁住某个对象,使得每次只有一个线程访问它。为了控制对关键代码的访问,必须在进入这段代码之前锁住一个互斥量,然后在完成操作之后解锁它。

#include <pthread.h>
int pthread_mutex_init(pthread_mutex_t *mutex,  const pthread_mutexattr_t *mutexattr);
int pthread_mutex_lock(pthread_mutex_t *mutex);
int pthread_mutex_unlock(pthread_mutex_t *mutex);
int pthread_mutex_destroy(pthread_mutex_t *mutex);

       成功时返回 0,失败返回错误代码,但这些函数并不设置 errno,你必须对函数的返回代码进行检查。

       与信号量相类似,这些函数的参数都是一个先前声明过的以一个指向前面声明的对象的指针为参数,在互斥方法是一个pthread_mutex_t。额外的属性参数pthread_mutexattr_t允许我们为互斥提供属性,这可以控制其行为。属性类型默认为"fast"。这有一个小的缺点,如果我们的程序试着在一个已经上锁的互斥量上调用pthread_mutex_lock,程序就会阻塞。因为拥有锁的线程现在被阻塞了,互斥量就不会被解锁,从而程序就会进入死锁状态。可以修改互斥量的属性,从而他或者可以检测这种情况并返回一个错误,或者是循环操作并且在同一个线程上允许多个锁。
实验--线程互斥量

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <semaphore.h>

void *thread_function(void *arg);
pthread_mutex_t work_mutex; /* protects both work_area and time_to_exit */

#define WORK_SIZE 1024
char work_area[WORK_SIZE];
int time_to_exit = 0;

int main() {
    int res;
    pthread_t a_thread;
    void *thread_result;
    res = pthread_mutex_init(&work_mutex, NULL);
    if (res != 0) {
        perror("Mutex initialization failed");
        exit(EXIT_FAILURE);
    }
    res = pthread_create(&a_thread, NULL, thread_function, NULL);
    if (res != 0) {
        perror("Thread creation failed");
        exit(EXIT_FAILURE);
    }
    pthread_mutex_lock(&work_mutex);
    printf("Input some text. Enter 'end' to finish\n");
    while(!time_to_exit) {
        fgets(work_area, WORK_SIZE, stdin);
        pthread_mutex_unlock(&work_mutex);
        while(1) {
            pthread_mutex_lock(&work_mutex);
            if (work_area[0] != '\0') {
                pthread_mutex_unlock(&work_mutex);
                sleep(1);
            }
            else {
                break;
            }
        }
    }
    pthread_mutex_unlock(&work_mutex);
    printf("\nWaiting for thread to finish...\n");
    res = pthread_join(a_thread, &thread_result);
    if (res != 0) {
        perror("Thread join failed");
        exit(EXIT_FAILURE);
    }
    printf("Thread joined\n");
    pthread_mutex_destroy(&work_mutex);
    exit(EXIT_SUCCESS);
}

void *thread_function(void *arg) {
    sleep(1);
    pthread_mutex_lock(&work_mutex);
    while(strncmp("end", work_area, 3) != 0) {
        printf("You input %d characters\n", strlen(work_area) -1);
        work_area[0] = '\0';
        pthread_mutex_unlock(&work_mutex);
        sleep(1);
        pthread_mutex_lock(&work_mutex);
        while (work_area[0] == '\0' ) {
            pthread_mutex_unlock(&work_mutex);
            sleep(1);
            pthread_mutex_lock(&work_mutex);
        }
    }
    time_to_exit = 1;
    work_area[0] = '\0';
    pthread_mutex_unlock(&work_mutex);
    pthread_exit(0);
}
运行

$ ./thread4
Input some text. Enter ‘end’ to finish
Whit
You input 4 characters
The Crow Road
You input 13 characters
end
Waiting for thread to finish...
Thread joined

实验解析

在程序的开始,我们声明了一个互斥量、工作区和一个变量 time_to_exit。如下所示:

pthread_mutex_t work_mutex; /* protects both work_area and time_to_exit */

#define WORK_SIZE 1024
char work_area[WORK_SIZE];
int time_to_exit = 0;
然后初始化互斥量:

res = pthread_mutex_init(&work_mutex, NULL);
if (res != 0) {
    perror("Mutex initialization failed");
    exit(EXIT_FAILURE);
}

接下来启动新线程:

pthread_mutex_lock(&work_mutex);
while(strncmp("end", work_area, 3) != 0) {
    printf("You input %d characters\n", strlen(work_area) -1);
    work_area[0] = '\0';
    pthread_mutex_unlock(&work_mutex);
    sleep(1);
    pthread_mutex_lock(&work_mutex);
    while (work_area[0] == '\0' ) {
         pthread_mutex_unlock(&work_mutex);
         sleep(1);
         pthread_mutex_lock(&work_mutex);
     }
}
time_to_exit = 1;
work_area[0] = '\0';
pthread_mutex_unlock(&work_mutex);
pthread_exit(0);

       新线程首先试图对互斥量加锁。如果它已经被锁住,这个调用将被阻塞直到它被释放为止。一旦获得访问权,我们就检查是否有申请退出程序的请求。如果就设置 time_to_exit 变量,再把工作区的第一个字符设置为 \0,然后退出。

       如果不想退出,就统计字符个数,然后把 work_area 中的第一个字符设置为 NULL。我们用将第一个字符设置为 NULL 的方法通知读取输入的线程,我们已经完成了字符统计。然后解锁互斥量并等待主线程继续运行。我们将周期性地尝试给互斥量加锁,如果加锁成功,就检查是否主线程又有字符送来要处理。如果还没有,就解锁互斥量继续等待;如果有,就统计字符个数并再次进入循环。

下面是主线程的程序:

pthread_mutex_lock(&work_mutex);
printf(“Input some text. Enter ‘end’ to finish/n”);
while(!time_to_exit) {
    fgets(work_area, WORK_SIZE, stdin);
    pthread_mutex_unlock(&work_mutex);
    while(1) {
        pthread_mutex_lock(&work_mutex);
        if (work_area[0] != ‘/0’) {
            pthread_mutex_unlock(&work_mutex);
            sleep(1);
        }
        else {
            break;
        }
    }
}
pthread_mutex_unlock(&work_mutex);
       这段代码和上面新线程中的很相似。我们首先给工作区加锁,读入文本到它里面,然后解锁以允许其它线程访问它并统计字符数目。我们周期性地对互斥量再加锁,检查字符数目是否已统计完(work_area[0]被设置为NULL)。如果还需要等待,就释放互斥量。如前所述,这种通过轮询来获得结果的方法通常并不是最好的编程方式。在实际的编程中,我们应该尽量用信号量来避免出现这种情况。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值