【那些年遇到过的面试题】pthread_mutex
2016-07-20 14:38
447 查看
#include <pthread.h> #include <stdio.h> #include <stdlib.h> //exit() #include <string.h> #include <unistd.h> //sleep() #define SIZE 1024 char buffer[SIZE]; void *thread_function(void *arg); pthread_mutex_t mutex; int main() { int status; pthread_t thread; void *thread_result; status = pthread_mutex_init(&mutex, NULL); if (status != 0) { perror("Mutex init failed!\n"); exit(EXIT_FAILURE); } status = pthread_create(&thread, NULL, thread_function, NULL); if (status != 0) { perror("Thread create failed!\n"); exit(EXIT_FAILURE); } printf("Input some text. Enter 'end' to finish\n"); while (1) { pthread_mutex_lock(&mutex); printf("main lock mutex!\n"); scanf("%s", buffer); pthread_mutex_unlock(&mutex); printf("main unlock mutex!\n"); if (strncmp("end", buffer, 3) == 0) break; sleep(1); } status = pthread_join(thread, &thread_result); if (status != 0) { perror("Thread join failed!"); exit(EXIT_FAILURE); } printf("Thread joined\n"); pthread_mutex_destroy(&mutex); exit(EXIT_SUCCESS); } void *thread_function(void *arg) { sleep(1); while (1) { pthread_mutex_lock(&mutex); printf("thread_function lock mutex!\n"); printf("You input %s\n",buffer); pthread_mutex_unlock(&mutex); printf("thread_function unlock mutex!\n"); if (strncmp("end", buffer, 3) == 0) break; sleep(1); } }
摘自百度百科
互斥锁
1. 创建和销毁
有两种方法创建互斥锁,静态方式和动态方式。POSIX定义了一个宏PTHREAD_MUTEX_INITIALIZER来静态初始化互斥锁,方法如下: pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER; 在LinuxThreads实现中,pthread_mutex_t是一个结构,而PTHREAD_MUTEX_INITIALIZER则是一个结构常量。
动态方式是采用pthread_mutex_init()函数来初始化互斥锁,API定义如下: int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *mutexattr) 其中mutexattr用于指定互斥锁属性(见下),如果为NULL则使用缺省属性。
pthread_mutex_destroy()用于注销一个互斥锁,API定义如下: int pthread_mutex_destroy(pthread_mutex_t *mutex) 销毁一个互斥锁即意味着释放它所占用的资源,且要求锁当前处于开放状态。由于在Linux中,互斥锁并不占用任何资源,因此LinuxThreads中的pthread_mutex_destroy()除了检查锁状态以外(锁定状态则返回EBUSY)没有其他动作。
2. 互斥锁属性
互斥锁的属性在创建锁的时候指定,在LinuxThreads实现中仅有一个锁类型属性,不同的锁类型在试图对一个已经被锁定的互斥锁加锁时表现不同。当前(glibc2.2.3,linuxthreads0.9)有四个值可供选择:
PTHREAD_MUTEX_TIMED_NP,这是缺省值,也就是普通锁。当一个线程加锁以后,其余请求锁的线程将形成一个等待队列,并在解锁后按优先级获得锁。这种锁策略保证了资源分配的公平性。
PTHREAD_MUTEX_RECURSIVE_NP,嵌套锁,允许同一个线程对同一个锁成功获得多次,并通过多次unlock解锁。如果是不同线程请求,则在加锁线程解锁时重新竞争。
PTHREAD_MUTEX_ERRORCHECK_NP,检错锁,如果同一个线程请求同一个锁,则返回EDEADLK,否则与PTHREAD_MUTEX_TIMED_NP类型动作相同。这样就保证当不允许多次加锁时不会出现最简单情况下的死锁。
PTHREAD_MUTEX_ADAPTIVE_NP,适应锁,动作最简单的锁类型,仅等待解锁后重新竞争。
3. 锁操作
锁操作主要包括加锁pthread_mutex_lock()、解锁pthread_mutex_unlock()和测试加锁pthread_mutex_trylock()三个,不论哪种类型的锁,都不可能被两个不同的线程同时得到,而必须等待解锁。对于普通锁和适应锁类型,解锁者可以是同进程内任何线程;而检错锁则必须由加锁者解锁才有效,否则返回EPERM;对于嵌套锁,文档和实现要求必须由加锁者解锁,但实验结果表明并没有这种限制,这个不同目前还没有得到解释。在同一进程中的线程,如果加锁后没有解锁,则任何其他线程都无法再获得锁。
int pthread_mutex_lock(pthread_mutex_t *mutex)
int pthread_mutex_unlock(pthread_mutex_t *mutex)
int pthread_mutex_trylock(pthread_mutex_t *mutex)
pthread_mutex_trylock()语义与pthread_mutex_lock()类似,不同的是在锁已经被占据时返回EBUSY而不是挂起等待。