C# threading issue  
Author Message
Aaron21





PostPosted: Visual C# General, C# threading issue Top

Ok, I'm having a bit of trouble with this one. Can somebody help me out and perhaps give me a better idea

Problem: In a nutshell, I want to synchronize several different threads but can't. One thread, we'll call it A, produces other multiple threads, B,C, and D. These threads execute asynchronously with A. B,C, and D will synchronize with each other, but not with A. A creates these threads and moves on to do other work, but requires B,C, and D to continue. It executes a wait on a eventwaithandle that only B,C, and D can signal (this eventwaithandle is encapsulated inside of a class. There is only one object of this class used by all of these threads and the primary thread. So they signal the eventwaithandle in this object, but the signal never seems to make it back to the main thread.

I don't know what's wrong with it, and in writing it out, I can see that it definitely looks like a messy solution. Does anyone know a better way to do this particular operation Thanks.

Aaron



Visual C#8  
 
 
Dan Dittenhafer





PostPosted: Visual C# General, C# threading issue Top

Aaron,

It looks like you are on the right track. Could you post some of your code so we can see how you are implementing your cross thread communication. Have you considered waiting on the Thread objects for B,C & D using the Join method Thread A could call threadB.Join, which would cause Thread A to block until threadB exits.

Dan


 
 
Peter Ritchie





PostPosted: Visual C# General, C# threading issue Top

Can A not proceed until B, C, D have completed If so, you might want to use the thread join pattern, via the Thread.Join() method, where thread A would "join" threads B, C, and D, blocking until B, C, D are complete. That saves you from having to create a new kernel object, like an EventWaitHandle and saves you the hassle of threads B, C, D setting the event to single their completion. There's also the problem of the race condition between when the event is signaled and when A does something with that signal and thread B, C, or D still technically "running"--that doesn't exist with Thread.Join.

Otherwise, as suggested, if you post some code people can offer better/more-detailed guidance.