Click here to Skip to main content
15,894,955 members
Home / Discussions / C#
   

C#

 
GeneralRe: Classes In Threads - Pause One Until Another Finished Pin
OriginalGriff8-Aug-18 22:08
mveOriginalGriff8-Aug-18 22:08 
GeneralRe: Classes In Threads - Pause One Until Another Finished Pin
Richard Deeming9-Aug-18 0:48
mveRichard Deeming9-Aug-18 0:48 
Questionthreads Pin
Laurent mortroux6-Aug-18 22:44
Laurent mortroux6-Aug-18 22:44 
AnswerRe: threads Pin
OriginalGriff6-Aug-18 23:31
mveOriginalGriff6-Aug-18 23:31 
SuggestionRe: threads Pin
Richard Deeming7-Aug-18 2:29
mveRichard Deeming7-Aug-18 2:29 
AnswerRe: threads Pin
Richard Deeming7-Aug-18 2:28
mveRichard Deeming7-Aug-18 2:28 
GeneralRe: threads Pin
Laurent mortroux7-Aug-18 3:28
Laurent mortroux7-Aug-18 3:28 
GeneralRe: threads Pin
Richard Deeming7-Aug-18 3:48
mveRichard Deeming7-Aug-18 3:48 
The point is that your semaphore allows three threads to run the protected code block at the same time. All three threads are trying to modify the same shared state at the same time.

The ++ operator is not atomic - it reads the value, increments it, and writes it back, which takes three operations. With multiple threads, those operations can be interleaved, resulting in incorrect behaviour.

The Interlocked.Increment operator is atomic. No other thread can sneak code into the middle of the operation.

(Interlocked operations using a 64-bit type on a 32-bit OS will only be atomic with respect to each other. Accessing the value without using the Interlocked class will still have problems.)

For anything more complicated that a single numeric variable, you need to use some kind of coordination primitive to protect the shared state. What you use will depend on how the state will be accessed. For example, if most accesses are reading the state, and only a few are modifying the state, a ReaderWriterLockSlim[^] would probably be a good choice.

Ultimately, the best option is to avoid shared state wherever possible.

Threading in C# - Part 2 - Basic Synchronization[^]



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

QuestionLG Smart TV webos 3.0 .net framework client development Pin
impeham6-Aug-18 12:05
impeham6-Aug-18 12:05 
AnswerRe: LG Smart TV webos 3.0 .net framework client development Pin
Simon_Whale7-Aug-18 4:22
Simon_Whale7-Aug-18 4:22 
QuestionHow do you color cell of the listview ? Pin
Member 24584675-Aug-18 17:51
Member 24584675-Aug-18 17:51 
AnswerRe: How do you color cell of the listview ? Pin
Mycroft Holmes5-Aug-18 19:43
professionalMycroft Holmes5-Aug-18 19:43 
AnswerRe: How do you color cell of the listview ? Pin
OriginalGriff5-Aug-18 20:27
mveOriginalGriff5-Aug-18 20:27 
GeneralRe: How do you color cell of the listview ? Pin
Member 24584678-Aug-18 15:33
Member 24584678-Aug-18 15:33 
QuestionSelectedIndex doesn't work with the SelectionChanged event handler but it works with the ButtonClick event handler. Pin
Member 138463763-Aug-18 22:32
Member 138463763-Aug-18 22:32 
AnswerRe: SelectedIndex doesn't work with the SelectionChanged event handler but it works with the ButtonClick event handler. Pin
OriginalGriff3-Aug-18 22:40
mveOriginalGriff3-Aug-18 22:40 
GeneralRe: SelectedIndex doesn't work with the SelectionChanged event handler but it works with the ButtonClick event handler. Pin
Member 138463763-Aug-18 23:11
Member 138463763-Aug-18 23:11 
GeneralRe: SelectedIndex doesn't work with the SelectionChanged event handler but it works with the ButtonClick event handler. Pin
OriginalGriff3-Aug-18 23:22
mveOriginalGriff3-Aug-18 23:22 
QuestionRe: SelectedIndex doesn't work with the SelectionChanged event handler but it works with the ButtonClick event handler. Pin
Eddy Vluggen3-Aug-18 23:41
professionalEddy Vluggen3-Aug-18 23:41 
AnswerRe: SelectedIndex doesn't work with the SelectionChanged event handler but it works with the ButtonClick event handler. Pin
OriginalGriff4-Aug-18 0:04
mveOriginalGriff4-Aug-18 0:04 
GeneralRe: SelectedIndex doesn't work with the SelectionChanged event handler but it works with the ButtonClick event handler. Pin
Eddy Vluggen4-Aug-18 0:17
professionalEddy Vluggen4-Aug-18 0:17 
AnswerRe: SelectedIndex doesn't work with the SelectionChanged event handler but it works with the ButtonClick event handler. Pin
Member 138463764-Aug-18 2:21
Member 138463764-Aug-18 2:21 
GeneralRe: SelectedIndex doesn't work with the SelectionChanged event handler but it works with the ButtonClick event handler. Pin
OriginalGriff4-Aug-18 2:28
mveOriginalGriff4-Aug-18 2:28 
GeneralRe: SelectedIndex doesn't work with the SelectionChanged event handler but it works with the ButtonClick event handler. Pin
Member 138463764-Aug-18 3:18
Member 138463764-Aug-18 3:18 
GeneralRe: SelectedIndex doesn't work with the SelectionChanged event handler but it works with the ButtonClick event handler. Pin
OriginalGriff4-Aug-18 3:30
mveOriginalGriff4-Aug-18 3:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.