如果想动态改变代码所使用的锁类型,ACE的ACE_Lock和ACE_Lock_Adapter,可以用于运行时替换。
下面的例子根据程序的输入选择使用递归互斥体或者非递归互斥体。但是使用动态绑定的缺点是每次调用都需要负担额外的经由虚函数表的间接层次。
#include <stdio.h>
#include <stdlib.h>
#include "ace/Lock_Adapter_T.h"
#include "ace/Thread.h"
#include "ace/Synch.h"
#include "ace/Log_Msg.h"
struct Args{
public:
Args(ACE_Lock *lock,int iterations):_mutex(lock),
iterations(iterations){}
ACE_Lock *_mutex;
int iterations;
};
static void* worker(void* arguments)
{
struct Args *args = (Args *)arguments;
for(int i = 0;i<args->iterations;i++)
{
ACE_DEBUG((LM_DEBUG,"(%t) Tring to get a hold of this iterations\n"));
args->_mutex->acquire();
ACE_DEBUG((LM_DEBUG,"(%t) get the hold of iterations [%d]\n",i));
ACE_OS::sleep(2);
args->_mutex->release();
}
return 0;
}
int main(int argc,char* argv[])
{
int thread_num = 0;
if(argc<4)
{
ACE_OS::printf("Usage:%s <number of threads>\
<number of iterations> <type of lock>\n",argv[0]);
ACE_OS::exit(1);
}
ACE_Lock *lock;
if(ACE_OS::strcmp(argv[3],"Recursive"))
lock = new ACE_Lock_Adapter<ACE_Recursive_Thread_Mutex>;
else
lock = new ACE_Lock_Adapter<ACE_Thread_Mutex>;
struct Args args(lock,ACE_OS::atoi(argv[2]));
thread_num = ACE_OS::atoi(argv[1]);
ACE_thread_t *thread_id = new ACE_thread_t[thread_num+1];
ACE_hthread_t *thread_handles = new ACE_hthread_t[thread_num+1];
if(ACE_Thread::spawn_n(thread_id,thread_num,(ACE_THR_FUNC)worker,
&args,THR_JOINABLE|THR_NEW_LWP,ACE_DEFAULT_THREAD_PRIORITY,
0,0,thread_handles)==-1)
{
ACE_DEBUG((LM_DEBUG,"Error in spawn threads\n"));
}
for(int i=0;i<thread_num;i++)
{
ACE_Thread::join(thread_handles[i]);
}
return 0;
}