{"id":202027,"date":"2020-10-08T09:17:47","date_gmt":"2020-10-08T01:17:47","guid":{"rendered":"https:\/\/gulass.cn\/?p=202027"},"modified":"2020-09-27T09:22:27","modified_gmt":"2020-09-27T01:22:27","slug":"linux-threading-lock","status":"publish","type":"post","link":"https:\/\/gulass.cn\/linux-threading-lock.html","title":{"rendered":"Linux\u4e4b\u7ebf\u7a0b\u4e92\u65a5\u9501"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5728\u7f16\u7a0b\u4e2d\uff0c\u5f15\u5165\u4e86\u5bf9\u8c61\u4e92\u65a5\u9501\u7684\u6982\u5ff5\uff0c\u6765\u4fdd\u8bc1\u5171\u4eab\u6570\u636e\u64cd\u4f5c\u7684\u5b8c\u6574\u6027\u3002\u6bcf\u4e2a\u5bf9\u8c61\u90fd\u5bf9\u5e94\u4e8e\u4e00\u4e2a\u53ef\u79f0\u4e3a\" \u4e92\u65a5\u9501\" \u7684\u6807\u8bb0\uff0c\u8fd9\u4e2a\u6807\u8bb0\u7528\u6765\u4fdd\u8bc1\u5728\u4efb\u4e00\u65f6\u523b\uff0c\u53ea\u80fd\u6709\u4e00\u4e2a\u7ebf\u7a0b\u8bbf\u95ee\u8be5\u5bf9\u8c61\u3002Linux\u5b9e\u73b0\u7684\u4e92\u65a5\u9501\u673a\u5236\u5305\u62ecPOSIX\u4e92\u65a5\u9501\u548c\u5185\u6838\u4e92\u65a5\u9501\uff0c\u672c\u6587\u4e3b\u8981\u8bb2POSIX\u4e92\u65a5\u9501\uff0c\u5373\u7ebf\u7a0b\u95f4\u4e92\u65a5\u9501\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u4fe1\u53f7\u91cf\u7528\u5728\u591a\u7ebf\u7a0b\u591a\u4efb\u52a1\u540c\u6b65\u7684\uff0c\u4e00\u4e2a\u7ebf\u7a0b\u5b8c\u6210\u4e86\u67d0\u4e00\u4e2a\u52a8\u4f5c\u5c31\u901a\u8fc7\u4fe1\u53f7\u91cf\u544a\u8bc9\u522b\u7684\u7ebf\u7a0b\uff0c\u522b\u7684\u7ebf\u7a0b\u518d\u8fdb\u884c\u67d0\u4e9b\u52a8\u4f5c(\u5927\u5bb6\u90fd\u5728sem_wait\u7684\u65f6\u5019\uff0c\u5c31\u963b\u585e\u5728 \u90a3\u91cc)\u3002\u800c\u4e92\u65a5\u9501\u662f\u7528\u5728\u591a\u7ebf\u7a0b\u591a\u4efb\u52a1\u4e92\u65a5\u7684\uff0c\u4e00\u4e2a\u7ebf\u7a0b\u5360\u7528\u4e86\u67d0\u4e00\u4e2a\u8d44\u6e90\uff0c\u90a3\u4e48\u522b\u7684\u7ebf\u7a0b\u5c31\u65e0\u6cd5\u8bbf\u95ee\uff0c\u76f4\u5230\u8fd9\u4e2a\u7ebf\u7a0bunlock\uff0c\u5176\u4ed6\u7684\u7ebf\u7a0b\u624d\u5f00\u59cb\u53ef\u4ee5\u5229\u7528\u8fd9 \u4e2a\u8d44\u6e90\u3002\u6bd4\u5982\u5bf9\u5168\u5c40\u53d8\u91cf\u7684\u8bbf\u95ee\uff0c\u6709\u65f6\u8981\u52a0\u9501\uff0c\u64cd\u4f5c\u5b8c\u4e86\uff0c\u5728\u89e3\u9501\u3002\u6709\u7684\u65f6\u5019\u9501\u548c\u4fe1\u53f7\u91cf\u4f1a\u540c\u65f6\u4f7f\u7528\u7684\u201d<\/p>\n

\u4e5f\u5c31\u662f\u8bf4\uff0c\u4fe1\u53f7\u91cf\u4e0d\u4e00\u5b9a\u662f\u9501\u5b9a\u67d0\u4e00\u4e2a\u8d44\u6e90\uff0c\u800c\u662f \u6d41\u7a0b\u4e0a\u7684\u6982\u5ff5\uff0c\u6bd4\u5982\uff1a\u6709A,B\u4e24\u4e2a\u7ebf\u7a0b\uff0cB\u7ebf\u7a0b\u8981\u7b49A\u7ebf\u7a0b\u5b8c\u6210\u67d0\u4e00\u4efb\u52a1\u4ee5\u540e\u518d\u8fdb\u884c\u81ea\u5df1\u4e0b\u9762\u7684\u6b65\u9aa4\uff0c\u8fd9\u4e2a\u4efb\u52a1\u5e76\u4e0d\u4e00\u5b9a\u662f\u9501\u5b9a\u67d0\u4e00\u8d44\u6e90\uff0c\u8fd8\u53ef\u4ee5\u662f\u8fdb\u884c\u4e00\u4e9b\u8ba1\u7b97 \u6216\u8005\u6570\u636e\u5904\u7406\u4e4b\u7c7b\u3002\u800c\u7ebf\u7a0b\u4e92\u65a5\u91cf\u5219\u662f\u201c\u9501\u4f4f\u67d0\u4e00\u8d44\u6e90\u201d\u7684\u6982\u5ff5\uff0c\u5728\u9501\u5b9a\u671f\u95f4\u5185\uff0c\u5176\u4ed6\u7ebf\u7a0b\u65e0\u6cd5\u5bf9\u88ab\u4fdd\u62a4\u7684\u6570\u636e\u8fdb\u884c\u64cd\u4f5c\u3002\u5728\u6709\u4e9b\u60c5\u51b5\u4e0b\u4e24\u8005\u53ef\u4ee5\u4e92\u6362\u3002<\/p>\n

\u4e24\u8005\u4e4b\u95f4\u7684\u533a\u522b:<\/p>\n

\u4f5c\u7528\u57df<\/strong><\/div>\n

\u4fe1\u53f7\u91cf : \u8fdb\u7a0b\u95f4\u6216\u7ebf\u7a0b\u95f4(linux\u4ec5\u7ebf\u7a0b\u95f4)<\/p>\n

\u4e92\u65a5\u9501 : \u7ebf\u7a0b\u95f4<\/p>\n

\u4e0a\u9501\u65f6<\/strong><\/div>\n

\u4fe1\u53f7\u91cf : \u53ea\u8981\u4fe1\u53f7\u91cf\u7684value\u5927\u4e8e0\uff0c\u5176\u4ed6\u7ebf\u7a0b\u5c31\u53ef\u4ee5sem_wait\u6210\u529f\uff0c\u6210\u529f\u540e\u4fe1\u53f7\u91cf\u7684value\u51cf\u4e00\u3002\u82e5value\u503c\u4e0d\u5927\u4e8e0\uff0c\u5219sem_wait\u963b\u585e\uff0c\u76f4\u5230sem_post\u91ca\u653e\u540evalue\u503c\u52a0\u4e00\u3002\u4e00\u53e5\u8bdd\uff0c\u4fe1\u53f7\u91cf\u7684value>=0 \u3002<\/p>\n

\u4e92\u65a5\u9501 : \u53ea\u8981\u88ab\u9501\u4f4f\uff0c\u5176\u4ed6\u4efb\u4f55\u7ebf\u7a0b\u90fd\u4e0d\u53ef\u4ee5\u8bbf\u95ee\u88ab\u4fdd\u62a4\u7684\u8d44\u6e90\u3002\u5982\u679c\u6ca1\u6709\u9501\uff0c\u83b7\u5f97\u8d44\u6e90\u6210\u529f\uff0c\u5426\u5219\u8fdb\u884c\u963b\u585e\u7b49\u5f85\u8d44\u6e90\u53ef\u7528\u3002\u4e00\u53e5\u8bdd\uff0c\u7ebf\u7a0b\u4e92\u65a5\u9501\u7684vlaue\u53ef\u4ee5\u4e3a\u8d1f\u6570 \u3002<\/p>\n

\u591a\u7ebf\u7a0b<\/strong><\/div>\n

\u7ebf\u7a0b\u662f\u8ba1\u7b97\u673a\u4e2d\u72ec\u7acb\u8fd0\u884c\u7684\u6700\u5c0f\u5355\u4f4d\uff0c\u8fd0\u884c\u65f6\u5360\u7528\u5f88\u5c11\u7684\u7cfb\u7edf\u8d44\u6e90\u3002\u4e0e\u591a\u8fdb\u7a0b\u76f8\u6bd4\uff0c\u591a\u8fdb\u7a0b\u5177\u6709\u591a\u8fdb\u7a0b\u4e0d\u5177\u5907\u7684\u4e00\u4e9b\u4f18\u70b9\uff0c\u5176\u6700\u91cd\u8981\u7684\u662f\uff1a\u5bf9\u4e8e\u591a\u7ebf\u7a0b\u6765\u8bf4\uff0c\u5176\u80fd\u591f\u6bd4\u591a\u8fdb\u7a0b\u66f4\u52a0\u8282\u7701\u8d44\u6e90\u3002<\/p>\n

\u7ebf\u7a0b\u521b\u5efa<\/strong><\/div>\n

\u5728Linux\u4e2d\uff0c\u65b0\u5efa\u7684\u7ebf\u7a0b\u5e76\u4e0d\u662f\u5728\u539f\u5148\u7684\u8fdb\u7a0b\u4e2d\uff0c\u800c\u662f\u7cfb\u7edf\u901a\u8fc7\u4e00\u4e2a\u7cfb\u7edf\u8c03\u7528clone()\u3002\u8be5\u7cfb\u7edfcopy\u4e86\u4e00\u4e2a\u548c\u539f\u5148\u8fdb\u7a0b\u5b8c\u5168\u4e00\u6837\u7684\u8fdb\u7a0b\uff0c\u5e76\u5728\u8fd9\u4e2a\u8fdb\u7a0b\u4e2d\u6267\u884c\u7ebf\u7a0b\u51fd\u6570\u3002<\/p>\n

\u5728Linux\u4e2d\uff0c\u901a\u8fc7\u51fd\u6570pthread_create()\u51fd\u6570\u5b9e\u73b0\u7ebf\u7a0b\u7684\u521b\u5efa\uff1a<\/p>\n

pthread_create()<\/strong><\/p>\n

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*st <\/pre>\n

\u5176\u4e2d\uff1a<\/p>\n

  • thread\u8868\u793a\u7684\u662f\u4e00\u4e2apthread_t\u7c7b\u578b\u7684\u6307\u9488;<\/li>\n
  • attr\u7528\u4e8e\u6307\u5b9a\u7ebf\u7a0b\u7684\u4e00\u4e9b\u5c5e\u6027;<\/li>\n
  • start_routine\u8868\u793a\u7684\u662f\u4e00\u4e2a\u51fd\u6570\u6307\u9488\uff0c\u8be5\u51fd\u6570\u662f\u7ebf\u7a0b\u8c03\u7528\u51fd\u6570;<\/li>\n
  • arg\u8868\u793a\u7684\u662f\u4f20\u9012\u7ed9\u7ebf\u7a0b\u8c03\u7528\u51fd\u6570\u7684\u53c2\u6570\u3002<\/li>\n

    \u5f53\u7ebf\u7a0b\u521b\u5efa\u6210\u529f\u65f6\uff0c\u51fd\u6570pthread_create()\u8fd4\u56de0\uff0c\u82e5\u8fd4\u56de\u503c\u4e0d\u4e3a0\u5219\u8868\u793a\u521b\u5efa\u7ebf\u7a0b\u5931\u8d25\u3002\u5bf9\u4e8e\u7ebf\u7a0b\u7684\u5c5e\u6027\uff0c\u5219\u5728\u7ed3\u6784\u4f53pthread_attr_t\u4e2d\u5b9a\u4e49\u3002<\/p>\n

    \u7ebf\u7a0b\u521b\u5efa\u7684\u8fc7\u7a0b\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

    \r\n#include  \r\n#include  \r\n#include  \r\n#include  \r\n \r\nvoid* thread(void *id){ \r\n   pthread_t newthid; \r\n \r\n   newthid = pthread_self(); \r\n   printf(\"this is a new thread, thread ID is %u\\n\", newthid); \r\n   return NULL; \r\n} \r\n \r\nint main(){ \r\n int num_thread = 5; \r\n pthread_t *pt = (pthread_t *)malloc(sizeof(pthread_t) * num_thread); \r\n \r\n printf(\"main thread, ID is %u\\n\", pthread_self()); \r\n for (int i = 0; i < num_thread; i++){ \r\n       if (pthread_create(&pt[i], NULL, thread, NULL) != 0){ \r\n          printf(\"thread create failed!\\n\"); \r\n          return 1; \r\n       } \r\n } \r\n sleep(2); \r\n free(pt); \r\n return 0; \r\n} \r\n<\/pre>\n

    \u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528\u5230\u4e86pthread_self()\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u7684\u4f5c\u7528\u662f\u83b7\u53d6\u672c\u7ebf\u7a0b\u7684\u7ebf\u7a0bID\u3002\u5728\u4e3b\u51fd\u6570\u4e2d\u7684sleep()\u7528\u4e8e\u5c06\u4e3b\u8fdb\u7a0b\u5904\u4e8e\u7b49\u5f85\u72b6\u6001\uff0c\u4ee5\u8ba9\u7ebf\u7a0b\u6267\u884c\u5b8c\u6210\u3002\u6700\u7ec8\u7684\u6267\u884c\u6548\u679c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

    \"\"<\/p>\n

    \u90a3\u4e48\uff0c\u5982\u4f55\u5229\u7528arg\u5411\u5b50\u7ebf\u7a0b\u4f20\u9012\u53c2\u6570\u5462?\u5176\u5177\u4f53\u7684\u5b9e\u73b0\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

    \r\n#include  \r\n#include  \r\n#include  \r\n#include  \r\n \r\nvoid* thread(void *id){ \r\n  pthread_t newthid; \r\n \r\n  newthid = pthread_self(); \r\n  int num = *(int *)id; \r\n  printf(\"this is a new thread, thread ID is %u,id:%d\\n\", newthid, num); \r\n  return NULL; \r\n} \r\n\r\nint main(){ \r\n  \/\/pthread_t thid; \r\n  int num_thread = 5; \r\n  pthread_t *pt = (pthread_t *)malloc(sizeof(pthread_t) * num_thread); \r\n  int * id = (int *)malloc(sizeof(int) * num_thread); \r\n \r\n  printf(\"main thread, ID is %u\\n\", pthread_self()); \r\n  for (int i = 0; i < num_thread; i++){ \r\n     id[i] = i; \r\n     if (pthread_create(&pt[i], NULL, thread, &id[i]) != 0){ \r\n        printf(\"thread create failed!\\n\"); \r\n        return 1; \r\n     } \r\n  } \r\n  sleep(2); \r\n  free(pt); \r\n  free(id); \r\n  return 0; \r\n} \r\n<\/pre>\n

    \u5176\u6700\u7ec8\u7684\u6267\u884c\u6548\u679c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n

    \"\"<\/p>\n

    \u5982\u679c\u5728\u4e3b\u8fdb\u7a0b\u63d0\u524d\u7ed3\u675f\uff0c\u4f1a\u51fa\u73b0\u4ec0\u4e48\u60c5\u51b5\u5462?\u5982\u4e0b\u8ff0\u7684\u4ee3\u7801\uff1a<\/p>\n

    #include  \r\n#include  \r\n#include  \r\n#include  \r\n \r\nvoid* thread(void *id){ \r\n  pthread_t newthid; \r\n \r\n  newthid = pthread_self(); \r\n  int num = *(int *)id; \r\n  printf(\"this is a new thread, thread ID is %u,id:%d\\n\", newthid, num); \r\n  sleep(2); \r\n  printf(\"thread %u is done!\\n\", newthid); \r\n  return NULL; \r\n} \r\n \r\nint main(){ \r\n  \/\/pthread_t thid; \r\nint num_thread = 5; \r\n  pthread_t *pt = (pthread_t *)malloc(sizeof(pthread_t) * num_thread); \r\n  int * id = (int *)malloc(sizeof(int) * num_thread); \r\n \r\n  printf(\"main thread, ID is %u\\n\", pthread_self()); \r\n  for (int i = 0; i < num_thread; i++){ \r\n     id[i] = i; \r\n     if (pthread_create(&pt[i], NULL, thread, &id[i]) != 0){ \r\n        printf(\"thread create failed!\\n\"); \r\n        return 1; \r\n     } \r\n   } \r\n   \/\/sleep(2); \r\n   free(pt); \r\n   free(id); \r\n   return 0; \r\n} \r\n<\/pre>\n

    \u6b64\u65f6\uff0c\u4e3b\u8fdb\u7a0b\u63d0\u524d\u7ed3\u675f\uff0c\u8fdb\u7a0b\u4f1a\u5c06\u8d44\u6e90\u56de\u6536\uff0c\u6b64\u65f6\uff0c\u7ebf\u7a0b\u90fd\u5c06\u9000\u51fa\u6267\u884c\uff0c\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

    \"\"<\/p>\n

    \u7ebf\u7a0b\u6302\u8d77<\/strong><\/div>\n

    \u5728\u4e0a\u8ff0\u7684\u5b9e\u73b0\u8fc7\u7a0b\u4e2d\uff0c\u4e3a\u4e86\u4f7f\u5f97\u4e3b\u7ebf\u7a0b\u80fd\u591f\u7b49\u5f85\u6bcf\u4e00\u4e2a\u5b50\u7ebf\u7a0b\u6267\u884c\u5b8c\u6210\u540e\u518d\u9000\u51fa\uff0c\u4f7f\u7528\u4e86free()\u51fd\u6570\uff0c\u5728Linux\u7684\u591a\u7ebf\u7a0b\u4e2d\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528pthread_join()\u51fd\u6570\u7528\u4e8e\u7b49\u5f85\u5176\u4ed6\u7ebf\u7a0b\uff0c\u51fd\u6570\u7684\u5177\u4f53\u5f62\u5f0f\u4e3a\uff1a<\/p>\n

    int pthread_join(pthread_t thread, void **retval); <\/pre>\n

    \u51fd\u6570pthread_join()\u7528\u6765\u7b49\u5f85\u4e00\u4e2a\u7ebf\u7a0b\u7684\u7ed3\u675f\uff0c\u5176\u8c03\u7528\u8fd9\u5c06\u88ab\u6302\u8d77\u3002<\/p>\n

    \u4e00\u4e2a\u7ebf\u7a0b\u4ec5\u5141\u8bb8\u4e00\u4e2a\u7ebf\u7a0b\u4f7f\u7528pthread_join()\u7b49\u5f85\u5b83\u7684\u7ec8\u6b62\u3002<\/p>\n

    \u5982\u9700\u8981\u5728\u4e3b\u7ebf\u7a0b\u4e2d\u7b49\u5f85\u6bcf\u4e00\u4e2a\u5b50\u7ebf\u7a0b\u7684\u7ed3\u675f\uff0c\u5982\u4e0b\u8ff0\u4ee3\u7801\u6240\u793a\uff1a<\/p>\n

    \r\n#include  \r\n#include  \r\n#include  \r\n#include  \r\n \r\nvoid* thread(void *id){ \r\n  pthread_t newthid; \r\n \r\n  newthid = pthread_self(); \r\n  int num = *(int *)id; \r\n  printf(\"this is a new thread, thread ID is %u,id:%d\\n\", newthid, num); \r\n \r\n  printf(\"thread %u is done\\n\", newthid); \r\n  return NULL; \r\n} \r\nint main(){ \r\n   int num_thread = 5; \r\n   pthread_t *pt = (pthread_t *)malloc(sizeof(pthread_t) * num_thread); \r\n   int * id = (int *)malloc(sizeof(int) * num_thread); \r\n \r\n   printf(\"main thread, ID is %u\\n\", pthread_self()); \r\n   for (int i = 0; i < num_thread; i++){ \r\n      id[i] = i; \r\n      if (pthread_create(&pt[i], NULL, thread, &id[i]) != 0){ \r\n         printf(\"thread create failed!\\n\"); \r\n         return 1; \r\n       } \r\n   } \r\n   for (int i = 0; i < num_thread; i++){ \r\n      pthread_join(pt[i], NULL); \r\n   } \r\n   free(pt); \r\n   free(id); \r\nreturn 0; \r\n} \r\n<\/pre>\n

    \u6700\u7ec8\u7684\u6267\u884c\u6548\u679c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

    \"\"<\/p>\n

    \u6ce8\uff1a\u5728\u7f16\u8bd1\u7684\u65f6\u5019\u9700\u8981\u94fe\u63a5libpthread.a\uff1a<\/p>\n

    g++ xx.c -lpthread -o xx<\/p>\n

    \u4e92\u65a5\u9501mutex<\/strong><\/div>\n
    \u591a\u7ebf\u7a0b\u7684\u95ee\u9898\u5f15\u5165<\/strong><\/div>\n

    \u591a\u7ebf\u7a0b\u7684\u6700\u5927\u7684\u7279\u70b9\u662f\u8d44\u6e90\u7684\u5171\u4eab\uff0c\u4f46\u662f\uff0c\u5f53\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u53bb\u64cd\u4f5c(\u540c\u65f6\u53bb\u6539\u53d8)\u4e00\u4e2a\u4e34\u754c\u8d44\u6e90\u65f6\uff0c\u4f1a\u7834\u574f\u4e34\u754c\u8d44\u6e90\u3002\u5982\u5229\u7528\u591a\u7ebf\u7a0b\u540c\u65f6\u5199\u4e00\u4e2a\u6587\u4ef6\uff1a<\/p>\n

    #include  \r\n#include  \r\n \r\nconst char filename[] = \"hello\"; \r\n \r\nvoid* thread(void *id){ \r\n  int num = *(int *)id; \r\n \r\n  \/\/ \u5199\u6587\u4ef6\u7684\u64cd\u4f5c \r\n  FILE *fp = fopen(filename, \"a+\"); \r\n  int start = *((int *)id); \r\n  int end = start + 1; \r\n  setbuf(fp, NULL);\/\/ \u8bbe\u7f6e\u7f13\u51b2\u533a\u7684\u5927\u5c0f \r\n  fprintf(stdout, \"%d\\n\", start); \r\n  for (int i = (start * 10); i < (end * 10); i ++){ \r\n      fprintf(fp, \"%d\\t\", i); \r\n  } \r\n  fprintf(fp, \"\\n\"); \r\n  fclose(fp); \r\n  return NULL; \r\n} \r\n \r\nint main(){ \r\n   int num_thread = 5; \r\n   pthread_t *pt = (pthread_t *)malloc(sizeof(pthread_t) * num_thread); \r\n   int * id = (int *)malloc(sizeof(int) * num_thread); \r\n \r\n   for (int i = 0; i < num_thread; i++){ \r\n      id[i] = i; \r\n      if (pthread_create(&pt[i], NULL, thread, &id[i]) != 0){ \r\n         printf(\"thread create failed!\\n\"); \r\n         return 1; \r\n         } \r\n   } \r\n   for (int i = 0; i < num_thread; i++){ \r\n      pthread_join(pt[i], NULL); \r\n   } \r\n   \/\/ \u91ca\u653e\u8d44\u6e90 \r\n   free(pt); \r\nfree(id); \r\n   return 0; \r\n} \r\n<\/pre>\n

    \u6267\u884c\u4ee5\u4e0a\u7684\u4ee3\u7801\uff0c\u6211\u4eec\u4f1a\u53d1\u73b0\uff0c\u5f97\u5230\u7684\u7ed3\u679c\u662f\u6df7\u4e71\u7684\uff0c\u51fa\u73b0\u4e0a\u8ff0\u7684\u6700\u4e3b\u8981\u7684\u539f\u56e0\u662f\uff0c\u6211\u4eec\u5728\u7f16\u5199\u591a\u7ebf\u7a0b\u4ee3\u7801\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6bcf\u4e00\u4e2a\u7ebf\u7a0b\u90fd\u5c1d\u8bd5\u53bb\u5199\u540c\u4e00\u4e2a\u6587\u4ef6\uff0c\u8fd9\u6837\u4fbf\u51fa\u73b0\u4e86\u4e0a\u8ff0\u7684\u95ee\u9898\uff0c\u8fd9\u4fbf\u662f\u5171\u4eab\u8d44\u6e90\u7684\u540c\u6b65\u95ee\u9898\uff0c\u5728Linux\u7f16\u7a0b\u4e2d\uff0c\u7ebf\u7a0b\u540c\u6b65\u7684\u5904\u7406\u65b9\u6cd5\u5305\u62ec\uff1a\u4fe1\u53f7\u91cf\uff0c\u4e92\u65a5\u9501\u548c\u6761\u4ef6\u53d8\u91cf\u3002<\/p>\n

    \u4e92\u65a5\u9501<\/strong><\/div>\n

    \u4e92\u65a5\u9501\u662f\u901a\u8fc7\u9501\u7684\u673a\u5236\u6765\u5b9e\u73b0\u7ebf\u7a0b\u95f4\u7684\u540c\u6b65\u95ee\u9898\u3002\u4e92\u65a5\u9501\u7684\u57fa\u672c\u6d41\u7a0b\u4e3a\uff1a<\/p>\n

  • \u521d\u59cb\u5316\u4e00\u4e2a\u4e92\u65a5\u9501\uff1apthread_mutex_init()\u51fd\u6570<\/li>\n
  • \u52a0\u9501\uff1apthread_mutex_lock()\u51fd\u6570\u6216\u8005pthread_mutex_trylock()\u51fd\u6570<\/li>\n
  • \u5bf9\u5171\u4eab\u8d44\u6e90\u7684\u64cd\u4f5c<\/li>\n
  • \u89e3\u9501\uff1apthread_mutex_unlock()\u51fd\u6570<\/li>\n
  • \u6ce8\u9500\u4e92\u65a5\u9501\uff1apthread_mutex_destory()\u51fd\u6570<\/li>\n

    \u5176\u4e2d\uff0c\u5728\u52a0\u9501\u8fc7\u7a0b\u4e2d\uff0cpthread_mutex_lock()\u51fd\u6570\u548cpthread_mutex_trylock()\u51fd\u6570\u7684\u8fc7\u7a0b\u7565\u6709\u4e0d\u540c\uff1a<\/p>\n

  • \u5f53\u4f7f\u7528pthread_mutex_lock()\u51fd\u6570\u8fdb\u884c\u52a0\u9501\u65f6\uff0c\u82e5\u6b64\u65f6\u5df2\u7ecf\u88ab\u9501\uff0c\u5219\u5c1d\u8bd5\u52a0\u9501\u7684\u7ebf\u7a0b\u4f1a\u88ab\u963b\u585e\uff0c\u76f4\u5230\u4e92\u65a5\u9501\u88ab\u5176\u4ed6\u7ebf\u7a0b\u91ca\u653e\uff0c\u5f53pthread_mutex_lock()\u51fd\u6570\u6709\u8fd4\u56de\u503c\u65f6\uff0c\u8bf4\u660e\u52a0\u9501\u6210\u529f;<\/li>\n
  • \u800c\u4f7f\u7528pthread_mutex_trylock()\u51fd\u6570\u8fdb\u884c\u52a0\u9501\u65f6\uff0c\u82e5\u6b64\u65f6\u5df2\u7ecf\u88ab\u9501\uff0c\u5219\u4f1a\u8fd4\u56deEBUSY\u7684\u9519\u8bef\u7801\u3002<\/li>\n

    \u540c\u65f6\uff0c\u89e3\u9501\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4e5f\u9700\u8981\u6ee1\u8db3\u4e24\u4e2a\u6761\u4ef6\uff1a<\/p>\n

  • \u89e3\u9501\u524d\uff0c\u4e92\u65a5\u9501\u5fc5\u987b\u5904\u4e8e\u9501\u5b9a\u72b6\u6001;<\/li>\n
  • \u5fc5\u987b\u7531\u52a0\u9501\u7684\u7ebf\u7a0b\u8fdb\u884c\u89e3\u9501\u3002<\/li>\n

    \u5f53\u4e92\u65a5\u9501\u4f7f\u7528\u5b8c\u6210\u540e\uff0c\u5fc5\u987b\u8fdb\u884c\u6e05\u9664\u3002<\/p>\n

    \u6709\u4e86\u4ee5\u4e0a\u7684\u51c6\u5907\uff0c\u6211\u4eec\u91cd\u65b0\u5b9e\u73b0\u4e0a\u8ff0\u7684\u591a\u7ebf\u7a0b\u5199\u64cd\u4f5c\uff0c\u5176\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

    \r\n#include  \r\n#include  \r\n \r\npthread_mutex_t mutex; \r\n \r\nconst char filename[] = \"hello\"; \r\n \r\nvoid* thread(void *id){ \r\n \r\n   int num = *(int *)id; \r\n   \/\/ \u52a0\u9501 \r\n \r\n   if (pthread_mutex_lock(&mutex) != 0){ \r\n     fprintf(stdout, \"lock error!\\n\"); \r\n   } \r\n   \/\/ \u5199\u6587\u4ef6\u7684\u64cd\u4f5c \r\n   FILE *fp = fopen(filename, \"a+\"); \r\n   int start = *((int *)id); \r\n   int end = start + 1; \r\n   setbuf(fp, NULL);\/\/ \u8bbe\u7f6e\u7f13\u51b2\u533a\u7684\u5927\u5c0f \r\nfprintf(stdout, \"%d\\n\", start); \r\n   for (int i = (start * 10); i < (end * 10); i ++){ \r\n      fprintf(fp, \"%d\\t\", i); \r\n   } \r\n   fprintf(fp, \"\\n\"); \r\n   fclose(fp); \r\n \r\n   \/\/ \u89e3\u9501 \r\n   pthread_mutex_unlock(&mutex); \r\n   return NULL; \r\n} \r\n \r\nint main(){ \r\n   int num_thread = 5; \r\n   pthread_t *pt = (pthread_t *)malloc(sizeof(pthread_t) * num_thread); \r\n   int * id = (int *)malloc(sizeof(int) * num_thread); \r\n \/\/ \u521d\u59cb\u5316\u4e92\u65a5\u9501 \r\n   if (pthread_mutex_init(&mutex, NULL) != 0){ \r\n     \/\/ \u4e92\u65a5\u9501\u521d\u59cb\u5316\u5931\u8d25 \r\n     free(pt); \r\n     free(id); \r\n     return 1; \r\n   } \r\n   for (int i = 0; i < num_thread; i++){ \r\n      id[i] = i; \r\n      if (pthread_create(&pt[i], NULL, thread, &id[i]) != 0){ \r\n         printf(\"thread create failed!\\n\"); \r\n         return 1; \r\n      } \r\n   } \r\n   for (int i = 0; i < num_thread; i++){ \r\n      pthread_join(pt[i], NULL); \r\n   } \r\n   pthread_mutex_destroy(&mutex); \r\n   \/\/ \u91ca\u653e\u8d44\u6e90 \r\n free(pt); \r\n   free(id); \r\n   return 0; \r\n} \r\n<\/pre>\n

    \u6700\u7ec8\u7684\u7ed3\u679c\u4e3a\uff1a<\/p>\n

    \"\"<\/p>\n","protected":false},"excerpt":{"rendered":"

    \u4fe1\u53f7\u91cf\u7528\u5728\u591a\u7ebf\u7a0b\u591a\u4efb\u52a1\u540c\u6b65\u7684\uff0c\u4e00\u4e2a\u7ebf\u7a0b\u5b8c\u6210\u4e86\u67d0\u4e00\u4e2a\u52a8\u4f5c\u5c31\u901a\u8fc7\u4fe1\u53f7\u91cf\u544a\u8bc9\u522b\u7684\u7ebf\u7a0b\uff0c\u522b\u7684\u7ebf\u7a0b\u518d\u8fdb\u884c\u67d0\u4e9b\u52a8\u4f5c(\u5927\u5bb6\u90fd […]<\/p>\n","protected":false},"author":1920,"featured_media":202033,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-202027","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/202027","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/1920"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=202027"}],"version-history":[{"count":2,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/202027\/revisions"}],"predecessor-version":[{"id":202081,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/202027\/revisions\/202081"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/202033"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=202027"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=202027"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=202027"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}