\u76ee\u5f55<\/p> \n
- \n
- 1. \u7ebf\u7a0b\u540c\u6b65\u6982\u8ff0<\/a>\n
- \n
- \u7ebf\u7a0b\u540c\u6b65\u5b9a\u4e49<\/a><\/li> \n
- \u7ebf\u7a0b\u540c\u6b65\u65b9\u6cd5<\/a><\/li> \n <\/ul><\/li> \n
- 2. \u4e92\u65a5\u9501<\/a>\n
- \n
- \u4e92\u65a5\u9501\u6982\u5ff5<\/a><\/li> \n
- \u4e92\u65a5\u9501\u57fa\u672cAPI<\/a>\n
- \n
- \u521d\u59cb\u5316\u4e0e\u9500\u6bc1<\/a><\/li> \n
- \u4e0a\u9501\u4e0e\u89e3\u9501<\/a><\/li> \n
- \u4e24\u4e2a\u7279\u6b8a\u7684\u4e0a\u9501\u51fd\u6570<\/a><\/li> \n <\/ul><\/li> \n
- \u793a\u4f8b\u4ee3\u7801<\/a><\/li> \n <\/ul><\/li> \n
- 3. \u907f\u514d\u6b7b\u9501<\/a>\n
- \n
- \u7ebf\u7a0b\u7684\u6b7b\u9501\u6982\u5ff5<\/a><\/li> \n
- \u4ea7\u751f\u6b7b\u9501\u7684\u56db\u4e2a\u5fc5\u8981\u6761\u4ef6<\/a><\/li> \n
- \u5982\u4f55\u907f\u514d\u6b7b\u9501<\/a><\/li> \n <\/ul><\/li> \n
- 4. \u6761\u4ef6\u53d8\u91cf<\/a>\n
- \n
- \u6761\u4ef6\u53d8\u91cf\u6982\u5ff5<\/a><\/li> \n
- \u6761\u4ef6\u53d8\u91cf\u57fa\u672cAPI<\/a>\n
- \n
- \u521d\u59cb\u5316\u4e0e\u9500\u6bc1<\/a><\/li> \n
- \u7b49\u5f85\u6761\u4ef6\u6ee1\u8db3<\/a><\/li> \n
- \u7ed9\u7ebf\u7a0b\u53d1\u4fe1\u53f7<\/a><\/li> \n <\/ul><\/li> \n
- \u793a\u4f8b\u4ee3\u7801<\/a><\/li> \n <\/ul><\/li> \n <\/ul> \n <\/div> \n <\/div> \n
1. \u7ebf\u7a0b\u540c\u6b65\u6982\u8ff0<\/h1> \n
\u7ebf\u7a0b\u540c\u6b65\u5b9a\u4e49<\/h2> \n
\u7ebf\u7a0b\u540c\u6b65\uff0c\u6307\u7684\u662f\u63a7\u5236\u591a\u7ebf\u7a0b\u95f4\u7684\u76f8\u5bf9\u6267\u884c\u987a\u5e8f<\/font>\uff0c\u4ece\u800c\u5728\u7ebf\u7a0b\u95f4\u6b63\u786e\u3001\u6709\u5e8f\u5730\u5171\u4eab\u6570\u636e<\/font>\uff0c\u4ee5\u4e0b\u4e3a\u7ebf\u7a0b\u540c\u6b65\u5e38\u89c1\u4f7f\u7528\u573a\u5408\u3002<\/p> \n
- \n
- \u591a\u7ebf\u7a0b\u6267\u884c\u7684\u4efb\u52a1\u5728\u987a\u5e8f\u4e0a\u5b58\u5728\u4f9d\u8d56\u5173\u7cfb<\/li> \n
- \u7ebf\u7a0b\u95f4\u5171\u4eab\u6570\u636e\u53ea\u80fd\u540c\u65f6\u88ab\u4e00\u4e2a\u7ebf\u7a0b\u4f7f\u7528<\/li> \n <\/ul> \n
\u7ebf\u7a0b\u540c\u6b65\u65b9\u6cd5<\/h2> \n
\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u7ecf\u5e38\u4f7f\u7528\u7684\u7ebf\u7a0b\u540c\u6b65\u65b9\u6cd5\u4e3b\u8981\u5206\u4e3a\u4e09\u79cd\uff1a<\/p> \n
- \n
- \u4e92\u65a5\u9501<\/li> \n
- \u6761\u4ef6\u53d8\u91cf<\/li> \n
- Posix\u4fe1\u53f7\u91cf\uff08\u5305\u62ec\u6709\u540d\u4fe1\u53f7\u91cf\u548c\u65e0\u540d\u4fe1\u53f7\u91cf\uff09<\/li> \n <\/ul> \n
\u672c\u8282\u5185\u5bb9\u53ea\u4ecb\u7ecd\u4e92\u65a5\u9501\u548c\u6761\u4ef6\u53d8\u91cf\uff0cPosix\u4fe1\u53f7\u91cf\u540e\u7eed\u5728Posix IPC\u4e13\u9898\u4e2d\u4ecb\u7ecd\u3002<\/p> \n
2. \u4e92\u65a5\u9501<\/h1> \n
\u4e92\u65a5\u9501\u6982\u5ff5<\/h2> \n
\u4e92\u65a5\u9501\u7528\u4e8e\u786e\u4fdd\u540c\u4e00\u65f6\u95f4\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u8bbf\u95ee\u5171\u4eab\u6570\u636e\uff0c\u4f7f\u7528\u65b9\u6cd5\u4e3a\uff1a<\/p> \n
- \n
- \u52a0\u9501<\/li> \n
- \u8bbf\u95ee\u5171\u4eab\u6570\u636e<\/li> \n
- \u89e3\u9501<\/li> \n <\/ul> \n
\u5bf9\u4e92\u65a5\u9501\u52a0\u9501\u540e\uff0c\u4efb\u4f55\u5176\u4ed6\u8bd5\u56fe\u518d\u6b21\u5bf9\u5176\u52a0\u9501\u7684\u7ebf\u7a0b\u90fd\u4f1a\u88ab\u963b\u585e\uff0c\u76f4\u5230\u5f53\u524d\u7ebf\u7a0b\u91ca\u653e\u8be5\u4e92\u65a5\u9501\uff0c\u89e3\u9501\u65f6\u6240\u6709\u963b\u585e\u7ebf\u7a0b\u90fd\u4f1a\u53d8\u6210\u53ef\u8fd0\u884c\u72b6\u6001\uff0c\u4f46\u7a76\u7adf\u54ea\u4e2a\u5148\u8fd0\u884c\uff0c\u8fd9\u4e00\u70b9\u662f\u4e0d\u786e\u5b9a\u7684\u3002<\/p> \n
\u4e92\u65a5\u9501\u57fa\u672cAPI<\/h2> \n
\u521d\u59cb\u5316\u4e0e\u9500\u6bc1<\/h3> \n
\u4e92\u65a5\u9501\u662f\u7528
pthread_mutex_t<\/code>\u6570\u636e\u7c7b\u578b\u8868\u793a\u7684\uff0c\u5728\u4f7f\u7528\u4e92\u65a5\u9501\u4e4b\u524d\uff0c\u9700\u8981\u5148\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u521d\u59cb\u5316\u65b9\u6cd5\u6709\u4e24\u79cd\uff1a<\/p> \n
- \n
- \u8bbe\u7f6e\u4e3a\u5e38\u91cf
PTHREAD_MUTEX_INITIALIZER<\/code>\uff0c\u53ea\u9002\u7528\u4e8e\u9759\u6001\u5206\u914d\u7684\u4e92\u65a5\u9501<\/li> \n
- \u8c03\u7528
pthread_mutex_init<\/code>\u51fd\u6570\uff0c\u9759\u6001\u5206\u914d\u548c\u52a8\u6001\u5206\u914d\u7684\u4e92\u65a5\u9501\u90fd\u53ef\u4ee5<\/li> \n <\/ul> \n
\u4e92\u65a5\u9501\u4f7f\u7528\u5b8c\u4ee5\u540e\uff0c\u53ef\u4ee5\u8c03\u7528
pthread_mutex_destroy<\/code>\u8fdb\u884c\u9500\u6bc1\uff0c\u5c24\u5176\u662f\u5bf9\u4e8e\u52a8\u6001\u5206\u914d\u7684\u4e92\u65a5\u9501\uff0c\u5728\u91ca\u653e\u5185\u5b58\u524d\uff0c\u8c03\u7528pthread_mutex_destroy\u662f\u5fc5\u987b\u7684\u3002<\/font><\/p> \n
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;\n\n\/\/\u4e24\u4e2a\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff1a\u6210\u529f\u8fd4\u56de0\uff0c\u5931\u8d25\u8fd4\u56de\u9519\u8bef\u7f16\u53f7\nint pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr);\nint pthread_mutex_destroy(pthread_mutex_t *mutex);<\/code><\/pre> \n
\u5176\u4e2d\uff0cpthread_mutex_init\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570attr\u7528\u4e8e\u8bbe\u7f6e\u4e92\u65a5\u9501\u7684\u5c5e\u6027\uff0c\u5982\u679c\u8981\u4f7f\u7528\u9ed8\u8ba4\u5c5e\u6027\uff0c\u53ea\u9700\u628aattr\u8bbe\u4e3aNULL\u3002<\/p> \n
\u4e0a\u9501\u4e0e\u89e3\u9501<\/h3> \n
\/\/\u4e24\u4e2a\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff1a\u6210\u529f\u8fd4\u56de0\uff0c\u5931\u8d25\u8fd4\u56de\u9519\u8bef\u7f16\u53f7\nint pthread_mutex_lock(pthread_mutex_t *mutex);\nint pthread_mutex_unlock(pthread_mutex_t *mutex);<\/code><\/pre> \n
\u5bf9\u4e92\u65a5\u9501\u4e0a\u9501\uff0c\u9700\u8981\u8c03\u7528
pthread_mutex_lock<\/code>\uff0c\u5982\u679c\u4e92\u65a5\u9501\u5df2\u7ecf\u4e0a\u9501\uff0c\u8c03\u7528\u7ebf\u7a0b\u5c06\u963b\u585e\u5230\u8be5\u4e92\u65a5\u9501\u88ab\u91ca\u653e\u3002
\u5bf9\u4e92\u65a5\u9501\u89e3\u9501\uff0c\u9700\u8981\u8c03\u7528pthread_mutex_unlock<\/code>\u3002<\/p> \n
\u4e24\u4e2a\u7279\u6b8a\u7684\u4e0a\u9501\u51fd\u6570<\/h3> \n
\u5c1d\u8bd5\u4e0a\u9501<\/h4> \n
\/\/\u6210\u529f\u8fd4\u56de0\uff0c\u5931\u8d25\u8fd4\u56de\u9519\u8bef\u7f16\u53f7\nint pthread_mutex_trylock(pthread_mutex_t *mutex);<\/code><\/pre> \n
\u5982\u679c\u4e0d\u5e0c\u671b\u8c03\u7528\u7ebf\u7a0b\u963b\u585e\uff0c\u53ef\u4ee5\u4f7f\u7528
pthread_mutex_trylock<\/code>\u5c1d\u8bd5\u4e0a\u9501\uff1a<\/p> \n
- \n
- \u82e5mutex\u672a\u4e0a\u9501\uff0cpthread_mutex_trylock\u5c06\u52a0\u9501\u6210\u529f\uff0c\u8fd4\u56de0<\/li> \n
- \u82e5mutex\u5df2\u4e0a\u9501\uff0cpthread_mutex_trylock\u4f1a\u52a0\u9501\u5931\u8d25\uff0c\u8fd4\u56de
EBUSY<\/code><\/li> \n <\/ul> \n
\u9650\u65f6\u4e0a\u9501<\/h4> \n
\/\/\u6210\u529f\u8fd4\u56de0\uff0c\u5931\u8d25\u8fd4\u56de\u9519\u8bef\u7f16\u53f7\nint pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *time);<\/code><\/pre> \n
pthread_mutex_timedlock<\/code>\u662f\u4e00\u4e2a\u53ef\u4ee5\u8bbe\u7f6e\u963b\u585e\u65f6\u95f4\u7684\u4e0a\u9501\u51fd\u6570\uff1a<\/p> \n
- \n
- \u5f53mutex\u5df2\u4e0a\u9501\u65f6\uff0c\u8c03\u7528\u7ebf\u7a0b\u4f1a\u963b\u585e\u8bbe\u5b9a\u7684\u65f6\u95f4<\/li> \n
- \u5f53\u8fbe\u5230\u8bbe\u5b9a\u65f6\u95f4\u65f6\uff0cpthread_mutex_timedlock\u5c06\u52a0\u9501\u5931\u8d25\u5e76\u89e3\u9664\u963b\u585e\uff0c\u8fd4\u56de
ETIMEDOUT<\/code><\/li> \n <\/ul> \n
\u5173\u4e8e\u7b2c\u4e8c\u4e2a\u53c2\u6570time\uff0c\u6709\u4e24\u70b9\u9700\u8981\u6ce8\u610f\uff1a<\/p> \n
- \n
- time\u8868\u793a\u7b49\u5f85\u7684\u7edd\u5bf9\u65f6\u95f4\uff0c\u9700\u8981\u5c06\u5176\u8bbe\u4e3a\u5f53\u524d\u65f6\u95f4 + \u7b49\u5f85\u65f6\u95f4<\/font><\/li> \n
- time\u662f\u7531struct timespec\u6307\u5b9a\u7684\uff0c\u5b83\u7531\u79d2\u548c\u7eb3\u79d2\u6765\u63cf\u8ff0\u65f6\u95f4<\/li> \n <\/ul> \n
\u793a\u4f8b\u4ee3\u7801<\/h2> \n
\/*\n * \u6d4b\u8bd5\u4f7f\u7528\u4e0a\u8ff04\u4e2a\u52a0\u9501\u51fd\u6570\n*\/\n\n#include <pthread.h>\n#include <time.h>\n#include <errno.h>\n#include <stdio.h>\n\npthread_mutex_t mutex1;\npthread_mutex_t mutex2;\npthread_mutex_t mutex3;\n\nvoid *thread1_start(void *arg)\n{\n pthread_mutex_lock(&mutex1);\n printf("thread1 has locked mutex1\\n");\n sleep(2); \/\/\u4fdd\u8bc1thread2\u6267\u884c\u65f6mutex1\u8fd8\u672a\u89e3\u9501\n pthread_mutex_unlock(&mutex1);\n}\n\nvoid *thread2_start(void *arg)\n{\n if (pthread_mutex_trylock(&mutex2) == 0)\n printf("thread2 trylock mutex2 sucess\\n");\n\n if (pthread_mutex_trylock(&mutex1) == EBUSY)\n printf("thread2 trylock mutex1 failed\\n");\n \n pthread_mutex_unlock(&mutex2);\n}\n\nvoid *thread3_start(void *arg)\n{\n struct timespec time;\n struct tm *tmp_time;\n char s[64];\n int err;\n \n pthread_mutex_lock(&mutex3);\n printf("thread3 has locked mutex3\\n");\n \n \/*\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\uff0c\u5e76\u8f6c\u5316\u4e3a\u672c\u5730\u65f6\u95f4\u6253\u5370*\/\n clock_gettime(CLOCK_REALTIME, &time);\n tmp_time = localtime(&time.tv_sec);\n strftime(s, sizeof(s), "%r", tmp_time);\n printf("current time is %s\\n", s);\n \n \/*\u8bbe\u7f6etime = \u5f53\u524d\u65f6\u95f4 + \u7b49\u5f85\u65f6\u95f410S*\/\n time.tv_sec = time.tv_sec + 10;\n \n \/*mutex3\u5df2\u4e0a\u9501\uff0c\u8fd9\u91cc\u4f1a\u963b\u585e*\/\n if (pthread_mutex_timedlock(&mutex3, &time) == ETIMEDOUT)\n printf("pthread_mutex_timedlock mutex3 timeout\\n");\n \n \/*\u518d\u6b21\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\uff0c\u5e76\u8f6c\u5316\u4e3a\u672c\u5730\u65f6\u95f4\u6253\u5370*\/","orderid":"0","title":"\u7ebf\u7a0b\u540c\u6b65(\u4e00)","smalltitle":"","mid":"0","fname":"Linux","special_id":"0","bak_id":"0","info":"0","hits":"84","pages":"3","comments":"0","posttime":"2019-09-15 00:33:37","list":"1568478817","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":"\u7ebf\u7a0b<\/A> \u540c\u6b65<\/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":"120.229.33.113","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"\u7ebf\u7a0b\u540c\u6b65","lastview":"1702372786","digg_num":"0","digg_time":"0","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}
- time\u662f\u7531struct timespec\u6307\u5b9a\u7684\uff0c\u5b83\u7531\u79d2\u548c\u7eb3\u79d2\u6765\u63cf\u8ff0\u65f6\u95f4<\/li> \n <\/ul> \n
- time\u8868\u793a\u7b49\u5f85\u7684\u7edd\u5bf9\u65f6\u95f4\uff0c\u9700\u8981\u5c06\u5176\u8bbe\u4e3a\u5f53\u524d\u65f6\u95f4 + \u7b49\u5f85\u65f6\u95f4<\/font><\/li> \n
- \u8c03\u7528
- \u8bbe\u7f6e\u4e3a\u5e38\u91cf
- \u7b49\u5f85\u6761\u4ef6\u6ee1\u8db3<\/a><\/li> \n
- \u6761\u4ef6\u53d8\u91cf\u57fa\u672cAPI<\/a>\n
- \u4ea7\u751f\u6b7b\u9501\u7684\u56db\u4e2a\u5fc5\u8981\u6761\u4ef6<\/a><\/li> \n
- \u4e0a\u9501\u4e0e\u89e3\u9501<\/a><\/li> \n
- \u4e92\u65a5\u9501\u57fa\u672cAPI<\/a>\n
- \u7ebf\u7a0b\u540c\u6b65\u65b9\u6cd5<\/a><\/li> \n <\/ul><\/li> \n
- \u7ebf\u7a0b\u540c\u6b65\u5b9a\u4e49<\/a><\/li> \n