ACE线程之ACE_Thread_Mutex互斥锁
来源:互联网 发布:mac虚拟机安装win7不了 编辑:程序博客网 时间:2024/06/04 18:56
互斥体实现了互相排斥(mutual exclusion)同步的简单形式。互斥体禁止多个线程同时进入受保护的代码临界区(critical section)。因此,在任意时刻,只有一个线程被允许进入这样代码保护区。
任何线程在进入临界区之前,必须获取acquire与此区域相关的互斥体的所有权。如果已有另一线程拥有了临界区互斥体,其他线程就不能再进入其中。这些线程必须等待,知道当前的属主线程释放release该互斥体。
什么时候需要使用互斥体呢?互斥体用于保护共享的易变代码,也就是,全局或静态数据。这样的数据必须通过互斥体进行保护,以防止它们在多个线程同时访问时损坏。
如下示例:
#include <QtCore/QCoreApplication>#include "ace/Synch.h"#include "ace/Log_Msg.h" //ace_debug#include "ace/OS_NS_stdio.h" ///ACE_OS::printf#include "ace/Thread.h"struct Args{public:Args(int iterations):mutex_(),iterations_(iterations) {}ACE_Thread_Mutex mutex_;int iterations_;};///the starting point for the worker threadsstatic void* worker(void *arguments){Args *arg= (Args*) arguments;for(int i=0; i<arg->iterations_; i++){ACE_DEBUG((LM_DEBUG,"(%t) Trying to get a hold of this iteration\n"));///this is our critical sectionarg->mutex_.acquire(); ///acquire: 获得ACE_DEBUG((LM_DEBUG,"(%t) This is iteration number %d\n",i));ACE_OS::sleep(2);//simulate critical(关键,临界) workarg->mutex_.release();}return 0;}int main(int argc, char *argv[]){QCoreApplication a(argc, argv);if(argc<2){ACE_OS::printf("Usage: %s <number_of_threads> <num_of_iterations>\n",argv[0]);ACE_OS::exit(1);}Args arg(ACE_OS::atoi(argv[2]));//setup the argumentsint n_threads= ACE_OS::atoi(argv[1]);ACE_thread_t *threadID=new ACE_thread_t[n_threads+1]; ///DWORDACE_hthread_t *threadHandles=new ACE_hthread_t[n_threads+1]; ///HANDLEif(ACE_Thread::spawn_n(threadID, ////id's for each of the threads n_threads, ////number of threads to spawn (ACE_THR_FUNC)worker, ///entry point for new thread &arg, ////args to worker THR_JOINABLE | THR_NEW_LWP, //flags ACE_DEFAULT_THREAD_PRIORITY, 0,0,threadHandles)==-1){ACE_DEBUG((LM_DEBUG,"Error in spawning thread\n"));}for(int i=0; i<n_threads; i++)ACE_Thread::join(threadHandles[i]);return 0;///a.exec();}
(9912) Trying to get a hold of this iteration
(9912) This is iteration number 0
(7312) Trying to get a hold of this iteration
(7312) This is iteration number 0
(9912) Trying to get a hold of this iteration
(9912) This is iteration number 1
(7312) Trying to get a hold of this iteration
(9912) Trying to get a hold of this iteration
(7312) This is iteration number 1
(7312) Trying to get a hold of this iteration
(9912) This is iteration number 2
(9912) Trying to get a hold of this iteration
(7312) This is iteration number 2
(7312) Trying to get a hold of this iteration
(7312) This is iteration number 3
(9912) This is iteration number 3
请按任意键继续. . .
- ACE线程之ACE_Thread_Mutex互斥锁
- ACE利用ACE_Thread_Mutex实现线程间互斥访问临界区
- ACE - ACE_Recursive_Thread_Mutex, ACE_Thread_Mutex, ACE_Condition
- ACE - ACE_Recursive_Thread_Mutex, ACE_Thread_Mutex, ACE_Condition .
- [ACE程序员教程笔记]使用ACE_Thread_Mutex访问子线程中的数据
- ACE之线程并发控制
- ACE之线程并发控制
- ACE-ACE线程管理机制
- 使用ACE创建线程
- ACE线程管理机制
- ACE线程管理
- ACE定时器线程无效
- ace线程控制
- ace线程管理
- ACE线程编写
- 利用ACE管理线程
- ACE_Thread_Manager-------ACE线程管理
- 关于ACE创建线程
- c/c++类型转换总结
- lightoj 1136 第一次打表找规律做题..
- JAVA实现DES加密
- 《高可用MySQL——构建健壮的数据中心》读书笔记之一
- TYPES和DATA的区别
- ACE线程之ACE_Thread_Mutex互斥锁
- Linux经验笔记
- C#连接数据库
- 追逐的日子
- linux系统下将php和mysql命令加入到环境变量中的方法
- [转]用友NC单据UI基本代码示例
- Github Mybatis深入学习之Java API
- ARM中的RO、RW和ZI DATA
- MFC中菜单相关