| 订阅 | 在线投稿
分享
 
 
当前位置: 王朝网络 >> c/c++ >> Win32 xia liang zhong yong yu C++ de xian cheng tong bu lei ( shang )

Win32 xia liang zhong yong yu C++ de xian cheng tong bu lei ( shang )

2008-06-01 02:10:18 编辑來源:互联网 评论
 
 
 
本文为【Win32下两种用于C++的线程同步类(上)】的拼音翻译版
  xian cheng tong bu shi duo xian cheng cheng xu she ji de gai xin nei rong , ta de mu de shi zheng que chu li duo xian cheng bing fa shi de ge zhong wen ti , li ru xian cheng de deng dai 、 duo ge xian cheng fang wen tong yi shu ju shi de hu chi , fang si suo deng 。Win32 ti gong duo zhong nei gai dui xiang he shou duan yong yu xian cheng tong bu , ru hu chi liang 、 xin hao liang 、 shi jian 、 lin jie qu deng 。 suo bu tong de shi , hu chi liang 、 xin hao liang 、 shi jian dou shi Windows de nei gai dui xiang , dang cheng xu dui zhe xie dui xiang jin xing kong zhi shi hui zi dong zhuan huan dao gai xin tai , er lin jie qu ben shen bu shi nei gai dui xiang , ta shi gong zuo zai yong hu tai de 。 wo men zhi dao cong yong hu tai zhuan huan dao gai xin tai shi xu yao yi shi jian wei dai jia de , suo yi jia ru neng zai yong hu tai jiu jian dan jie jue de wen ti , jiu ke yi bu bi lao fan gai xin tai le 。
  zhe li wo yao shuo de shi liang zhong yong yu C++ de duo xian cheng tong bu lei , tong guo dui zhe liang zhong lei de shi yong jiu ke yi fang bian de shi xian dui bian liang huo dai ma duan de jia suo kong zhi , cong er fang zhi duo xian cheng dui bian liang bu zheng que de cao zuo 。
  suo wei jia suo , jiu shi shuo dang wo men yao fang wen mou yao hai bian liang zhi qian , dou xu yao shou xian huo de da ying cai neng ji xu , jia ru wei huo de da ying ze zhi you deng dai 。 yi ge yao hai bian liang yong you yi ba suo , yi ge xian cheng bi xu xian de dao zhe ba suo ( qi shi cheng wei yao shi ke neng geng xing xiang ) cai ke yi fang wen zhe ge bian liang , er dang mou ge bian liang chi you zhe ba suo de shi hou , qi ta xian cheng jiu bu neng chong fu de de dao ta , zhi you deng chi you suo de xian cheng ba suo gui huan yi hou qi ta xian cheng cai you ke neng de dao ta 。 zhi suo yi zhe yang zuo , jiu shi wei le fang zhi yi ge xian cheng du qu mou dui xiang tu zhong ling yi xian cheng dui ta jin xing le xiu gai , huo liang xian cheng tong shi dui yi bian liang jin xing xiu gai , li ru :
  // quan ju :
  strUCt MyStruct ... { int a, b; } ;
  MyStruct s;
  // xian cheng 1:
  int a = s.a;
  int b = s.b;
  // xian cheng 2:
  s.a ++ ;
  s.b -- ;
  jia ru shi ji de zhi xing shun xu jiu shi shang shu shu xie de shun xu na dao mei you shen me , dan jia ru xian cheng 2 de zhi xing da duan le xian cheng 1, bian wei ru xia shun xu :
  int a = s.a; // xian cheng 1
  s.a++; // xian cheng 2
  s.b++; // xian cheng 2
  int b = s.b; // xian cheng 1
  na me zhe shi xian cheng 1 du chu lai de a he b jiu hui you wen ti le , yin wei a shi zai xiu gai qian du de , er b shi zai xiu gai hou du de , zhe yang du chu lai de shi bu wan zheng de shu ju , hui dui cheng xu dai lai bu ke yu liao de hou guo 。 tian zhi dao liang ge cheng de tiao du shun xu shi shen me yang de 。 wei le fang zhi zhe zhong qing kuang de chu xian , xu yao dui bian liang s jia suo , ye jiu shi dang xian cheng 1 de dao suo yi hou jiu ke yi fang xin de fang wen s, zhe shi jia ru xian cheng 2 yao xiu gai s, zhi you deng xian cheng 1 fang wen wan cheng yi hou jiang suo shi fang cai ke yi , cong er bao zheng le shang shu liang xian cheng jiao cha fang wen bian liang de qing kuang bu hui chu xian 。
  shi yong Win32 ti gong de lin jie qu ke yi fang bian de shi xian zhe zhong suo :
  // quan ju :
  CRITICAL_SECTION cs;
  InitializeCriticalSection( & cs);
  // xian cheng 1:
  EnterCriticalSection( & cs);
  int a = s.a;
  int b = s.b;
  LeaveCriticalSection( & cs);
  // xian cheng 2:
  EnterCriticalSection( & cs);
  s.a ++ ;
  s.b -- ;
  LeaveCriticalSection( & cs);
  // zui hou :
  DeleteCriticalSection( & cs);
  dai ma zhong de lin jie qu bian liang (cs) jiu ke yi kan zuo shi bian liang s de suo , dang han shu EnterCriticalSection fan hui shi , dang qian xian cheng jiu huo de le zhe ba suo , zhi hou jiu shi dui bian liang de fang wen le 。 fang wen wan cheng hou , tiao yong LeaveCriticalSection biao shi shi fang zhe ba suo , da ying qi ta xian cheng ji xu shi yong ta 。
  jia ru mei dang xu yao dui yi ge bian liang jin xing jia suo shi dou xu yao zuo zhe xie cao zuo , xian de you xie ma fan , er qie bian liang cs yu s zhi you luo ji shang de suo guan xi , zai yu fa shang mei you shen me lian xi , zhe dui yu suo de zhi li dai lai le bu xiao de ma fan 。 cheng xu yuan zong shi zui lan de , ke yi xiang chu ge zhong tou lan de ban fa lai jie jue wen ti , li ru rang bei suo de bian liang yu jia suo de bian liang xing cheng wu li shang de lian xi , shi de suo bian liang cheng wei bei suo bian liang bu ke fen ge de yi bu fen , zhe ting qi lai shi ge hao zhu yi 。
  shou xian xiang dao de shi ba suo feng bi zai yi ge lei li , rang lei de gou zao han shu he xi gou han shu lai zhi li dui suo de chu shi hua he suo hui dong zuo , wo men cheng zhe ge suo wei “ shi li suo ”:
  class InstanceLockBase
  ... {
  CRITICAL_SECTION cs;
  protected :
  InstanceLockBase() ... { InitialCriticalSection( & cs); }
  ~ InstanceLockBase() ... { DeleteCriticalSection( & cs); }
  } ;
  jia ru shu xi C++, kan dao zhe li yi ding zhi dao hou mian wo yao gan shen me le , dui le , jiu shi ji xu , yin wei wo ba gou zao han shu he xi gou han shu dou sheng ming wei bao hu de (protected), zhe yang wei yi de zuo yong jiu shi zai zi lei li shi yong ta 。 rang wo men de bei bao hu shu ju cong zhe ge lei ji xu , na me ta men bu jiu bu ke fen ge le ma :
  struct MyStruct: public InstanceLockBase
  ... { … } ;
  shen me ? jie gou ti huan neng cong lei ji xu ? dang ran ,C++ zhong jie gou ti he lei chu le cheng yuan de mo ren fang wen kong zhi bu tong wai mei you shen me bu yi yang ,class neng zuo de struct ye neng zuo 。 ci wai , ye xu ni huan hui wen , jia ru bei suo de shi ge jian dan lei xing , bu neng ji xu zen me ban , na me yao me yong yi ge lei dui zhe ge jian dan lei xing jin xing feng zhuang ( ji de Java li you int he Integer ma ), yao me zhi hao shou gong zhi li ta men de lian xi le 。 jia ru bei suo lei yi jing you le ji lei ne ? mei guan xi ,C++ shi da ying duo ji xu de , duo yi ge ji lei ye mei shen me 。
  xian zai wo men de shu ju li mian yi jing bao han yi ba suo le , zhi hou jiu shi yao tian jia jia suo he jie suo de dong zuo , ba ta men zuo wei InstanceLockBase lei de cheng yuan han shu zai he shi bu guo le :
  class InstanceLockBase
  ... {
  CRITICAL_SECTION cs;
  void Lock() ... { EnterCriticalSection( & cs); }
  void Unlock() ... { LeaveCriticalSection( & cs); }
  …
  } ;
  kan dao zhe li ke neng hui fa xian , wo ba Lock he Unlock han shu dou sheng ming wei si you le , na me ru he fang wen zhe liang ge han shu ne ? shi de , wo men zong shi xu yao you yi ge di fang lai tiao yong zhe liang ge han shu yi shi xian jia suo he jie suo de , er qie ta men zong ying gai cheng dui chu xian , dan C++ yu fa ben shen mei neng xian zhi wo men bi xu cheng dui de tiao yong liang ge han shu , jia ru jia wan suo wang le jie , na hou guo shi yan chong de 。 zhe li you yi ge li wai , jiu shi C++ dui yu gou zao han shu he xi gou han shu de tiao yong shi zi dong cheng dui de , dui le , na jiu ba dui Lock he Unlock de tiao yong zhuan men xie zai yi ge lei de gou zao han shu he xi gou han shu zhong :
  
  
  class InstanceLock
  ... {
  InstanceLockBase * _pObj;
  public :
  InstanceLock(InstanceLockBase * pObj)
  ... {
  _pObj = pObj; // zhe li hui bao cun yi fen zhi xiang s de zhi zhen , yong yu jie suo
  if (NULL != _pObj)
  _pObj -> Lock(); // zhe li jia suo
  }
  ~ InstanceLock()
  ... {
  if (NULL != _pObj)
  _pObj -> Unlock(); // zhe li jie suo
  }
  } ;
  zui hou bie wang le zai lei InstanceLockBase zhong ba InstanceLock sheng ming wei you yuan , shi de ta neng zheng que fang wen Lock he Unlock zhe liang ge si you han shu :
  class InstanceLockBase
  ... {
  friend class InstanceLock;
  …
  } ;
  hao le , you le shang mian de ji chu , xian zai dui bian liang s de jia jie suo zhi li bian cheng le dui InstanceLock de shi li de sheng ming zhou qi de zhi li le 。 jia ru wo men you yi ge han shu ModifyS zhong yao dui s jin xing xiu gai , na me zhi yao zai han shu yi kai shi jiu sheng ming yi ge InstaceLock de shi li , zhe yang zheng ge han shu jiu zi dong dui s jia suo , yi dan jin ru zhe ge han shu , qi ta xian cheng jiu dou bu neng huo de s de suo le :
  void ModifyS()
  ... {
  InstanceLock lock ( & s); // zhe li yi jing shi xian jia suo le
  // some operations on s
  } // yi dan li kai lock dui xiang de zuo yong yu , zi dong jie suo
  jia ru shi yao dui mou han shu zhong yi bu fen dai ma jia suo , zhi yao yong yi dui da kuo hao ba ta men kuo qi lai zai sheng ming yi ge lock jiu ke yi le :
  …
  ... {
  InstanceLock lock ( & s);
  // do something …
  }
  …
  hao le , jiu shi zhe me jian dan 。 xia mian lai kan yi ge ce shi 。
  shou xian yu bei yi ge shu chu han shu , dui wo men li jie cheng xu you bang zhu 。 ta hui zai shu chu wo men xiang shu chu de nei rong tong shi da chu xing hao he shi jian :
  void Say( char * text)
  ... {
  static int count = 0 ;
  SYSTEMTIME st;
  ::GetLocalTime( & st);
  printf( " %03d [%02d:%02d:%02d.%03d]%s " , ++ count, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds, text);
  }
  dang ran , yuan ze shang dang duo xian cheng dou tiao yong zhe ge han shu shi ying gai dui qi jing tai ju bu bian liang count jin xing jia suo , zhe li jiu sheng lue le 。
  wo men sheng ming yi ge fei chang jian dan de bei suo de lei xing , bing sheng cheng yi ge shi li :
  class MyClass: public InstanceLockBase
  ... {} ;
  MyClass mc;
  zi xian cheng de ren wu jiu shi dui zhe ge dui xiang jia suo , ran hou shu chu yi xie xin xi :
  DWord CALLBACK ThreadProc(LPVOID param)
  ... {
  InstanceLock il( & mc);
  Say( " in sub thread, lock " );
  Sleep( 2000 );
  Say( " in sub thread, unlock " );
  return 0 ;
  }
  zhe li hui shu chu liang tiao xin xi , yi shi zai gang gang huo de suo de shi jian , er shi zai shi fang suo de shi hou , zhong jian tong guo Sleep lai yan chi 2 miao 。
  zhu xian cheng fu ze kai qi zi xian cheng , ran hou ye dui mc jia suo :
  CreateThread( 0 , 0 , ThreadProc, 0 , 0 , 0 );
  ... {
  InstanceLock il( & mc);
  Say( " in main thread, lock " );
  Sleep( 3000 );
  Say( " in main thread, lock " );
  }
  yun xing ci cheng xu , de dao de shu chu ru xia :
  001 [13:43:23.781]in main thread, lock
  002 [13:43:26.781]in main thread, lock
  003 [13:43:26.781]in sub thread, lock
  004 [13:43:28.781]in sub thread, unlock
  cong qi shu chu de xing hao he shi jian ke yi qing chu de kan dao liang ge xian cheng jian de hu chi : dang zhu xian cheng qia hao shou xian huo de suo shi , ta hui yan chi 3 miao , ran hou shi fang suo , zhi hou zi xian cheng cai de yi ji xu jin xing 。 zhe ge li zi ye zheng shi wo men de lei gong zuo de hen hao 。
  zong jie yi xia , yao shi yong InstanceLock xi lie lei , yao zuo de jiu shi :
  1、 rang bei suo lei cong InstanceLockBase ji xu
  2、 suo you yao fang wen bei suo dui xiang de dai ma qian mian sheng ming InstanceLock de shi li , bing chuan ru bei suo dui xiang de zhi zhen 。
  fu : wan zheng yuan dai ma :
  #pragma once
  #include < windows.h >
  class InstanceLock;
  class InstanceLockBase
  ... {
  friend class InstanceLock;
  CRITICAL_SECTION cs;
  void Lock()
  ... {
  ::EnterCriticalSection( & cs);
  }
  void Unlock()
  ... {
  ::LeaveCriticalSection( & cs);
  }
  protected :
  InstanceLockBase()
  ... {
  ::InitializeCriticalSection( & cs);
  }
  ~ InstanceLockBase()
  ... {
  ::DeleteCriticalSection( & cs);
  }
  } ;
  
   class InstanceLock
  ... {
  InstanceLockBase * _pObj;
  public :
  InstanceLock(InstanceLockBase * pObj)
  ... {
  _pObj = pObj;
  if (NULL != _pObj)
  _pObj -> Lock();
  }
  ~ InstanceLock()
  ... {
  if (NULL != _pObj)
  _pObj -> Unlock();
  }
  } ;【原文】【汉音对照
 
 
 
 
 
 
 
 
 
日版宠物情人插曲《Winding Road》歌词

日版宠物情人2017的插曲,很带节奏感,日语的,女生唱的。 最后听见是在第8集的时候女主手割伤了,然后男主用嘴帮她吸了一下,插曲就出来了。 歌手:Def...

兄弟共妻,我成了他们夜里的美食

老钟家的两个儿子很特别,就是跟其他的人不太一样,魔一般的执着。兄弟俩都到了要结婚的年龄了,不管自家老爹怎么磨破嘴皮子,兄弟俩说不娶就不娶,老父母为兄弟两操碎了心...

如何磨出破洞牛仔裤?牛仔裤怎么剪破洞?

把牛仔裤磨出有线的破洞 1、具体工具就是磨脚石,下面垫一个硬物,然后用磨脚石一直磨一直磨,到把那块磨薄了,用手撕开就好了。出来的洞啊很自然的。需要猫须的话调几...

我就是扫描下图得到了敬业福和爱国福

先来看下敬业福和爱国福 今年春节,支付宝再次推出了“五福红包”活动,表示要“把欠大家的敬业福都还给大家”。 今天该活动正式启动,和去年一样,需要收集“五福”...

冰箱异味产生的原因和臭味去除的方法

有时候我们打开冰箱就会闻到一股异味,冰箱里的这种异味是因为一些物质发出的气味的混合体,闻起来让人恶心。 产生这些异味的主要原因有以下几点。 1、很多人有这种习...

 
 
xian cheng tong bu shi duo xian cheng cheng xu she ji de gai xin nei rong , ta de mu de shi zheng que chu li duo xian cheng bing fa shi de ge zhong wen ti , li ru xian cheng de deng dai 、 duo ge xian cheng fang wen tong yi shu ju shi de hu chi , fang si suo deng 。Win32 ti gong duo zhong nei gai dui xiang he shou duan yong yu xian cheng tong bu , ru hu chi liang 、 xin hao liang 、 shi jian 、 lin jie qu deng 。 suo bu tong de shi , hu chi liang 、 xin hao liang 、 shi jian dou shi Windows de nei gai dui xiang , dang cheng xu dui zhe xie dui xiang jin xing kong zhi shi hui zi dong zhuan huan dao gai xin tai , er lin jie qu ben shen bu shi nei gai dui xiang , ta shi gong zuo zai yong hu tai de 。 wo men zhi dao cong yong hu tai zhuan huan dao gai xin tai shi xu yao yi shi jian wei dai jia de , suo yi jia ru neng zai yong hu tai jiu jian dan jie jue de wen ti , jiu ke yi bu bi lao fan gai xin tai le 。    zhe li wo yao shuo de shi liang zhong yong yu C++ de duo xian cheng tong bu lei , tong guo dui zhe liang zhong lei de shi yong jiu ke yi fang bian de shi xian dui bian liang huo dai ma duan de jia suo kong zhi , cong er fang zhi duo xian cheng dui bian liang bu zheng que de cao zuo 。    suo wei jia suo , jiu shi shuo dang wo men yao fang wen mou yao hai bian liang zhi qian , dou xu yao shou xian huo de da ying cai neng ji xu , jia ru wei huo de da ying ze zhi you deng dai 。 yi ge yao hai bian liang yong you yi ba suo , yi ge xian cheng bi xu xian de dao zhe ba suo ( qi shi cheng wei yao shi ke neng geng xing xiang ) cai ke yi fang wen zhe ge bian liang , er dang mou ge bian liang chi you zhe ba suo de shi hou , qi ta xian cheng jiu bu neng chong fu de de dao ta , zhi you deng chi you suo de xian cheng ba suo gui huan yi hou qi ta xian cheng cai you ke neng de dao ta 。 zhi suo yi zhe yang zuo , jiu shi wei le fang zhi yi ge xian cheng du qu mou dui xiang tu zhong ling yi xian cheng dui ta jin xing le xiu gai , huo liang xian cheng tong shi dui yi bian liang jin xing xiu gai , li ru : // quan ju : strUCt MyStruct ... { int a, b; } ; MyStruct s; // xian cheng 1: int a = s.a; int b = s.b; // xian cheng 2: s.a ++ ; s.b -- ;    jia ru shi ji de zhi xing shun xu jiu shi shang shu shu xie de shun xu na dao mei you shen me , dan jia ru xian cheng 2 de zhi xing da duan le xian cheng 1, bian wei ru xia shun xu : int a = s.a; // xian cheng 1 s.a++; // xian cheng 2 s.b++; // xian cheng 2 int b = s.b; // xian cheng 1    na me zhe shi xian cheng 1 du chu lai de a he b jiu hui you wen ti le , yin wei a shi zai xiu gai qian du de , er b shi zai xiu gai hou du de , zhe yang du chu lai de shi bu wan zheng de shu ju , hui dui cheng xu dai lai bu ke yu liao de hou guo 。 tian zhi dao liang ge cheng de tiao du shun xu shi shen me yang de 。 wei le fang zhi zhe zhong qing kuang de chu xian , xu yao dui bian liang s jia suo , ye jiu shi dang xian cheng 1 de dao suo yi hou jiu ke yi fang xin de fang wen s, zhe shi jia ru xian cheng 2 yao xiu gai s, zhi you deng xian cheng 1 fang wen wan cheng yi hou jiang suo shi fang cai ke yi , cong er bao zheng le shang shu liang xian cheng jiao cha fang wen bian liang de qing kuang bu hui chu xian 。    shi yong Win32 ti gong de lin jie qu ke yi fang bian de shi xian zhe zhong suo : // quan ju : CRITICAL_SECTION cs; InitializeCriticalSection( & cs); // xian cheng 1: EnterCriticalSection( & cs); int a = s.a; int b = s.b; LeaveCriticalSection( & cs); // xian cheng 2: EnterCriticalSection( & cs); s.a ++ ; s.b -- ; LeaveCriticalSection( & cs); // zui hou : DeleteCriticalSection( & cs);    dai ma zhong de lin jie qu bian liang (cs) jiu ke yi kan zuo shi bian liang s de suo , dang han shu EnterCriticalSection fan hui shi , dang qian xian cheng jiu huo de le zhe ba suo , zhi hou jiu shi dui bian liang de fang wen le 。 fang wen wan cheng hou , tiao yong LeaveCriticalSection biao shi shi fang zhe ba suo , da ying qi ta xian cheng ji xu shi yong ta 。    jia ru mei dang xu yao dui yi ge bian liang jin xing jia suo shi dou xu yao zuo zhe xie cao zuo , xian de you xie ma fan , er qie bian liang cs yu s zhi you luo ji shang de suo guan xi , zai yu fa shang mei you shen me lian xi , zhe dui yu suo de zhi li dai lai le bu xiao de ma fan 。 cheng xu yuan zong shi zui lan de , ke yi xiang chu ge zhong tou lan de ban fa lai jie jue wen ti , li ru rang bei suo de bian liang yu jia suo de bian liang xing cheng wu li shang de lian xi , shi de suo bian liang cheng wei bei suo bian liang bu ke fen ge de yi bu fen , zhe ting qi lai shi ge hao zhu yi 。    shou xian xiang dao de shi ba suo feng bi zai yi ge lei li , rang lei de gou zao han shu he xi gou han shu lai zhi li dui suo de chu shi hua he suo hui dong zuo , wo men cheng zhe ge suo wei “ shi li suo ”: class InstanceLockBase ... { CRITICAL_SECTION cs; protected : InstanceLockBase() ... { InitialCriticalSection( & cs); } ~ InstanceLockBase() ... { DeleteCriticalSection( & cs); } } ;    jia ru shu xi C++, kan dao zhe li yi ding zhi dao hou mian wo yao gan shen me le , dui le , jiu shi ji xu , yin wei wo ba gou zao han shu he xi gou han shu dou sheng ming wei bao hu de (protected), zhe yang wei yi de zuo yong jiu shi zai zi lei li shi yong ta 。 rang wo men de bei bao hu shu ju cong zhe ge lei ji xu , na me ta men bu jiu bu ke fen ge le ma : struct MyStruct: public InstanceLockBase ... { … } ;    shen me ? jie gou ti huan neng cong lei ji xu ? dang ran ,C++ zhong jie gou ti he lei chu le cheng yuan de mo ren fang wen kong zhi bu tong wai mei you shen me bu yi yang ,class neng zuo de struct ye neng zuo 。 ci wai , ye xu ni huan hui wen , jia ru bei suo de shi ge jian dan lei xing , bu neng ji xu zen me ban , na me yao me yong yi ge lei dui zhe ge jian dan lei xing jin xing feng zhuang ( ji de Java li you int he Integer ma ), yao me zhi hao shou gong zhi li ta men de lian xi le 。 jia ru bei suo lei yi jing you le ji lei ne ? mei guan xi ,C++ shi da ying duo ji xu de , duo yi ge ji lei ye mei shen me 。    xian zai wo men de shu ju li mian yi jing bao han yi ba suo le , zhi hou jiu shi yao tian jia jia suo he jie suo de dong zuo , ba ta men zuo wei InstanceLockBase lei de cheng yuan han shu zai he shi bu guo le : class InstanceLockBase ... {  CRITICAL_SECTION cs;  void Lock() ... { EnterCriticalSection( & cs); }  void Unlock() ... { LeaveCriticalSection( & cs); }  … } ;    kan dao zhe li ke neng hui fa xian , wo ba Lock he Unlock han shu dou sheng ming wei si you le , na me ru he fang wen zhe liang ge han shu ne ? shi de , wo men zong shi xu yao you yi ge di fang lai tiao yong zhe liang ge han shu yi shi xian jia suo he jie suo de , er qie ta men zong ying gai cheng dui chu xian , dan C++ yu fa ben shen mei neng xian zhi wo men bi xu cheng dui de tiao yong liang ge han shu , jia ru jia wan suo wang le jie , na hou guo shi yan chong de 。 zhe li you yi ge li wai , jiu shi C++ dui yu gou zao han shu he xi gou han shu de tiao yong shi zi dong cheng dui de , dui le , na jiu ba dui Lock he Unlock de tiao yong zhuan men xie zai yi ge lei de gou zao han shu he xi gou han shu zhong : class InstanceLock ... {  InstanceLockBase * _pObj;  public :   InstanceLock(InstanceLockBase * pObj)   ... {    _pObj = pObj; // zhe li hui bao cun yi fen zhi xiang s de zhi zhen , yong yu jie suo    if (NULL != _pObj)    _pObj -> Lock(); // zhe li jia suo   }   ~ InstanceLock()   ... {    if (NULL != _pObj)    _pObj -> Unlock(); // zhe li jie suo  } } ;    zui hou bie wang le zai lei InstanceLockBase zhong ba InstanceLock sheng ming wei you yuan , shi de ta neng zheng que fang wen Lock he Unlock zhe liang ge si you han shu : class InstanceLockBase ... {  friend class InstanceLock;  … } ;    hao le , you le shang mian de ji chu , xian zai dui bian liang s de jia jie suo zhi li bian cheng le dui InstanceLock de shi li de sheng ming zhou qi de zhi li le 。 jia ru wo men you yi ge han shu ModifyS zhong yao dui s jin xing xiu gai , na me zhi yao zai han shu yi kai shi jiu sheng ming yi ge InstaceLock de shi li , zhe yang zheng ge han shu jiu zi dong dui s jia suo , yi dan jin ru zhe ge han shu , qi ta xian cheng jiu dou bu neng huo de s de suo le : void ModifyS() ... {  InstanceLock lock ( & s); // zhe li yi jing shi xian jia suo le  // some operations on s } // yi dan li kai lock dui xiang de zuo yong yu , zi dong jie suo    jia ru shi yao dui mou han shu zhong yi bu fen dai ma jia suo , zhi yao yong yi dui da kuo hao ba ta men kuo qi lai zai sheng ming yi ge lock jiu ke yi le : … ... {  InstanceLock lock ( & s);  // do something … } …    hao le , jiu shi zhe me jian dan 。 xia mian lai kan yi ge ce shi 。    shou xian yu bei yi ge shu chu han shu , dui wo men li jie cheng xu you bang zhu 。 ta hui zai shu chu wo men xiang shu chu de nei rong tong shi da chu xing hao he shi jian : void Say( char * text) ... {  static int count = 0 ;  SYSTEMTIME st;  ::GetLocalTime( & st);  printf( " %03d [%02d:%02d:%02d.%03d]%s " , ++ count, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds, text); }    dang ran , yuan ze shang dang duo xian cheng dou tiao yong zhe ge han shu shi ying gai dui qi jing tai ju bu bian liang count jin xing jia suo , zhe li jiu sheng lue le 。    wo men sheng ming yi ge fei chang jian dan de bei suo de lei xing , bing sheng cheng yi ge shi li : class MyClass: public InstanceLockBase ... {} ; MyClass mc;    zi xian cheng de ren wu jiu shi dui zhe ge dui xiang jia suo , ran hou shu chu yi xie xin xi : DWord CALLBACK ThreadProc(LPVOID param) ... {  InstanceLock il( & mc);  Say( " in sub thread, lock " );  Sleep( 2000 );  Say( " in sub thread, unlock " );  return 0 ; }    zhe li hui shu chu liang tiao xin xi , yi shi zai gang gang huo de suo de shi jian , er shi zai shi fang suo de shi hou , zhong jian tong guo Sleep lai yan chi 2 miao 。    zhu xian cheng fu ze kai qi zi xian cheng , ran hou ye dui mc jia suo : CreateThread( 0 , 0 , ThreadProc, 0 , 0 , 0 ); ... {  InstanceLock il( & mc);  Say( " in main thread, lock " );  Sleep( 3000 );  Say( " in main thread, lock " ); }    yun xing ci cheng xu , de dao de shu chu ru xia : 001 [13:43:23.781]in main thread, lock 002 [13:43:26.781]in main thread, lock 003 [13:43:26.781]in sub thread, lock 004 [13:43:28.781]in sub thread, unlock    cong qi shu chu de xing hao he shi jian ke yi qing chu de kan dao liang ge xian cheng jian de hu chi : dang zhu xian cheng qia hao shou xian huo de suo shi , ta hui yan chi 3 miao , ran hou shi fang suo , zhi hou zi xian cheng cai de yi ji xu jin xing 。 zhe ge li zi ye zheng shi wo men de lei gong zuo de hen hao 。    zong jie yi xia , yao shi yong InstanceLock xi lie lei , yao zuo de jiu shi :   1、 rang bei suo lei cong InstanceLockBase ji xu   2、 suo you yao fang wen bei suo dui xiang de dai ma qian mian sheng ming InstanceLock de shi li , bing chuan ru bei suo dui xiang de zhi zhen 。    fu : wan zheng yuan dai ma : #pragma once #include < windows.h > class InstanceLock; class InstanceLockBase ... {  friend class InstanceLock;  CRITICAL_SECTION cs;  void Lock()  ... {   ::EnterCriticalSection( & cs);  }  void Unlock()  ... {   ::LeaveCriticalSection( & cs);  }  protected :  InstanceLockBase()  ... {   ::InitializeCriticalSection( & cs);  }  ~ InstanceLockBase()  ... {   ::DeleteCriticalSection( & cs);  } } ; class InstanceLock ... {  InstanceLockBase * _pObj;  public :   InstanceLock(InstanceLockBase * pObj)   ... {    _pObj = pObj;    if (NULL != _pObj)     _pObj -> Lock();   }  ~ InstanceLock()  ... {   if (NULL != _pObj)    _pObj -> Unlock();  } } ;
󰈣󰈤
  免责声明:本文仅代表作者个人观点,与王朝网络无关。王朝网络登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
情人节的清纯女生(9)
情人节的清纯女生(8)
情人节的清纯女生(7)
情人节的清纯女生(6)
山东蓬莱海边组照
一探哲蚌 II
一探哲蚌 III
古长城的一角
 
>>返回首页<<
 
 
 为你推荐
 
 
 
 转载本文
 UBB代码 HTML代码
复制到剪贴板...
 
 热帖排行
 
 
 
 
 
©2005- 王朝网络 版权所有