9

I am trying to wrap my head around how threading works, and I believe I might have overcomplicated it.

Original Understanding:

Previous I thought threads were limited to a single class and its sub classes, no class can be shared across threads, and the communication between threads made it real hard for me to understand.

New understanding:

But the more I read about it, it instead seems like threads are just running at a random point in the same code. You can restrict it to certain areas but in theory the same code could run multiple times in each of the threads.

To put it another way, if you see the code as a road system with turns and connections, those are the flow a program can make, the main thread just runs through this. If a new thread is spawned all you do is saying where it should start from, and the code goes from there, can use same classes and commands, so if you structure your code "bad" there is the risk of two pieces of code "crashing", but beside that there is no restrictions on reading from and writing to code, its all part of the same program, just multiple "cars" on the "roads".

You could restrict a thread to stay within a certain area (loop/limited part of code), and only interact at certain places, or you could ignore it and just hope to avoid the "crashes".


With the "New understanding" working with threads it is for me so much easier to wrap my head around, so is it understood correct?

5
  • 7
    Yes, your later understanding is more or less correct! To extend your analogy, thread synchronization functions and objects (e.g. ManualResetEvent and lock) are like traffic lights which prevent the cars colliding. :) Commented Mar 25, 2013 at 9:19
  • 7
    There's an excellent free ebook about threading here: Albahari Commented Mar 25, 2013 at 9:20
  • Thanks for the input from both of you. Suddenly makes it so much clearer for me to work with and wrap my head around. So much simpler ... SO much more dangerous if done wrong :)
    – Taoh
    Commented Mar 25, 2013 at 9:25
  • To be clear, the only risk of CODE 'crashing' is if the code is self-modifying. It's all about the data accessed from that code. Commented Mar 25, 2013 at 12:49
  • Sorry, should have said, program entering a state where it is unable to recover or acts unexpected until a restart has been performed, either due to a deadlock or parameters entering values unforeseen due to 2 or more threads modifying the same variables at the same time due to lack of a proper established mutex :)
    – Taoh
    Commented Mar 25, 2013 at 13:46

1 Answer 1

1

You have pretty much hit it. In general threads are there to do specialized work and won't overlap with each other. If the main thread has to spawn off a sub-thread to do work, the main thread will almost always not also be doing this work.

The areas where they do overlap will need to be "Thread safe" so as not to step on each other's toes; for example multiple worker threads filling up a collection for the main thread. When you're looking up classes on MSDN you'll notice the blurb at the bottom saying whether they have thread safety built in or you'll need to do it yourself through the use of .net's locking, mutexes or semaphores.

1
  • Thanks, were looking at threads at the wrong way earlier. As areas that was separated off from the rest of the program, instead of seeing them as "multiple drivers", and a way to avoid collisions would be to separate them off but not being required to.
    – Taoh
    Commented Mar 25, 2013 at 13:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.