线程相关函数(4)-pthread_mutex_lock(), pthread_mutex_unlock() 互斥锁
2018-01-11 10:50
351 查看
互斥锁实例:
#include <pthread.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int pthread_mutex_destroy(pthread_mutex_t *mutex);
int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);
int pthread_mutex_lock(pthread_mutex_t *mutex);
int pthread_mutex_trylock(pthread_mutex_t *mutex);
int pthread_mutex_unlock(pthread_mutex_t *mutex);
示例代码:
运行结果:
....
71025700: 9979
71025700: 9980
71025700: 9981
71025700: 9982
71025700: 9983
71025700: 9984
71025700: 9985
71025700: 9986
71025700: 9987
71025700: 9988
71025700: 9989
71025700: 9990
71025700: 9991
71025700: 9992
71025700: 9993
71025700: 9994
71025700: 9995
71025700: 9996
71025700: 9997
71025700: 9998
71025700: 9999
71025700: 10000
#include <pthread.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int pthread_mutex_destroy(pthread_mutex_t *mutex);
int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);
int pthread_mutex_lock(pthread_mutex_t *mutex);
int pthread_mutex_trylock(pthread_mutex_t *mutex);
int pthread_mutex_unlock(pthread_mutex_t *mutex);
示例代码:
#include <pthread.h> #include <stdio.h> #define NLOOP 5000 static pthread_mutex_t counter_mutex = PTHREAD_MUTEX_INITIALIZER; static int counter; void *doit(void *); int main() { pthread_t tidA, tidB; pthread_create(&tidA, NULL, doit, NULL); pthread_create(&tidB, NULL, doit, NULL); /*wait for both threads to terminate*/ pthread_join(tidA, NULL); pthread_join(tidB, NULL); return 0; } void *doit(void *arg) { int i, val; for (i=0; i<NLOOP; i++) { pthread_mutex_lock(&counter_mutex); val = counter; printf("%x: %d\n", (unsigned int)pthread_self(), val+1); counter = val + 1; pthread_mutex_unlock(&counter_mutex); } return NULL; }
运行结果:
....
71025700: 9979
71025700: 9980
71025700: 9981
71025700: 9982
71025700: 9983
71025700: 9984
71025700: 9985
71025700: 9986
71025700: 9987
71025700: 9988
71025700: 9989
71025700: 9990
71025700: 9991
71025700: 9992
71025700: 9993
71025700: 9994
71025700: 9995
71025700: 9996
71025700: 9997
71025700: 9998
71025700: 9999
71025700: 10000
相关文章推荐
- 互斥锁----pthread_mutex_t,pthread_mutex_init,pthread_mutex_lock,pthread_mutext_unlock,pthread_mutex_des
- Linux 多线程等待超时机制的实现:pthread_mutex_lock/pthread_cond_signal/pthread_mutex_unlock
- linux下pthread_mutex_lock,pthread_mutex_unlock的使用http://blog.chinaunix.net/uid-28458801-id-4300492.ht
- linux基础编程 多线程中的互斥锁 pthread_mutex_lock
- pthread_mutex_init, pthread_mutex_lock, pthread_mutex_unlock,函数(线程互斥的例子)
- linux基础编程 多线程中的互斥锁 pthread_mutex_lock
- pthread_mutex_t pthread_mutex_init pthread_mutex_lock -- pthread_mutex_unlock pthread_delay_np
- pthread 线程库编程 一:线程与互斥锁 mutex_lock
- linux基础编程 多线程中的互斥锁 pthread_mutex_lock
- 互斥锁----pthread_mutex_t,pthread_mutex_init,pthread_mutex_lock,pthread_mutext_unlock,pthread_mutex_des
- pthread_cond_signal 和 pthread_mutex_unlock顺序问题
- man pthread_mutex_lock,man pthread_cond_wait没文档的解决办法
- pthread_mutex_lock
- linux互斥锁用法范例pthread_mutex_lock
- ubuntu环境无pthread_mutex_lock 函数 man 手册
- 互斥锁pthread_mutex_t的使用
- spin_lock 自旋锁 和 mutex_lock 互斥锁的区别
- pthread_mutex_t 互斥锁
- pthread_mutex_t pthread_mutex_lock() pthread_ulock() pthread_guard pthread_cond_t wait用法