1.涉及类
ACE_RW_Thread_Mutex //ACE读写锁
ACE_Read_Guard //ACE加读锁
ACE_Write_Guard //ACE加写锁
ACE_Thread_Manager //ACE线程管理
2.简介
ACE读/写锁
读/写锁与互斥体相类似。例如,获取读/写锁的线程也必须释放它。多个线程可同时获取一个读/写锁用于读,但只有一个线程可以获取该锁用于写。当互斥体保护的资源用于读远比用于写要频繁时,读/写互斥体有助于改善并发的执行。
ACE线程库提供了一个叫作ACE_RW_Mutex的类,在C++封装类中可移植地实现了读/写锁的语义。读/写锁将优先选择权给写。因而,如果有多个读和一个写在锁上等待,写将会首先获取它。
3.示例代码
1 #include <iostream> 2 #include "ace/Thread_Manager.h" 3 #include "ace/Guard_T.h" 4 #include "ace/OS_NS_time.h" 5 6 using namespace std; 7 ACE_RW_Thread_Mutex mutex; 8 9 ACE_THR_FUNC_RETURN ThreadFunRead(void *arg) 10 { 11 ACE_thread_t threadId=ACE_Thread_Manager::instance()->thr_self(); 12 13 ACE_Read_Guard<ACE_RW_Thread_Mutex> guard(mutex); 14 cout<<"Thread ReadLock begin NO:"<<threadId<<endl; 15 16 17 //ACE_OS::sleep(4); 18 cout<<"Thread ReadLock end NO:"<<threadId<<endl; 19 return NULL; 20 } 21 ACE_THR_FUNC_RETURN ThreadFunWrite(void *arg) 22 { 23 ACE_thread_t threadId=ACE_Thread_Manager::instance()->thr_self(); 24 25 ACE_Write_Guard<ACE_RW_Thread_Mutex> guard(mutex); 26 cout<<"Thread WriteLock begin NO:"<<threadId<<endl; 27 28 ACE_OS::sleep(1);//休眠1s 29 cout<<"Thread WriteLock end NO:"<<threadId<<endl; 30 return NULL; 31 } 32 33 int main(int argc, char *argv[]) 34 { 35 //1000纳秒=1ms 36 timespec_t t; 37 t.tv_sec=0;t.tv_nsec=1000; 38 39 ACE_Thread_Manager* pThrMgr = ACE_Thread_Manager::instance(); 40 //依次一读一写依次3个循环,结果显示是此读写锁写优先。即:加锁等待队列里写操作永远在读前面! 41 for(int i=0;i<4;i++) 42 { 43 pThrMgr->spawn((ACE_THR_FUNC)ThreadFunRead); 44 ACE_OS::nanosleep (&t); 45 pThrMgr->spawn((ACE_THR_FUNC)ThreadFunWrite); 46 ACE_OS::nanosleep (&t); 47 } 48 pThrMgr->wait(); 49 50 return 0; 51 }
4.示例运行结果
$ ./tt Thread ReadLock begin NO:140015069234944 Thread ReadLock end NO:140015069234944 Thread WriteLock begin NO:140015058745088 Thread WriteLock end NO:140015058745088 Thread WriteLock begin NO:140015037765376 Thread WriteLock end NO:140015037765376 Thread WriteLock begin NO:140015016785664 Thread WriteLock end NO:140015016785664 Thread WriteLock begin NO:140014658778880 Thread WriteLock end NO:140014658778880 Thread ReadLock begin NO:140015048255232 Thread ReadLock end NO:140015048255232 Thread ReadLock begin NO:140015027275520 Thread ReadLock end NO:140015027275520 Thread ReadLock begin NO:140015006295808 Thread ReadLock end NO:140015006295808
5.Makefile
#bin THE_INCLUDE = -I./ -I${ACE_ROOT}/ THE_LIBPATH = -L./ -L${ACE_ROOT}/lib/ THE_LIB = -g -D_DEBUG_ -lACE# -lxti CFLAGS = ${THE_LIB} ${THE_INCLUDE} ${THE_LIBPATH} CC=g++ SRC=$(wildcard *.cpp) OBJ=$(addprefix ./, $(addsuffix .o, $(basename $(SRC)))) TARGET=tt all: $(TARGET) $(TARGET): $(SRC) $(CC) -o $@ $^ $(CFLAGS) @echo make all done %.o: %.cpp $(CC) $(CFLAGS) -o $@ -c $< clean: rm -f $(TARGET) $(OBJ)
********************************************************************
高级线程(任务)ACE_Task_Base类来创建线程是更方便和优雅
6.示例代码
1 #include <iostream> 2 #include "ace/Task.h" 3 4 using namespace std; 5 6 //静态锁 7 struct Resource 8 { 9 static ACE_RW_Thread_Mutex mutex; 10 }; 11 ACE_RW_Thread_Mutex Resource::mutex; 12 13 class ThreadFunRead : public ACE_Task_Base { 14 public: 15 virtual int svc() 16 { 17 ACE_Read_Guard<ACE_RW_Thread_Mutex> guard(Resource::mutex); 18 cout<<"Thread ReadLock begin NO:"<<ACE_Thread::self()<<endl; 19 20 //ACE_OS::sleep(4); 21 cout<<"Thread ReadLock end NO:"<<ACE_Thread::self()<<endl; 22 return 0; 23 } 24 }; 25 26 class ThreadFunWrite : public ACE_Task_Base { 27 public: 28 virtual int svc() 29 { 30 ACE_Write_Guard<ACE_RW_Thread_Mutex> guard(Resource::mutex); 31 cout<<"Thread WriteLock begin NO:"<<ACE_Thread::self()<<endl; 32 33 ACE_OS::sleep(1);//休眠1s 34 cout<<"Thread WriteLock end NO:"<<ACE_Thread::self()<<endl; 35 return 0; 36 } 37 }; 38 39 40 int main(int argc, char *argv[]) 41 { 42 //1000纳秒=1ms 43 timespec_t t; 44 t.tv_sec=0;t.tv_nsec=1000; 45 46 ACE_Thread_Manager* pThrMgr = ACE_Thread_Manager::instance(); 47 //依次一读一写依次3个循环,结果显示是此读写锁写优先。即:加锁等待队列里写操作永远在读前面! 48 for(int i=0;i<4;i++) 49 { 50 ThreadFunRead threadRead; 51 ThreadFunWrite threadWrite; 52 threadRead.activate(); 53 ACE_OS::nanosleep (&t); 54 threadWrite.activate(); 55 ACE_OS::nanosleep (&t); 56 } 57 58 pThrMgr->wait(); 59 60 return 0; 61 }