{"id":10021168,"date":"2026-01-06T13:17:21","date_gmt":"2026-01-06T05:17:21","guid":{"rendered":"https:\/\/itmax.cn\/?p=10021168"},"modified":"2026-01-06T13:17:21","modified_gmt":"2026-01-06T05:17:21","slug":"%e4%bd%a0%e7%9c%9f%e7%9a%84%e7%90%86%e8%a7%a3-interlocked-exchange-%e5%90%97%ef%bc%9fc-net-%e5%8e%9f%e5%ad%90%e6%93%8d%e4%bd%9c%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10021168","title":{"rendered":"\u4f60\u771f\u7684\u7406\u89e3 Interlocked.Exchange \u5417\uff1fC#.NET \u539f\u5b50\u64cd\u4f5c\u8be6\u89e3"},"content":{"rendered":"<h3>\u4ec0\u4e48\u662f Interlocked.Exchange\uff1f<\/h3>\n<p><code>Interlocked.Exchange<\/code> \u662f <code>System.Threading.Interlocked<\/code> \u7c7b\u4e2d\u7684\u9759\u6001\u65b9\u6cd5\uff0c\u7528\u4e8e\u539f\u5b50\u5730\u66ff\u6362\uff08\u4ea4\u6362\uff09\u4e00\u4e2a\u53d8\u91cf\u7684\u503c\uff0c\u5e76\u8fd4\u56de\u8be5\u53d8\u91cf\u7684\u65e7\u503c\u3002\u6574\u4e2a\u8bfb-\u6539-\u5199\u8fc7\u7a0b\u662f\u4e0d\u53ef\u5206\u5272\u7684\uff0c\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\u3002<\/p>\n<ul>\n<li>\u6838\u5fc3\u4f5c\u7528\uff1a\u65e0\u9501\u5730\uff08<code>lock-free<\/code>\uff09\u5c06\u53d8\u91cf\u8bbe\u7f6e\u4e3a\u65b0\u503c\uff0c\u540c\u65f6\u83b7\u53d6\u65e7\u503c\u3002<\/li>\n<li>\u5e38\u89c1\u91cd\u8f7d\uff1a<\/li>\n<\/ul>\n<pre><code class=\"csharp\">public static int Exchange(ref int location, int value);\npublic static long Exchange(ref long location, long value);\npublic static float Exchange(ref float location, float value);\npublic static double Exchange(ref double location, double value);\npublic static T Exchange&lt;T&gt;(ref T location, T value) where T : class;  \/\/ \u5f15\u7528\u7c7b\u578b\npublic static object? Exchange(ref object? location, object? newValue);<\/code><\/pre>\n<ul>\n<li>\n<p>\u53c2\u6570\uff1a<\/p>\n<ul>\n<li><code>location<\/code>\uff1a\u8981\u66ff\u6362\u7684\u5171\u4eab\u53d8\u91cf\uff08\u5fc5\u987bref\u4f20\u9012\uff0c\u76f4\u63a5\u64cd\u4f5c\u5185\u5b58\u5730\u5740\uff09\u3002<\/li>\n<li><code>value \/ newValue<\/code>\uff1a\u8981\u5199\u5165\u7684\u65b0\u503c\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u8fd4\u56de\u503c\uff1a\u64cd\u4f5c\u524d\u7684\u65e7\u503c\u3002<\/li>\n<\/ul>\n<p>\u5e95\u5c42\u57fa\u4e8e <code>CPU<\/code> \u7684\u539f\u5b50\u4ea4\u6362\u6307\u4ee4\uff08\u5982 <code>x86<\/code> \u7684 <code>xchg<\/code>\uff09\uff0c\u6027\u80fd\u6781\u9ad8\u3002<\/p>\n<h3>\u4e3a\u4ec0\u4e48\u4f7f\u7528 Interlocked.Exchange\uff1f<\/h3>\n<p>\u666e\u901a\u7684 \u201c\u8bfb\u53d6\u503c + \u8d4b\u503c\u65b0\u503c\u201d \u662f\u4e24\u6b65\u975e\u539f\u5b50\u64cd\u4f5c\uff0c\u591a\u7ebf\u7a0b\u4e0b\u4f1a\u56e0\u7ade\u6001\u6761\u4ef6\u5bfc\u81f4\u903b\u8f91\u9519\u8bef<\/p>\n<ul>\n<li>\u5178\u578b\u95ee\u9898\uff1a<\/li>\n<\/ul>\n<pre><code class=\"csharp\">\/\/ \u975e\u539f\u5b50\u64cd\u4f5c\uff1a\u8bfb\u53d6\u65e7\u503c\u548c\u8d4b\u503c\u65b0\u503c\u4e4b\u95f4\u53ef\u80fd\u88ab\u5176\u4ed6\u7ebf\u7a0b\u6253\u65ad\nprivate static int _flag = 0;\npublic static int UnsafeReplace(int newValue)\n{\n    int oldValue = _flag; \/\/ \u6b65\u9aa41\uff1a\u8bfb\u53d6\u65e7\u503c\n    _flag = newValue;     \/\/ \u6b65\u9aa42\uff1a\u8d4b\u503c\u65b0\u503c\n    return oldValue;      \/\/ \u8fd4\u56de\u65e7\u503c\n}<\/code><\/pre>\n<p>\u82e5\u4e24\u4e2a\u7ebf\u7a0b\u540c\u65f6\u6267\u884c\u4e0a\u8ff0\u4ee3\u7801\uff0c\u53ef\u80fd\u51fa\u73b0 \u201c\u7ebf\u7a0b A \u8bfb\u53d6\u65e7\u503c\u540e\uff0c\u7ebf\u7a0b B \u5df2\u4fee\u6539\u503c\uff0c\u7ebf\u7a0b A \u6700\u7ec8\u8fd4\u56de\u7684\u65e7\u503c\u4e0e\u5b9e\u9645\u8d4b\u503c\u524d\u7684\u65e7\u503c\u4e0d\u4e00\u81f4\u201d \u7684\u95ee\u9898\u3002<br \/><code>Interlocked.Exchange<\/code> \u5c06 \u201c\u8bfb\u53d6\u65e7\u503c + \u8bbe\u7f6e\u65b0\u503c\u201d \u5408\u5e76\u4e3a\u4e0d\u53ef\u4e2d\u65ad\u7684\u539f\u5b50\u64cd\u4f5c\uff0c\u4ece\u5e95\u5c42\u675c\u7edd\u7ade\u6001\u6761\u4ef6\uff0c\u4e14\u65e0\u9700\u963b\u585e\u7ebf\u7a0b\uff08\u65e0\u9501\uff09\u3002<\/p>\n<ul>\n<li>\n<p><code>Exchange<\/code> \u7684\u4f18\u52bf\uff1a<\/p>\n<ul>\n<li>\u539f\u5b50\u6027\uff1a\u8bfb\u53d6\u65e7\u503c\u548c\u5199\u5165\u65b0\u503c\u4e00\u6b65\u5b8c\u6210\u3002<\/li>\n<li>\u9ad8\u6027\u80fd\uff1a\u65e0\u9501\uff0c\u5f00\u9500\u6781\u4f4e\u3002<\/li>\n<li>\u53ef\u89c1\u6027\uff1a\u6240\u6709\u7ebf\u7a0b\u7acb\u5373\u53ef\u89c1\u3002<\/li>\n<li>\u6709\u5e8f\u6027\uff1a\u5199\u5165\u524d\u7684\u64cd\u4f5c\u4e0d\u4f1a\u88ab\u91cd\u6392\u5230\u4e4b\u540e\u3002<\/li>\n<li>\u65e0\u9501\uff1a\u4e0d\u963b\u585e\u7ebf\u7a0b\u3002<\/li>\n<li>\n<p>\u9002\u7528\u573a\u666f\uff1a<\/p>\n<ul>\n<li>\u7ebf\u7a0b\u5b89\u5168\u7684\u72b6\u6001\u5207\u6362\uff08\u5982\u542f\u7528\/\u7981\u7528\u6807\u5fd7\uff09\u3002<\/li>\n<li>\u5b9e\u73b0\u61d2\u52a0\u8f7d\u5355\u4f8b\uff08\u66ff\u6362 <code>null<\/code> \u4e3a\u5b9e\u4f8b\uff09\u3002<\/li>\n<li>\u539f\u5b50\u66f4\u65b0\u5f15\u7528\uff08\u5982\u7f13\u5b58\u5bf9\u8c61\uff09\u3002<\/li>\n<li>\u5b9e\u73b0 <code>SpinLock<\/code> \u6216\u81ea\u5b9a\u4e49\u540c\u6b65\u539f\u8bed\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>\u6838\u5fc3\u539f\u7406\uff1aCPU \u7ea7\u539f\u5b50\u6307\u4ee4<\/h3>\n<p><code>Interlocked.Exchange<\/code> \u7684\u539f\u5b50\u6027\u4f9d\u8d56 <code>CPU<\/code> \u786c\u4ef6\u6307\u4ee4\uff1a<\/p>\n<ul>\n<li><code>x86<\/code> \u67b6\u6784\uff1a\u4f7f\u7528 <code>LOCK XCHG<\/code> \u6307\u4ee4\uff08<code>LOCK<\/code> \u524d\u7f00\u72ec\u5360\u5185\u5b58\u603b\u7ebf\uff0c\u963b\u6b62\u5176\u4ed6 <code>CPU<\/code> \u6838\u5fc3\u4fee\u6539\u8be5\u5185\u5b58\u5730\u5740\uff09\uff1b<\/li>\n<li><code>ARM<\/code> \u67b6\u6784\uff1a\u4f7f\u7528 <code>SWP<\/code>\uff08\u4ea4\u6362\u6307\u4ee4\uff09\u6216 <code>LDXR\/STXR<\/code>\uff08\u52a0\u8f7d &#8211; \u5b58\u50a8\u72ec\u5360\uff09\u6307\u4ee4\u3002<\/li>\n<\/ul>\n<p>\u540c\u65f6\uff0c\u8be5\u64cd\u4f5c\u4f1a\u89e6\u53d1\u5168\u5185\u5b58\u5c4f\u969c\uff08<code>Full Memory fence<\/code>\uff09\uff1a<\/p>\n<ul>\n<li>\u4fdd\u8bc1\u64cd\u4f5c\u524d\u540e\u7684\u5185\u5b58\u8bfb\u5199\u4e0d\u4f1a\u88ab <code>CPU<\/code> \u91cd\u6392\u5e8f\uff1b<\/li>\n<li>\u786e\u4fdd\u6240\u6709\u7ebf\u7a0b\u80fd\u7acb\u5373\u770b\u5230\u53d8\u91cf\u7684\u6700\u65b0\u503c\uff08\u907f\u514d <code>CPU<\/code> \u7f13\u5b58\u5bfc\u81f4\u7684 \u201c\u810f\u8bfb\u201d\uff09\u3002<\/li>\n<\/ul>\n<h3>\u57fa\u7840\u4f7f\u7528\u793a\u4f8b<\/h3>\n<h4>\u6700\u7b80\u5355\u7684\u539f\u5b50\u66ff\u6362\uff08\u8fd4\u56de\u539f\u59cb\u503c\uff09<\/h4>\n<pre><code class=\"csharp\">private static int _counter = 0;\n\npublic static void TestExchange()\n{\n    \/\/ \u76ee\u6807\uff1a\u5c06_counter\u4ece0\u66ff\u6362\u4e3a100\uff0c\u83b7\u53d6\u539f\u59cb\u503c\n    int newValue = 100;\n    int originalValue = Interlocked.Exchange(ref _counter, newValue);\n    \n    Console.WriteLine($&quot;\u539f\u59cb\u503c\uff1a{originalValue}\uff0c\u65b0\u503c\uff1a{_counter}&quot;);\n    \/\/ \u8f93\u51fa\uff1a\u539f\u59cb\u503c\uff1a0\uff0c\u65b0\u503c\uff1a100\n\n    \/\/ \u518d\u6b21\u66ff\u6362\uff1a\u5c06_counter\u4ece100\u66ff\u6362\u4e3a200\n    originalValue = Interlocked.Exchange(ref _counter, 200);\n    Console.WriteLine($&quot;\u539f\u59cb\u503c\uff1a{originalValue}\uff0c\u65b0\u503c\uff1a{_counter}&quot;);\n    \/\/ \u8f93\u51fa\uff1a\u539f\u59cb\u503c\uff1a100\uff0c\u65b0\u503c\uff1a200\n}<\/code><\/pre>\n<h4>\u7ebf\u7a0b\u5b89\u5168\u6807\u5fd7\u4f4d<\/h4>\n<pre><code class=\"csharp\">private int _isProcessing = 0; \/\/ 0=\u672a\u5904\u7406, 1=\u5904\u7406\u4e2d\n\npublic bool StartProcessing()\n{\n    \/\/ \u5982\u679c\u5f53\u524d\u4e3a0\uff08\u672a\u5904\u7406\uff09\uff0c\u5219\u8bbe\u4e3a1\uff08\u5904\u7406\u4e2d\uff09\u5e76\u8fd4\u56detrue\n    return Interlocked.Exchange(ref _isProcessing, 1) == 0;\n}\n\npublic void EndProcessing()\n{\n    \/\/ \u91cd\u7f6e\u4e3a\u672a\u5904\u7406\u72b6\u6001\n    Interlocked.Exchange(ref _isProcessing, 0);\n}<\/code><\/pre>\n<h4>\u4e00\u6b21\u6027\u8d44\u6e90\u91ca\u653e\uff08\u9632\u6b62\u91cd\u590d Dispose\uff09<\/h4>\n<pre><code class=\"csharp\">private IDisposable? _resource;\n\npublic void Dispose()\n{\n    var res = Interlocked.Exchange(ref _resource, null);\n    res?.Dispose();\n}<\/code><\/pre>\n<h4>\u65e0\u9501\u53d1\u5e03\u5bf9\u8c61\uff08Safe Publication\uff09<\/h4>\n<pre><code class=\"csharp\">private object? _instance;\n\npublic object GetOrCreate()\n{\n    if (_instance != null)\n        return _instance;\n\n    var newObj = new object();\n    Interlocked.Exchange(ref _instance, newObj);\n    return newObj;\n}<\/code><\/pre>\n<h4>\u591a\u7ebf\u7a0b\u4e0b\u7684\u539f\u5b50\u72b6\u6001\u5207\u6362<\/h4>\n<p>\u7528 <code>Exchange<\/code> \u5b9e\u73b0\u7ebf\u7a0b\u5b89\u5168\u7684 \u201c\u4e00\u6b21\u6027\u521d\u59cb\u5316\u201d\uff08\u72b6\u6001\u4ece\u672a\u521d\u59cb\u5316\u2192\u5df2\u521d\u59cb\u5316\uff0c\u4ec5\u6267\u884c\u4e00\u6b21\uff09\uff1a<\/p>\n<pre><code class=\"csharp\">\/\/ \u72b6\u6001\u679a\u4e3e\uff1a0=\u672a\u521d\u59cb\u5316\uff0c1=\u521d\u59cb\u5316\u4e2d\uff0c2=\u5df2\u521d\u59cb\u5316\nprivate static int _initState = 0;\n\/\/ \u6a21\u62df\u521d\u59cb\u5316\u6210\u672c\u9ad8\u7684\u5bf9\u8c61\nprivate static ExpensiveObject? _expensiveObj;\n\n\/\/\/ &lt;summary&gt;\n\/\/\/ \u7ebf\u7a0b\u5b89\u5168\u7684\u4e00\u6b21\u6027\u521d\u59cb\u5316\n\/\/\/ &lt;\/summary&gt;\npublic static ExpensiveObject GetExpensiveObject()\n{\n    \/\/ 1. \u539f\u5b50\u66ff\u6362\uff1a\u5c06\u72b6\u6001\u4ece0\uff08\u672a\u521d\u59cb\u5316\uff09\u6539\u4e3a1\uff08\u521d\u59cb\u5316\u4e2d\uff09\n    int originalState = Interlocked.Exchange(ref _initState, 1);\n    \n    \/\/ 2. \u53ea\u6709\u539f\u59cb\u72b6\u6001\u662f0\u7684\u7ebf\u7a0b\uff0c\u6267\u884c\u521d\u59cb\u5316\n    if (originalState == 0)\n    {\n        Console.WriteLine($&quot;\u7ebf\u7a0b{Thread.CurrentThread.ManagedThreadId}\uff1a\u6267\u884c\u521d\u59cb\u5316...&quot;);\n        _expensiveObj = new ExpensiveObject(); \/\/ \u8017\u65f6\u521d\u59cb\u5316\n        \/\/ 3. \u521d\u59cb\u5316\u5b8c\u6210\uff0c\u5c06\u72b6\u6001\u6539\u4e3a2\uff08\u5df2\u521d\u59cb\u5316\uff09\n        Interlocked.Exchange(ref _initState, 2);\n    }\n    else\n    {\n        \/\/ \u5176\u4ed6\u7ebf\u7a0b\u7b49\u5f85\u521d\u59cb\u5316\u5b8c\u6210\n        while (_initState != 2)\n        {\n            Thread.SpinWait(10); \/\/ \u81ea\u65cb\u7b49\u5f85\uff08\u8f7b\u91cf\u7ea7\uff09\n        }\n        Console.WriteLine($&quot;\u7ebf\u7a0b{Thread.CurrentThread.ManagedThreadId}\uff1a\u521d\u59cb\u5316\u5df2\u5b8c\u6210\uff0c\u76f4\u63a5\u8fd4\u56de\u5bf9\u8c61&quot;);\n    }\n    \n    return _expensiveObj!;\n}\n\n\/\/ \u6d4b\u8bd5\uff1a10\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8c03\u7528\uff0c\u4ec51\u4e2a\u7ebf\u7a0b\u6267\u884c\u521d\u59cb\u5316\nvar tasks = Enumerable.Range(0, 10)\n    .Select(_ =&gt; Task.Run(() =&gt; GetExpensiveObject()))\n    .ToList();\nTask.WaitAll(tasks.ToArray());\n\n\/\/ \u6a21\u62df\u521b\u5efa\u6210\u672c\u9ad8\u7684\u5bf9\u8c61\npublic class ExpensiveObject\n{\n    public ExpensiveObject()\n    {\n        Thread.Sleep(1000); \/\/ \u6a21\u62df1\u79d2\u8017\u65f6\u521d\u59cb\u5316\n    }\n}<\/code><\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c<\/p>\n<p>\u4ec5 1 \u4e2a\u7ebf\u7a0b\u6267\u884c\u521d\u59cb\u5316\uff0c\u5176\u4f59\u7ebf\u7a0b\u7b49\u5f85\u540e\u76f4\u63a5\u8fd4\u56de\u5bf9\u8c61\uff1a<\/p>\n<pre><code>\u7ebf\u7a0b3\uff1a\u6267\u884c\u521d\u59cb\u5316...\n\u7ebf\u7a0b4\uff1a\u521d\u59cb\u5316\u5df2\u5b8c\u6210\uff0c\u76f4\u63a5\u8fd4\u56de\u5bf9\u8c61\n\u7ebf\u7a0b5\uff1a\u521d\u59cb\u5316\u5df2\u5b8c\u6210\uff0c\u76f4\u63a5\u8fd4\u56de\u5bf9\u8c61\n...\uff08\u5176\u4f59\u7ebf\u7a0b\u5747\u8f93\u51fa\u6b64\u5185\u5bb9\uff09<\/code><\/pre>\n<h3>\u9ad8\u7ea7\u5e94\u7528\u573a\u666f<\/h3>\n<h4>\u5355\u4f8b\u6a21\u5f0f\uff08\u7ed3\u5408 <code>Exchange<\/code> \u5b9e\u73b0\u61d2\u52a0\u8f7d\uff09<\/h4>\n<p>\u7528 <code>Exchange<\/code> \u66ff\u4ee3 <code>lock<\/code> \u5b9e\u73b0\u9ad8\u6027\u80fd\u5355\u4f8b<\/p>\n<pre><code class=\"csharp\">public sealed class LazySingleton\n{\n    \/\/ \u5355\u4f8b\u5b9e\u4f8b\uff08\u5f15\u7528\u7c7b\u578b\uff09\n    private static LazySingleton? _instance;\n    \/\/ \u521d\u59cb\u5316\u6807\u8bb0\uff1a0=\u672a\u521d\u59cb\u5316\uff0c1=\u5df2\u521d\u59cb\u5316\n    private static int _initialized = 0;\n\n    private LazySingleton() { } \/\/ \u79c1\u6709\u6784\u9020\n\n    public static LazySingleton Instance\n    {\n        get\n        {\n            \/\/ \u539f\u5b50\u66ff\u6362\uff1a\u5c06_initialized\u4ece0\u6539\u4e3a1\uff0c\u4ec5\u7b2c\u4e00\u6b21\u8c03\u7528\u8fd4\u56de0\n            if (Interlocked.Exchange(ref _initialized, 1) == 0)\n            {\n                _instance = new LazySingleton(); \/\/ \u4ec5\u6267\u884c\u4e00\u6b21\u521d\u59cb\u5316\n            }\n            return _instance!;\n        }\n    }\n}<\/code><\/pre>\n<h4>\u53d6\u6d88\u4ee4\u724c\u7684\u539f\u5b50\u91cd\u7f6e<\/h4>\n<p>\u7528 <code>Exchange<\/code> \u539f\u5b50\u66ff\u6362 <code>CancellationTokenSource<\/code>\uff0c\u5b9e\u73b0 \u201c\u53d6\u6d88\u540e\u91cd\u7f6e\u201d \u7684\u7ebf\u7a0b\u5b89\u5168\u903b\u8f91\uff1a<\/p>\n<pre><code class=\"csharp\">private static CancellationTokenSource? _cts = new CancellationTokenSource();\n\n\/\/\/ &lt;summary&gt;\n\/\/\/ \u539f\u5b50\u91cd\u7f6e\u53d6\u6d88\u4ee4\u724c\uff08\u53d6\u6d88\u65e7\u4ee4\u724c\uff0c\u521b\u5efa\u65b0\u4ee4\u724c\uff09\n\/\/\/ &lt;\/summary&gt;\npublic static CancellationToken ResetCts()\n{\n    \/\/ 1. \u539f\u5b50\u66ff\u6362\u65e7\u7684Cts\u4e3a\u65b0\u5b9e\u4f8b\uff0c\u83b7\u53d6\u65e7\u5b9e\u4f8b\n    CancellationTokenSource? oldCts = Interlocked.Exchange(ref _cts, new CancellationTokenSource());\n    \/\/ 2. \u53d6\u6d88\u65e7\u4ee4\u724c\uff08\u907f\u514d\u65e7\u4efb\u52a1\u7ee7\u7eed\u6267\u884c\uff09\n    if (oldCts != null)\n    {\n        oldCts.Cancel();\n        oldCts.Dispose();\n    }\n    \/\/ 3. \u8fd4\u56de\u65b0\u4ee4\u724c\n    return _cts.Token;\n}<\/code><\/pre>\n<h4>\u5f15\u7528\u7c7b\u578b\uff1a\u539f\u5b50\u66ff\u6362\u5bf9\u8c61\u5f15\u7528\uff08\u6700\u5e38\u7528\uff09<\/h4>\n<pre><code class=\"csharp\">private ExpensiveObject? _cache = null;\n\npublic ExpensiveObject GetOrCreate()\n{\n    \/\/ \u5982\u679c\u4e3a null\uff0c\u539f\u5b50\u66ff\u6362\u4e3a\u65b0\u5b9e\u4f8b\n    var newInstance = new ExpensiveObject();\n    var oldInstance = Interlocked.Exchange(ref _cache, newInstance);\n\n    return oldInstance ?? newInstance;  \/\/ \u5982\u679c\u65e7\u503c\u4e3a null\uff0c\u8fd4\u56de\u65b0\u5b9e\u4f8b\n}<\/code><\/pre>\n<h4>\u4e0e Interlocked.CompareExchange \u7684\u5173\u7cfb<\/h4>\n<table>\n<thead>\n<tr>\n<th>\u65b9\u6cd5<\/th>\n<th>\u884c\u4e3a<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Exchange<\/td>\n<td>\u65e0\u6761\u4ef6\u66ff\u6362<\/td>\n<\/tr>\n<tr>\n<td>CompareExchange<\/td>\n<td>\u6761\u4ef6\u66ff\u6362\uff08CAS\uff09<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5bf9\u6bd4\u793a\u4f8b<\/p>\n<pre><code class=\"csharp\">\/\/ Exchange\uff1a\u4e0d\u7ba1\u539f\u6765\u662f\u4ec0\u4e48\uff0c\u90fd\u6362\nInterlocked.Exchange(ref state, 1);\n\n\/\/ CompareExchange\uff1a\u53ea\u6709 state == 0 \u624d\u6362\nInterlocked.CompareExchange(ref state, 1, 0);<\/code><\/pre>\n<h3>\u603b\u7ed3<\/h3>\n<blockquote><p>Interlocked.Exchange \u662f .NET \u4e2d\u6700\u7b80\u5355\u3001\u6700\u5feb\u3001\u6700\u5b89\u5168\u7684\u201c\u539f\u5b50\u66ff\u6362\u201d\u64cd\u4f5c\uff0c<br \/>\u5b83\u662f\u5b9e\u73b0\u4e00\u6b21\u6027\u6267\u884c\u3001\u65e0\u9501\u72b6\u6001\u5207\u6362\u3001\u5b89\u5168\u5bf9\u8c61\u53d1\u5e03\u7684\u57fa\u77f3\u3002<\/p><\/blockquote>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000047523301\">https:\/\/segmentfault.com\/a\/1190000047523301<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ec0\u4e48\u662f Interlocked.Exchange\uff1f Interlocked.Exchange \u662f System [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10021168","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021168","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10021168"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021168\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10021168"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10021168"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10021168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}