\u4ee3\u7801\u4e2d\u7684MEOW_DEFER\u6211\u5728\u4e4b\u524d\u7684RAII\u76f8\u5173\u6587\u7ae0\u4e2d\u4ecb\u7ecd\u8fc7\uff0c\u5b83\u5185\u90e8\u7684\u51fd\u6570\u4f1a\u5728\u751f\u547d\u5468\u671f\u7ed3\u675f\u540e\u89e6\u53d1\u3002\u5b83\u7684\u6838\u5fc3\u51fd\u6570\u5176\u5b9e\u5c31\u662f\u4e0b\u9762\u8fd9\u56db\u4e2a\uff1a<\/p>\n
\r\nint sem_init(sem_t *sem,int pshared,unsigned int value);\r\nint sem_post(sem_t *sem);\r\nint sem_wait(sem_t *sem);\r\nint sem_destroy(sem_t *sem);\r\n<\/pre>\n\u5177\u4f53\u542b\u4e49\u5927\u5bb6\u5e94\u8be5\u770b\u540d\u5b57\u5c31\u77e5\u9053\uff0c\u8fd9\u91cc\u7684\u91cd\u70b9\u5c31\u662fsem_init\u4e2d\u7684pshared\u53c2\u6570\uff0c\u8be5\u53c2\u6570\u4e3a1\u8868\u793a\u53ef\u5728\u8fdb\u7a0b\u95f4\u5171\u4eab\uff0c\u4e3a0\u8868\u793a\u53ea\u5728\u8fdb\u7a0b\u5185\u90e8\u5171\u4eab\u3002<\/p>\n
\u7b2c\u4e8c\u79cd\u65b9\u5f0f\u662f\u4f7f\u7528\u9501\uff0c\u5373pthread_mutex_t\uff0c\u53ef\u662fpthread_mutex\u4e0d\u662f\u7528\u4f5c\u7ebf\u7a0b\u95f4\u6570\u636e\u7ade\u4e89\u7684\u5417\uff0c\u600e\u4e48\u80fd\u7528\u5728\u8fdb\u7a0b\u95f4\u5462?<\/p>\n
\u6211\u4e5f\u662f\u6700\u8fd1\u624d\u77e5\u9053\uff0c\u53ef\u4ee5\u7ed9\u5b83\u914d\u7f6e\u4e00\u4e2a\u5c5e\u6027\uff0c\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n
\r\npthread_mutex_t* mutex;\r\npthread_mutexattr_t mutexattr;\r\n\r\npthread_mutexattr_init(&mutexattr);\r\npthread_mutexattr_setpshared(&mutexattr, PTHREAD_PROCESS_SHARED);\r\npthread_mutex_init(mutex, &mutexattr);\r\n<\/pre>\n\u5b83\u7684\u9ed8\u8ba4\u5c5e\u6027\u662f\u8fdb\u7a0b\u5185\u79c1\u6709\uff0c\u4f46\u662f\u5982\u679c\u7ed9\u5b83\u914d\u7f6e\u6210PTHREAD_PROCESS_SHARED\uff0c\u5b83\u5c31\u53ef\u4ee5\u7528\u5728\u8fdb\u7a0b\u95f4\u901a\u4fe1\u4e2d\u3002<\/p>\n
\u5b8c\u6574\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n
\r\nvoid func() {\r\n const char* mapname = \"\/mapname\";\r\n int mapfd = shm_open(mapname, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);\r\n\r\n MEOW_DEFER {\r\n if (mapfd > 0) {\r\n close(mapfd);\r\n mapfd = 0;\r\n }\r\n shm_unlink(mapname);\r\n };\r\n\r\n if (mapfd == -1) {\r\n perror(\"shm_open failed \\n\");\r\n exit(EXIT_FAILURE);\r\n }\r\n\r\n if (ftruncate(mapfd, kMappingSize) == -1) {\r\n perror(\"ftruncate failed \\n\");\r\n exit(EXIT_FAILURE);\r\n }\r\n\r\n void* sp = mmap(nullptr, kMappingSize, PROT_READ | PROT_WRITE, MAP_SHARED, mapfd, 0);\r\n if (!sp) {\r\n perror(\"mmap failed \\n\");\r\n exit(EXIT_FAILURE);\r\n }\r\n\r\n pthread_mutex_t* mutex = (pthread_mutex_t*)sp;\r\n pthread_mutexattr_t mutexattr;\r\n\r\n pthread_mutexattr_init(&mutexattr);\r\n pthread_mutexattr_setpshared(&mutexattr, PTHREAD_PROCESS_SHARED);\r\n pthread_mutex_init(mutex, &mutexattr);\r\n\r\n MEOW_DEFER {\r\n pthread_mutexattr_destroy(&mutexattr);\r\n pthread_mutex_destroy(mutex);\r\n };\r\n\r\n int* num = (int*)((char*)sp + sizeof(pthread_mutex_t));\r\n int cid, proc_count = 0, max_proc_count = 8;\r\n for (int i = 0; i < max_proc_count; ++i) {\r\n cid = fork();\r\n if (cid == -1) {\r\n perror(\"fork failed \\n\");\r\n continue;\r\n }\r\n if (cid == 0) {\r\n pthread_mutex_lock(mutex);\r\n (*num)++;\r\n printf(\"process %d : %d \\n\", getpid(), *num);\r\n pthread_mutex_unlock(mutex);\r\n\r\n if (munmap(sp, kMappingSize) == -1) {\r\n perror(\"munmap failed\\n\");\r\n }\r\n close(mapfd);\r\n exit(EXIT_SUCCESS);\r\n }\r\n ++proc_count;\r\n }\r\n\r\n int stat;\r\n while (proc_count--) {\r\n cid = wait(&stat);\r\n if (cid == -1) {\r\n perror(\"wait failed \\n\");\r\n break;\r\n }\r\n }\r\n\r\n printf(\"ok \\n\");\r\n}\r\n<\/pre>\n\u6211\u60f3\u8fd9\u4e24\u79cd\u65b9\u5f0f\u5e94\u8be5\u53ef\u4ee5\u6ee1\u8db3\u6211\u4eec\u65e5\u5e38\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u7684\u5927\u591a\u6570\u9700\u6c42\u3002<\/p>\n
\u9501\u7684\u65b9\u5f0f\u4ecb\u7ecd\u5b8c\u4e4b\u540e\uff0c\u53ef\u80fd\u5f88\u591a\u670b\u53cb\u81ea\u7136\u5c31\u4f1a\u60f3\u5230\u539f\u5b50\u53d8\u91cf\uff0c\u8fd9\u5757\u6211\u4e5f\u641c\u7d22\u4e86\u4e00\u4e0b\u3002\u4f46\u662f\u4e5f\u4e0d\u592a\u786e\u5b9aC++\u6807\u51c6\u4e2d\u7684atomic\u662f\u5426\u5728\u8fdb\u7a0b\u95f4\u901a\u4fe1\u4e2d\u6709\u4f5c\u7528\uff0c\u4e0d\u8fc7\u770b\u6837\u5b50boost\u4e2d\u7684atomic\u662f\u53ef\u4ee5\u7528\u5728\u8fdb\u7a0b\u95f4\u901a\u4fe1\u4e2d\u7684\u3002<\/p>\n
\u5176\u5b9e\u5728\u7814\u7a76\u8fd9\u4e2a\u95ee\u9898\u7684\u8fc7\u7a0b\u4e2d\uff0c\u8fd8\u627e\u5230\u4e86\u4e00\u4e9b\u5f88\u591a\u89e3\u51b3\u529e\u6cd5\uff0c\u5305\u62ec\uff1a<\/p>\n
Disabling Interrupts<\/ol>\nLock Variables<\/ol>\nStrict Alternation<\/ol>\nPeterson's Solution<\/ol>\nThe TSL Instruction<\/ol>\nSleep and Wakeup<\/ol>\nSemaphores<\/ol>\nMutexes<\/ol>\nMonitors<\/ol>\nMessage Passing<\/ol>\nBarriers<\/ol>\n\u8fd9\u91cc\u5c31\u4e0d\u8fc7\u591a\u4ecb\u7ecd\u5566\uff0c\u5927\u5bb6\u611f\u5174\u8da3\u7684\u53ef\u4ee5\u81ea\u884c\u67e5\u9605\u8d44\u6599\u54c8\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"
\u5173\u4e8e\u8fdb\u7a0b\u95f4\u7684\u901a\u4fe1\u65b9\u5f0f\u4f30\u8ba1\u5927\u591a\u6570\u4eba\u90fd\u77e5\u9053\uff0c\u8fd9\u4e5f\u662f\u5e38\u89c1\u7684\u9762\u8bd5\u516b\u80a1\u6587\u4e4b\u4e00\u3002 \u4e2a\u4eba\u8ba4\u4e3a\u8fd9\u79cd\u9762\u8bd5\u9898\u6ca1\u4ec0\u4e48\u610f\u4e49\uff0c\u65e0\u975e\u5c31\u662f\u7b54 […]<\/p>\n","protected":false},"author":1329,"featured_media":235546,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-235542","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/235542","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1329"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=235542"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/235542\/revisions"}],"predecessor-version":[{"id":235547,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/235542\/revisions\/235547"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/235546"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=235542"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=235542"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=235542"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}