标签:
#include <pthread.c> 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);与信号量相似,这些函数的参数都是一个先前声明过的对象的指针.对互斥量来说,这个对象的类型为pthread_mutex_t.pthread_mutex_init函数中的属性参数默认为fast,一般传递参数NULL保持默认属性.
/************************************************************************* > File Name: thread4.c > Description: thread4.c通过互斥量来保证任一时刻只有一个线程访问字符数组work_area(对它进行读(统计)/写) > Author: Liubingbing > Created Time: 2015年07月05日 星期日 20时47分06秒 > Other: thread4.c省略了对互斥量加锁和解锁调用的返回值应该进行的一些错误检查 ************************************************************************/ #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_acea 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; /* pthread_mutex_init初始化互斥量work_mutex */ res = pthread_mutex_init(&work_mutex, NULL); if (res != 0) { perror("Mutex initialization failed"); exit(EXIT_FAILURE); } /* pthread_create创建新线程 * a_thread中保存新线程的标识符, thread_function为新线程启动调用的函数 */ res = pthread_create(&a_thread, NULL, thread_function, NULL); if (res != 0) { perror("Thread creation failed"); exit(EXIT_FAILURE); } /* pthread_mutex_lock对互斥量work_mutex加锁,如果它已经被锁住,则这个调用将被阻塞直到它被释放为止 */ 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_mutext解锁 */ pthread_mutex_unlock(&work_mutex); while (1) { /* pthread_mutex_lock对互斥量work_mutex加锁 */ pthread_mutex_lock(&work_mutex); /* 如果work_area第一个字符不为null,则对互斥量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); /* 首先试图对互斥量work_mutex加锁,如果它已经被锁住,这个调用将被阻塞直到它释放为止. */ pthread_mutex_lock(&work_mutex); while (strncmp("end", work_area, 3) != 0) { printf("You input %d characters\n", strlen(work_area) - 1); /* 通过将第一个字符设置为null的方法来通知读取输入的线程, 新线程完成了字符统计 */ work_area[0] = '\0'; /* 对互斥量work_mutex解锁, 并等待主线程继续运行*/ pthread_mutex_unlock(&work_mutex); sleep(1); /* 对互斥量work_mutex加锁, 如果加锁成功就检查是否主线程又有字符送来要处理 */ pthread_mutex_lock(&work_mutex); while (work_area[0] == '\0') { /* 如果没有字符要处理,就解锁互斥量,继续等待 */ pthread_mutex_unlock(&work_mutex); sleep(1); pthread_mutex_lock(&work_mutex); } } /* 如果work_area="end",则设置time_to_exit = 1, 之后退出 */ time_to_exit = 1; work_area[0] = '\0'; pthread_mutex_unlock(&work_mutex); pthread_exit(0); }新线程首先对等待1秒,然后对互斥量work_mutex加锁,如果work_mutex已经加锁,则这个调用将被阻塞直到互斥量被时释放为止.一旦获得访问权,先判断work_area是否为end.
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/yiranant/article/details/46772403