Click here to Skip to main content
15,895,011 members
Home / Discussions / C#
   

C#

 
AnswerRe: Classes In Threads - Pause One Until Another Finished Pin
OriginalGriff8-Aug-18 8:09
mveOriginalGriff8-Aug-18 8:09 
GeneralRe: Classes In Threads - Pause One Until Another Finished Pin
Kevin Marois8-Aug-18 8:22
professionalKevin Marois8-Aug-18 8:22 
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 
You have up to three threads modifying the same shared variable at the same time, without any coordination. This will not end well.

For example:
Thread 0: Read compteur == 0
Thread 1: Read compteur == 0
Thread 0: Update compteur = compteur + 1 == 1
Thread 2: Read compteur == 1
Thread 2: Update compteur = compteur + 1 == 2
Thread 1: Update compteur = compteur + 1 == 1

Three threads have incremented the same variable, but the value has only increased by 1.

Things get even more complicated when you start considering memory models, CPU cache, speculative execution, ...

To safely increment the value within your thread, you need to use Interlocked.Increment[^]:
C#
public static void Thread_test() 
{
    sem.Wait();
    
    int result = 0;
    for (int i = 0; i < 1000000; i++)
    {
        result = Interlocked.Increment(compteur);
    }
    
    sem.Release();
    
    System.Console.WriteLine(result);
}

To do anything more complicated, you will need to use locks or other coordination primitives to prevent the shared state from being modified by multiple threads at the same time.



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

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 
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 

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.