Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread safe programming

I keep hearing about thread safe. What is that exactly and how and where can I learn to program thread safe code?

Also, assume I have 2 threads, one that writes to a structure and another one that reads from it. Is that dangerous in any way? Is there anything I should look for? I don't think it is a problem. Both threads will not (well can't ) be accessing the struct at the exact same time..

Also, can someone please tell me how in this example : https://stackoverflow.com/a/5125493/1248779 we are doing a better job in concurrency issues. I don't get it.

like image 553
Kam Avatar asked Mar 05 '12 07:03

Kam


1 Answers

It's a very deep topic. At the heart threads are usually about making things go fast by using multiple cores at the same time; or about doing long operations in the background when you don't have a good way to interleave the operation with a 'primary' thread. The latter being very common in UI programming.

Your scenario is one of the classic trouble spots, and one of the first people run into. It's vary rare to have a struct where the members are truly independent. It's very common to want to modify multiple values in the structure to maintain consistency. Without any precautions it is very possible to modify the first value, then have the other thread read the struct and operate on it before the second value has been written.

Simple example would be a 'point' struct for 2d graphics. You'd like to move the point from [2,2] to [5,6]. If you had a different thread drawing a line to that point you could end up drawing to [5,2] very easily.

This is the tip of the iceberg really. There are lots of great books, but learning this space usually goes something like this:

  1. Uh oh, I just read from that thing in an inconsistent state.
  2. Uh oh, I just modified that thing from 2 threads and now it's garbage.
  3. Yay! I learned about locks
  4. Whoa, I have a lot of locks and everything seems to just hang sometimes when I have lots of them locking in nested code.
  5. Hrm. I need to stop doing this locking on the fly, I seem to be missing a lot of places; so I should encapsulate them in a data structure.
  6. That data structure thing was great, but now I seem to be locking all the time and my code is just as slow as a single thread.
  7. condition variables are weird
  8. It's fast because I got clever with how I lock things. Hrm. Sometimes data corrupts.
  9. Whoa.... InterlockedWhatDidYouSay?
  10. Hey, look no lock, I do this thing called a spin lock.
  11. Condition variables. Hrm... I see.
  12. You know what, how about I just start thinking about how to operate on this stuff in completely independent ways, pipelineing my operations, and having as few cross thread dependencies as possible...

Obviously it's not all about condition variables. But there are many problems that can be solved with threading, and probably almost as many ways to do it, and even more ways to do it wrong.

like image 152
Joe Avatar answered Sep 27 '22 21:09

Joe