{"rsdb":{"rid":"158062","subhead":"","postdate":"0","aid":"118510","fid":"49","uid":"1","topic":"1","content":"
\n C++<\/a>\u4e4b\u5728\u8d44\u6e90\u7ba1\u7406\u7c7b\u4e2d\u5c0f\u5fc3copying\u884c\u4e3a\uff0814\uff09---\u300aEffective C++<\/a>\u300b \n
\n

\u7b2c\u4e00\u8282 <\u80cc\u666f><\/strong>
\u6761\u6b3e13\u4e2d\u8bb2\u5230\u201c\u8d44\u6e90\u53d6\u5f97\u7684\u65f6\u673a\u4fbf\u662f\u521d\u59cb\u5316\u65f6\u673a\u201d\u5e76\u7531\u6b64\u5f15\u51fa\u201c\u4ee5\u5bf9\u8c61\u7ba1\u7406\u8d44\u6e90\u201d\u7684\u6982\u5ff5\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\u4f7f\u7528std\u4e2d\u7684auto_ptr(\u667a\u80fd\u6307\u9488)\u548ctr1::shared_ptr(\u5f15\u6570\u667a\u80fd\u6307\u9488)\u4f5c\u4e3a\u7ba1\u7406\u8d44\u6e90\u7684\u5bf9\u8c61\u3002
\u4e8b\u5b9e\u4e0a\uff0c\u8fd9\u79cd\u7ba1\u7406\u65b9\u6cd5\u5341\u5206\u6709\u6548\u3002\u4f46\u662f\uff0cauto_ptr\u548ctr1::shared_ptr\u53ea\u80fd\u7ba1\u7406\u57fa\u4e8e\u5806(heap-based)\u7684\u8d44\u6e90\uff0c\u800c\u975eheap-based\u7684\u8d44\u6e90\u5374\u5f80\u5f80\u4e0d\u9002\u5408\u3002
\u56e0\u6b64\uff0c\u6709\u7684\u65f6\u5019\u4f60\u9700\u8981\u5efa\u7acb\u81ea\u5df1\u7684\u8d44\u6e90\u7ba1\u7406\u7c7b\u3002\u672c\u6587\u4ecb\u7ecd\u7684\u5185\u5bb9\u662f\u5728\u4f60\u5efa\u7acb\u81ea\u5df1\u7684\u8d44\u6e90\u7ba1\u7406\u7c7b\u65f6\u5e94\u8be5\u6ce8\u610f\u7684\u4e8b\u9879\u3002<\/p> \n

\u7b2c\u4e8c\u8282 <\u6b63\u6587><\/strong>
\u6211\u4eec\u77e5\u9053\u5728C API\u4e2d\u5904\u7406Mutex\u7684\u4e92\u65a5\u5bf9\u8c61\uff0c\u6709lock\u4f55unlock\u4e24\u4e2a\u51fd\u6570\u53ef\u7528\uff1a<\/p> \n

\r\n  void lock(Mutex* pm);            \/\/ \u9501\u5b9apm\u6307\u5411\u7684\u4e92\u65a5\u91cf\r\n  void unlock(Mutex* pm);          \/\/ pm\u6307\u5411\u7684\u4e92\u65a5\u91cf\u89e3\u9501<\/pre> \n  

\u5047\u8bbe\u6211\u4eec\u5199\u4e86Lock\u7c7b\u6765\u7ba1\u7406\u9501\u3002<\/p> \n

\r\nclass  Mutex{\r\npublic:\r\n    Mutex():Count(0){}\r\npublic:\r\n    int Count;\r\n};\r\nvoid lock(Mutex* pm){pm->Count++;}\r\nvoid unlock(Mutex* pm){pm->Count--;}\r\nclass Lock\r\n{\r\npublic:\r\n    explicit Lock(Mutex* pm):mutexPtr(pm)\r\n    {lock(mutexPtr);}          \/\/ \u5c06mutexPtr\u6307\u5411\u7684\u4e92\u65a5\u53d8\u91cf\u52a0\u9501\r\n    ~Lock(){unlock(mutexPtr);} \/\/ \u5c06mutexPtr\u6307\u5411\u7684\u4e92\u65a5\u53d8\u91cf\u89e3\u9501\r\nprivate :\r\n    Mutex * mutexPtr;\r\n};<\/pre> \n  

\u4e0a\u9762\u4ee3\u7801\u6ee1\u8db3RAII(Resource Acquisition is Initialization)\u539f\u5219\u5373\uff0c\u8d44\u6e90\u5728\u83b7\u53d6\u65f6\u65e2\u662f\u521d\u59cb\u5316\u65f6\uff0c\u5931\u53bb\u65f6\u65e2\u662f\u6e05\u7406\u65f6\u3002
\u60f3\u8c61\u4e0b\u9762\u7684\u573a\u666f\u65f6\uff0c\u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u662f\u4ec0\u4e48\u3002<\/p> \n

\r\n1     Mutex m;\r\n2     cout << "Mutex is " << m.Count << endl;\r\n3     Lock m1(&m);\r\n4     cout << "Mutex is " << m.Count << endl;\r\n5     Lock m2(m1);\r\n6     cout << "Mutex is " << m.Count << endl;\r\n7     m1.~Lock();\r\n8     cout << "Mutex is " << m.Count << endl;<\/pre> \n  

\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p> \n

\r\nMutex is 0\r\nMutex is 1\r\nMutex is 1\r\nMutex is 0<\/pre> \n  

\u8fd9\u662f\u4e3a\u4ec0\u4e48\u5462\uff1f\u524d\u4e24\u4e2a0\u548c1\u8f93\u51fa\u65e0\u53ef\u539a\u975e\uff0c\u7b2c\u4e09\u4e2a\u7684\u8f93\u51fa\u4e3a\u62ffm1\u4f5c\u4e3a\u5b9e\u4f8b\u5bf9\u8c61\u53bb\u8d4b\u503c\u7ed9m2\uff0c\u64cd\u4f5c\u5bf9\u8c61\u4e3am1\uff0c\u4e0d\u4f1a\u76f4\u63a5\u5f71\u54cdm\uff1b\u7b2c\u56db\u4e2a\u4e92\u65a5\u91cfm\u7684\u7ba1\u7406\u8005m1\u88ab\u9500\u6bc1\u4e86\uff0c\u90a3\u4e48m\u4e5f\u5c31\u88ab\u89e3\u9501\u4e86\u3002<\/p> \n

\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0cm\u7684\u503c\u4e0d\u65ad\u88ab\u53d8\u66f4\uff0c\u663e\u7136\uff0c\u8fd9\u79cd\u8d44\u6e90\u7684\u7ba1\u7406\u7684\u65b9\u5f0f\u662f\u4e0d\u5408\u7406\u7684\u3002<\/p> \n

\u53ef\u80fd\u7684\u89e3\u51b3\u65b9\u6cd5:<\/p> \n

1.\u7981\u6b62\u590d\u5236\u3002\u7981\u6b62\u590d\u5236\u7684\u505a\u6cd5\u5177\u4f53\u7684\u53ef\u53c2\u7167\u6761\u6b3e6\u7684\u8bf4\u660e\u3002<\/p> \n

\r\nclass UnCopyable {\r\npublic:\r\n     UnCopyable(){}\r\nprivate:\r\n     UnCopyable(const UnCopyable& ths) {\r\n     }\r\n};\r\nclass Lock:private UnCopyable {\r\n    ...\r\n}<\/pre> \n  

2.\u4f7f\u7528\u5f15\u7528\u8ba1\u6570\u667a\u80fd\u6307\u9488:tr1::shared_ptr\u3002<\/p> \n

\u4ece\u6761\u6b3e13\u6211\u4eec\u5df2\u7ecf\u77e5\u9053\u5f15\u7528\u8ba1\u6570\u667a\u80fd\u6307\u9488\u4f1a\u8ddf\u8e2a\u4f7f\u7528\u8be5\u8d44\u6e90\u7684\u6240\u6709\u5bf9\u8c61\u6570\uff0c\u8ba1\u6570\u4e3a0\u65f6\uff0c\u8d44\u6e90\u4f1a\u88ab\u5220\u9664\u3002\u6ce8\u610f\uff0c\u8fd9\u91cc\u5220\u9664\u4e92\u65a5\u91cfm\u4e0d\u662f\u6211\u4eec\u6240\u671f\u5f85\u7684\uff0c\u6211\u4eec\u671f\u5f85\u662f\u89e3\u9501\u4e92\u65a5\u91cf<\/p> \n

\u5e78\u8fd0\u7684\u662ftr1::shared_ptr\u5141\u8bb8\u81ea\u5b9a\u4e49\u6240\u8c13\u7684\u201c\u5220\u9664\u201d\u52a8\u4f5c\uff0c\u8be5\u52a8\u4f5c\u662f\u5728\u8ba1\u6570\u4e3a0\u65f6\u6267\u884c\u7684\u3002\u4e8e\u662f\u7c7bLock\u53ef\u4ee5\u662f\u4e0b\u9762\u7684\u6837\u5b50\u3002<\/p> \n

\r\nclass Lock\r\n{\r\npublic:\r\n     explicit Lock(Mutex* pm):mutexPtr(pm,unlock)\r\n     {lock(mutexPtr.get());}          \/\/ \u5c06mutexPtr\u6307\u5411\u7684\u4e92\u65a5\u53d8\u91cf\u52a0\u9501\r\nprivate :\r\n     shared_ptr\n   \n     mutexPtr;\r\n};\n   <\/mutex><\/pre> \n  

\u6709\u6ca1\u6709\u53d1\u89c9\u8c8c\u4f3c\u5c11\u4e86\u70b9\u4e1c\u897f\uff1f\u5bf9\uff0c\u6790\u6784\u51fd\u6570\u6ca1\u6709\u4e86\u3002\u56e0\u4e3ashare_ptr\u4f1a\u5e2e\u4f60\u5b8c\u6210\u8fd9\u4e00\u5de5\u4f5c\u3002<\/p> \n

3.\u590d\u5236\u7ba1\u7406\u5bf9\u8c61\u65f6\u4e5f\u590d\u5236\u6240\u7ba1\u7406\u7684\u8d44\u6e90\u3002<\/p> \n

\u8bf7\u56de\u5934\u60f3\u4e00\u4e2a\u95ee\u9898\uff1a\u4e3a\u4ec0\u4e48\u9700\u8981\u81ea\u5df1\u7684\u8d44\u6e90\u7ba1\u7406\u7c7b\uff1f\u90a3\u4e48\uff0c\u53ef\u80fd\u7684\u7406\u7531\u662f\u5f53\u4e0d\u9700\u8981\u67d0\u4e2a\u8d44\u6e90\u65f6\uff0c\u8d44\u6e90\u80fd\u88ab\u6b63\u5e38\u91ca\u653e\uff08\u5220\u9664\uff0c\u5176\u4ed6\u52a8\u4f5c\uff09\u3002\u8d44\u6e90\u5b58\u5728\u591a\u4e2a\u590d\u4ef6\u5e76\u4e0d\u53ef\u6015\uff0c\u53ef\u6015\u7684\u662f\u590d\u4ef6\u5728\u8be5\u9500\u6bc1\u7684\u65f6\u5019\u5374\u6ca1\u6709\u9500\u6bc1\u3002\u4e5f\u5c31\u662f\uff0c\u7ba1\u7406\u5bf9\u8c61\u4e0e\u6240\u7ba1\u7406\u7684\u8d44\u6e90\u8981\u4e00\u4e00\u5bf9\u5e94\u3002\u4e3a\u4e86\u4fdd\u8bc1\u8fd9\u79cd\u5bf9\u5e94\u5173\u7cfb\uff0c\u5728\u590d\u5236\u7ba1\u7406\u5bf9\u8c61\u65f6\u4e5f\u590d\u5236\u6240\u7ba1\u7406\u7684\u8d44\u6e90\u3002<\/p> \n

4.\u8f6c\u79fb\u8d44\u6e90\u7684\u7ba1\u7406\u6743\u3002<\/p> \n

\u5728\u67d0\u4e9b\u7279\u6b8a\u573a\u5408\u4e0b\uff0c\u4f60\u53ef\u80fd\u5e0c\u671b\u8d44\u6e90\u53ea\u88ab\u4e00\u4e2a\u5bf9\u8c61\u62e5\u6709\uff0c\u4e5f\u5c31\u662f\u7ba1\u7406\u5bf9\u8c61\u5728copying\u65f6\u8981\u8fdb\u884c\u8d44\u6e90\u6240\u6709\u6743\u7684\u8f6c\u79fb\u3002\u4ece\u6761\u6b3e13\u4e2d\u8bb2\u5230\u7684auto_ptr\u53ef\u4ee5\u5b8c\u7f8e\u7684\u5b9e\u73b0\u8fd9\u4e2a\u9700\u6c42\u3002<\/p> \n

\u25a0\u603b\u7ed3<\/strong>
1.\u590d\u5236\u7ba1\u7406\u5bf9\u8c61\u65f6\uff0c\u8bf7\u4e00\u5e76\u590d\u5236\u5bf9\u8c61\u6240\u7ba1\u7406\u7684\u8d44\u6e90\uff0c\u8d44\u6e90\u7684copy\u884c\u4e3a\u51b3\u5b9a\u4e86\u7ba1\u7406\u5bf9\u8c61\u7684copy\u884c\u4e3a
2.\u666e\u904d\u7684RAII class\u7684copy\u884c\u4e3a\u662f\u6291\u5236\u590d\u5236\uff0c\u4f7f\u7528\u5f15\u7528\u8ba1\u6570\u3002<\/p> \n <\/div>\n<\/dd>","orderid":"0","title":"C++\u4e4b\u5728\u8d44\u6e90\u7ba1\u7406\u7c7b\u4e2d\u5c0f\u5fc3copying\u884c\u4e3a\uff0814\uff09---\u300aEffective C++\u300b","smalltitle":"","mid":"0","fname":"c++\u7f16\u7a0b\u57fa\u7840","special_id":"0","bak_id":"0","info":"0","hits":"8567","pages":"1","comments":"0","posttime":"2017-07-25 10:23:45","list":"1500949425","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"https:\/\/www.cppentry.com\/upload_files\/","ispic":"0","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"\u8d44\u6e90\u7ba1\u7406<\/A> \u5c0f\u5fc3<\/A> copying<\/A> \u884c\u4e3a<\/A> ---<\/A> Effective<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"218.17.197.195","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"C++\u4e4b\u5728\u8d44\u6e90\u7ba1\u7406\u7c7b\u4e2d\u5c0f\u5fc3copying\u884c\u4e3a\uff0814\uff09---\u300aEffective C++\u300b","lastview":"1711781571","digg_num":"1410","digg_time":"0","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}