+ 1
C# - Unsafe threading
Hello World! :) I'm still learning Threading and I have problems with this little code below. It's quasi-theoretical, I know it's not the best. The question is: it stops in the Main's loop, and I don't know why. More precisely I do, I shoud use it more safely. I have tried locks, etc. I have solved this with two-way signaling. Any discussion would be appreciated, what causes the core of the problem. Have a nice day! https://code.sololearn.com/cFkx5s0xRlJN/?ref=app https://code.sololearn.com/cFkx5s0xRlJN/?ref=app
2 Answers
0
I've just spent a couple weeks trying to understand asynchronous programming in c#, and it's really efficient. Using Tasks, async/await, lock, Parallel library instead of using threads and reset events by hand is much more effective. My problem was that I had a large file to read, and it took massive time to work with. Using the parallel library really facitilate my life.
Also recommended to use System.Collections.Concurrent for thread safe code.
0
Thinking the question further. The Thread.Start() called first, but it needs to change of state of Thread and joins the "need to be processed" queue, which takes time. The Main's loop already running and not wait for the signaling. If I use two-way signaling, and put another pause-signal autoresetevent, or use a simple Thread.Sleep() after signaling, the code exits with the proper output. But it looses the parallel or synchronous usage, and not as elegant to be proud of.