{"rsdb":{"rid":"379051","subhead":"","postdate":"0","aid":"264982","fid":"99","uid":"1","topic":"1","content":"
\n
\n

\u4f5c\u8005\uff1a\u5d14\u6653\u5175<\/p> \n <\/blockquote> \n

\u4ece\u4e00\u4e2a\u7ebf\u4e0a\u95ee\u9898\u8bf4\u8d77<\/h2> \n

\u6700\u8fd1\u5728\u7ebf\u4e0a\u9047\u5230\u4e86\u4e00\u4e9b[HMDConfigManager remoteConfigWithAppID:]<\/code>\u5361\u6b7b<\/p> \n

\u521d\u6b65\u5206\u6790<\/h3> \n

\u89c2\u5bdf\u4e86\u4e0b\u4e3b\u7ebf\u7a0b\u5806\u6808\uff0c\u7528\u5230\u7684\u9501\u662f\u8bfb\u5199\u9501<\/p> \n

\"\u56fe\u7247\"<\/p> \n

\u968f\u540e\u53c8\u53bb\u7ffb\u4e86\u4e0b\u6301\u6709\u7740\u9501\u7684\u5b50\u7ebf\u7a0b\uff0c\u6709\u5404\u79cd\u5404\u6837\u7684\u60c5\u51b5\uff0c\u4e14\u57fa\u672c\u90fd\u5904\u4e8e\u6b63\u5e38\u7684\u6267\u884c\u72b6\u6001\uff0c\u4f8b\u5982\u6709\u7684\u5904\u4e8e\u6253\u5f00\u6587\u4ef6\u72b6\u6001\uff0c\u6709\u7684\u5904\u4e8eread<\/code>\u72b6\u6001\uff0c\u6709\u7684\u6b63\u5728\u6267\u884cNSUserDefaults<\/code>\u7684\u65b9\u6cd5\u2026\"\u56fe\u7247\"\"\u56fe\u7247\"\"\u56fe\u7247\"\u901a\u8fc7\u89c2\u5bdf\u53d1\u73b0\uff0c\u51fa\u95ee\u9898\u7684\u7ebf\u7a0b\u90fd\u6709QOS:BACKGROUND<\/code>\u6807\u8bb0\u3002\u6574\u4f53\u770b\u8d77\u6765\u6301\u6709\u9501\u7684\u5b50\u7ebf\u7a0b\u4ecd\u7136\u5728\u6267\u884c\uff0c\u53ea\u662f\u7559\u7ed9\u4e3b\u7ebf\u7a0b\u7684\u65f6\u95f4\u4e0d\u591f\u4e86\u3002\u4e3a\u4ec0\u4e48\u8fd9\u4e9b\u5b50\u7ebf\u7a0b\u5728\u6301\u6709\u9501\u7684\u60c5\u51b5\u4e0b\uff0c\u9700\u8981\u6267\u884c\u8fd9\u4e48\u4e45\uff0c\u76f4\u5230\u4e3b\u7ebf\u7a0b\u76848s\u5361\u6b7b\uff1f\u4e00\u79cd\u60c5\u51b5\u5c31\u662f\u771f\u7684\u5982\u6b64\u8017\u65f6\uff0c\u53e6\u4e00\u79cd\u5219\u662f\u51fa\u73b0\u4e86\u4f18\u5148\u7ea7\u53cd\u8f6c\u3002<\/p> \n

\u89e3\u51b3\u529e\u6cd5<\/h3> \n

\u5728\u8fd9\u4e2a\u6848\u4f8b\u91cc\u9762\uff0c\u6301\u6709\u8bfb\u5199\u9501\u4e14\u4f18\u5148\u7ea7\u4f4e\u7684\u7ebf\u7a0b\u8fdf\u8fdf\u5f97\u4e0d\u5230\u8c03\u5ea6\uff08\u53c8\u6216\u8005\u5f97\u5230\u8c03\u5ea6\u7684\u65f6\u5019\u53c8\u88ab\u62a2\u5360\u4e86\uff0c\u6216\u8005\u5f97\u5230\u8c03\u5ea6\u7684\u65f6\u5019\u65f6\u95f4\u5df2\u7136\u4e0d\u591f\u4e86\uff09\uff0c\u800c\u5177\u6709\u9ad8\u4f18\u5148\u7ea7\u7684\u7ebf\u7a0b\u7531\u4e8e\u62ff\u4e0d\u5230\u8bfb\u5199\u9501\uff0c\u4e00\u76f4\u88ab\u963b\u585e\uff0c\u6240\u4ee5\u4e92\u76f8\u6b7b\u9501\u3002iOS8<\/code>\u4e4b\u540e\u5f15\u5165\u4e86QualityOfService<\/code>\u7684\u6982\u5ff5\uff0c\u7c7b\u4f3c\u4e8e\u7ebf\u7a0b\u7684\u4f18\u5148\u7ea7\uff0c\u8bbe\u7f6e\u4e0d\u540c\u7684QualityOfService<\/code>\u7684\u503c\u540e\u7cfb\u7edf\u4f1a\u5206\u914d\u4e0d\u540c\u7684CPU<\/code>\u65f6\u95f4\u3001\u7f51\u7edc\u8d44\u6e90\u548c\u786c\u76d8\u8d44\u6e90\u7b49\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e2a\u8bbe\u7f6e\u961f\u5217\u7684\u4f18\u5148\u7ea7 \u3002<\/p> \n

\u65b9\u6848\u4e00\uff1a\u53bb\u9664\u5bf9NSOperationQueue<\/code>\u7684\u4f18\u5148\u7ea7\u8bbe\u7f6e<\/h4> \n

\u5728 Threading Programming Guide \u6587\u6863\u4e2d\uff0c\u82f9\u679c\u7ed9\u51fa\u4e86\u63d0\u793a\uff1a<\/p> \n

\n

Important<\/strong>: It is generally a good idea to leave the priorities of your threads at their default values. Increasing the priorities of some threads also increases the likelihood of starvation among lower-priority threads. If your application contains high-priority and low-priority threads that must interact with each other, the starvation of lower-priority threads may block other threads and create performance bottlenecks.<\/p> \n <\/blockquote> \n

\u82f9\u679c\u7684\u5efa\u8bae\u662f\u4e0d\u8981\u968f\u610f\u4fee\u6539\u7ebf\u7a0b\u7684\u4f18\u5148\u7ea7\uff0c\u5c24\u5176\u662f\u8fd9\u4e9b\u9ad8\u4f4e\u4f18\u5148\u7ea7\u7ebf\u7a0b\u4e4b\u95f4\u5b58\u5728\u4e34\u754c\u8d44\u6e90\u7ade\u4e89\u7684\u60c5\u51b5\u3002\u6240\u4ee5\u5220\u9664\u76f8\u5173\u4f18\u5148\u7ea7\u8bbe\u7f6e\u4ee3\u7801\u5373\u53ef\u89e3\u51b3\u95ee\u9898\u3002<\/p> \n

\u65b9\u6848\u4e8c\uff1a\u4e34\u65f6\u4fee\u6539\u7ebf\u7a0b\u4f18\u5148\u7ea7<\/h4> \n

\u5728 pthread_rwlock_rdlock(3pthread) \u53d1\u73b0\u4e86\u5982\u4e0b\u63d0\u793a\uff1a<\/p> \n

\n

Realtime applications may encounter priority inversion when using read-write locks<\/strong>. The problem occurs when a high priority thread \u201clocks\u201d a read-write lock that is about to be \u201cunlocked\u201d by a low priority thread, but the low priority thread is preempted by a medium priority thread. This scenario leads to priority inversion; a high priority thread is blocked by lower priority threads for an unlimited period of time. During system design, realtime programmers must take into account the possibility of this kind of priority inversion. They can deal with it in a number of ways, such as by having critical sections that are guarded by read-write locks execute at a high priority, so that a thread cannot be preempted while executing in its critical section<\/strong>.<\/p> \n <\/blockquote> \n

\u5c3d\u7ba1\u9488\u5bf9\u7684\u662f\u5b9e\u65f6\u7cfb\u7edf\uff0c\u4f46\u662f\u8fd8\u662f\u6709\u4e00\u4e9b\u542f\u793a\u548c\u5e2e\u52a9\u3002\u6309\u7167\u63d0\u793a\uff0c\u5bf9\u6709\u95ee\u9898\u7684\u4ee3\u7801\u8fdb\u884c\u4e86\u4fee\u6539\uff1a\u5728\u7ebf\u7a0b\u901a\u8fc7pthread_rwlock_wrlock<\/code>\u62ff\u5230_rwlock<\/code>\u7684\u65f6\u5019\uff0c\u4e34\u65f6\u63d0\u5347\u5176\u4f18\u5148\u7ea7\uff0c\u5728\u91ca\u653e_rwlock<\/code>\u4e4b\u540e\uff0c\u6062\u590d\u5176\u539f\u5148\u7684\u4f18\u5148\u7ea7<\/strong>\u3002<\/p> \n

- (id)remoteConfigWithAppID:(NSString *)appID\n{\n    .......\n    pthread_rwlock_rdlock(&_rwlock);\n    HMDHeimdallrConfig *result = ....... \/\/ get existing config\n    pthread_rwlock_unlock(&_rwlock);\n    \n    if(result == nil) {\n        result = [[HMDHeimdallrConfig alloc] init]; \/\/ make a new config\n        pthread_rwlock_wrlock(&_rwlock);\n        \n        qos_class_t oldQos = qos_class_self();\n        BOOL needRecover = NO;\n        \n        \/\/ \u4e34\u65f6\u63d0\u5347\u7ebf\u7a0b\u4f18\u5148\u7ea7\n        if (_enablePriorityInversionProtection && oldQos < QOS_CLASS_USER_INTERACTIVE) {\n            int ret = pthread_set_qos_class_self_np(QOS_CLASS_USER_INTERACTIVE, 0);\n            needRecover = (ret == 0);\n        }\n            \n        ......\n\n        pthread_rwlock_unlock(&_rwlock);\n        \n        \/\/ \u6062\u590d\u7ebf\u7a0b\u4f18\u5148\u7ea7\n        if (_enablePriorityInversionProtection && needRecover) {\n            pthread_set_qos_class_self_np(oldQos, 0);\n        }\n    }\n    \n    return result;\n}<\/code><\/pre> \n 
\n

\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u91cc\u53ea\u80fd\u4f7f\u7528pthread<\/code>\u7684api<\/code>\uff0cNSThread<\/code>\u63d0\u4f9b\u7684API<\/code>\u662f\u4e0d\u53ef\u884c\u7684<\/p> \n <\/blockquote> \n

Demo \u9a8c\u8bc1<\/h3> \n

\u4e3a\u4e86\u9a8c\u8bc1\u4e0a\u8ff0\u7684\u624b\u52a8\u8c03\u6574\u7ebf\u7a0b\u4f18\u5148\u7ea7\u662f\u5426\u6709\u4e00\u5b9a\u7684\u6548\u679c\uff0c\u8fd9\u91cc\u901a\u8fc7demo<\/code>\u8fdb\u884c\u672c\u5730\u5b9e\u9a8c\uff1a\u5b9a\u4e49\u4e862000<\/code>\u4e2aoperation<\/code>\uff08\u76ee\u7684\u662f\u4e3a\u4e86CPU<\/code>\u7e41\u5fd9\uff09\uff0c\u4f18\u5148\u7ea7\u8bbe\u7f6eNSQualityOfServiceUserInitiated<\/code>\uff0c\u4e14\u5bf9\u5176\u4e2d\u53ef\u4ee5\u88ab100<\/code>\u6574\u9664\u7684operation<\/code>\u7684\u4f18\u5148\u7ea7\u8c03\u6574\u4e3aNSQualityOfServiceBackground<\/code>\uff0c\u5728\u6bcf\u4e2aoperation<\/code>\u6267\u884c\u76f8\u540c\u7684\u8017\u65f6\u4efb\u52a1\uff0c\u7136\u540e\u5bf9\u8fd9\u88ab\u9009\u4e2d\u768410<\/code>\u4e2aoperation<\/code>\u8fdb\u884c\u8017\u65f6\u7edf\u8ba1\u3002<\/p> \n

for (int j = 0; j < 2000; ++j) {\n    NSOperationQueue *operation = [[NSOperationQueue alloc] init];\n    operation.maxConcurrentOperationCount = 1;\n    operation.qualityOfService = NSQualityOfServiceUs","orderid":"0","title":"\u4f18\u5148\u7ea7\u53cd\u8f6c\u90a3\u4e9b\u4e8b\u513f(\u4e00)","smalltitle":"","mid":"0","fname":"iOS","special_id":"0","bak_id":"0","info":"0","hits":"673","pages":"15","comments":"0","posttime":"2023-07-23 13:26:40","list":"1690090000","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":"\u90a3\u4e9b\u4e8b<\/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":"119.59.235.169","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"\u4f18\u5148\u7ea7\u53cd\u8f6c\u90a3\u4e9b\u4e8b\u513f","lastview":"1714870004","digg_num":"0","digg_time":"0","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}