1.不同平台使用有差异性
/**
* @class ACE_Thread_Mutex
*
* @brief ACE_Thread_Mutex wrapper (only valid for threads in the same
* process).
*
* This implementation is optimized for locking threads that are
* in the same process. It maps to <CRITICAL_SECTION>s on NT
* and <ACE_mutex_t> with <type> set to <USYNC_THREAD> on UNIX.
* ACE_Thread_Mutex is recursive on some platforms (like
* Win32). However, on most platforms (like Solaris) it is not
* recursive. To be totally safe and portable, developers
* should use ACE_Recursive_Thread_Mutex when they need a
* recursive mutex.
*/
ACE的ACE_Thread_Mutex互斥锁在windows平台上是可重入的,而在Linux平台上是不可重入的,原因上面也有说明Windows下是使用Critical Section实现的,而Critical Section在window下是可递归的。Linux下的pthread_mutex_t锁默认(USYNC_THREAD)是非递归的。可以显示的设置PTHREAD_MUTEX_RECURSIVE属性,将pthread_mutex_t设为递归锁。
2.锁最好不要复制
在ACE中ACE_Thread_Mutex也没有拷贝构造函数,即使有使用起来也会很不安全,一般都用锁的拷贝或者引用来传递。如果锁可以拷贝,那么可能出现两个线程同时拥有一个锁,同时对数据操作的情况,这个时候和没有加锁一样了。
ACE递归锁读写锁
最新推荐文章于 2024-03-26 10:24:13 发布