winston 发表于 2011-12-31 19:52:27

ace 锁

来自:http://www.cppblog.com/arrow8209/archive/2011/12/31/144410.html
在网络编程中多线程是一个必用一种技术,但是在初步时候多线程的时候一些开发者会遇到这样或那样的问题,其实这些问题我个人感觉主要是集中数据和线程同步上。ACE的“锁”机制完全可以解决这些问题。
首先我们讲讲什么是“锁”,在现实中我们需要把一些东西保护起来不让别人使用,我们怎么办呢?这个时候我们会用一个箱子把这些东西锁起来,防止别人使用。线程的“锁”就是把一部分代码保护起来,不要别人访问它,除非我们把箱子打开。

ace的锁主要分三类,“读写锁”“线程锁”“进程锁”。
“读写锁”
ACE_RW_Mutex:在我们对数据进行“读”和“写”的时候保护数据不被其他人读写,说到这里我必须提一下c++中所有的类型如:vector、list、map等都不是安全的,我们在进行多线程编程是要用读写锁来控制这些类型的读写。

首先我们来看看下面这个例子

http://www.cppblog.com/Images/OutliningIndicators/None.gifstatic ACE_RW_Mutex g_RW_Mutex;
http://www.cppblog.com/Images/OutliningIndicators/None.gif
http://www.cppblog.com/Images/OutliningIndicators/None.gifint g_Cout =
0;
http://www.cppblog.com/Images/OutliningIndicators/None.gif
http://www.cppblog.com/Images/OutliningIndicators/None.gifDWORD WINAPI Fun1Write1(LPVOID lpParameter)//thread data
http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gifhttp://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gifhttp://www.cppblog.com/Images/dot.gif{
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif//g_RW_Mutex.acquire_write();
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif
while(g_Cout <
5)
http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttp://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif http://www.cppblog.com/Images/dot.gif{
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gifACE_OS::sleep(1);//为了突出效果所以我这里暂停一秒
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gifg_Cout++;
http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif }
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif//g_RW_Mutex.release();
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif std::cout <<
"插入完成"
<<std::endl;
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif return
0;
http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif}
http://www.cppblog.com/Images/OutliningIndicators/None.gif
http://www.cppblog.com/Images/OutliningIndicators/None.gifint _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gifhttp://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gifhttp://www.cppblog.com/Images/dot.gif{
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif HANDLE hThread1;
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif HANDLE hThread2;
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif //创建线程
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif hThread1=CreateThread(NULL,0,Fun1Write1,NULL,0,NULL);
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif hThread2=CreateThread(NULL,0,Fun1Write2,NULL,0,NULL);
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif getchar();
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif std::cout << g_Cout << std::endl;
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif CloseHandle(hThread1);
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif CloseHandle(hThread2);
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif getchar();
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif return
0;
http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif}
http://www.cppblog.com/Images/OutliningIndicators/None.gif
http://www.cppblog.com/Images/OutliningIndicators/None.gif


最后g_Count的结果是6.这就是数据同步的问题。
如果我们把注释的地方打开g_Count的结果就是5

为什么g_Count的结果为6就由读者自己来分析吧。
我来解释一下为把注释的地方打开后g_Count的结果会是5:g_RW_Mutex我们可以理解为一个锁
当线程执行到g_RW_Mutex.acquire_write()首先判断这个锁是不是被其他线程获取了,如果没有被其他线程使用,那么其他的线程执行到这里的时候就不能继续执行,除非调用了g_RW_Mutex.release()释放这个锁,那么其他的线程才能继续执行,在其他线程继续执行的时候同样要遵守

ztenv 发表于 2012-1-30 14:14:59

嗯,ACE中有很多种锁,使用起来非常的方便,不过也有陷阱值得注意。
页: [1]
查看完整版本: ace 锁