Last active
April 21, 2017 07:22
-
-
Save hythof/449ffe81cd8f124c8950 to your computer and use it in GitHub Desktop.
マルチスレッドで高速なC#を書くためのロック戦略 ref: http://qiita.com/tadokoro/items/28b3623a5ec58517d431
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int incrementedValue = Interlocked.Increment(ref intValue); // 安全にインクリメント |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class X { | |
object lockObject = new object(); | |
public void Work() { | |
lock(lockObject) { | |
// ロックの中 | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class X { | |
SemaphoreSlim sem = new SemaphoreSlim(1, 1); | |
public async Task Work() { | |
await sem.WaitAsync().ConfigureAwait(false); | |
try { | |
// ロックの中、awaitもok | |
} finally { | |
sem.Release(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class X { | |
Semaphore sem = new Semaphore(1, 1); | |
public void Work() { | |
sem.WaitOne(); | |
try { | |
// ロックの中、awaitもok | |
} finally { | |
sem.Release(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class X { | |
Mutex mut = new Mutex(); | |
public void Work() { | |
mut.WaitOne(); | |
try { | |
// ロックの中 | |
} finally { | |
mut.ReleaseMutex(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment