Multi-threading n00bery

so i’ve written a bunch of multi-threaded tools so far, but i’m quite sure i’m doing a bunch of dangerous things without knowing it. i know very little about locks, semaphores, all that fun stuff that you kinda need to know unless you wanna hang yourself.

anyone got any tips? learning resources? simple examples of multi-threaded awesomeness?

Just to chime in, I’d like to know more too - anyone have any resources for learning these skills that you would rate as being ‘Awesome?’

I could just pick stuff out of Google, but you can also pick doctors randomly out of the phonebook, and that’s not the most optimal choice either! =)

Ok, last year i was thought the basics of multithreading. Since you have already programmed threaded applications i will not bother with the very basics, if you would like me to let me know. Some of the teaching material included two pictures that make the main problems of multithreading very clear: (All C++ but i imagine it works they same way in every language)

First up, The Mutex and locking threads:

Why would we need a mutex?

  • What if thread one reads in variable 1.
  • Then your OS pauzes the execution of thread one and goes on with thread two.
  • Thread two changes the value of variable 1.
  • The Os switches threads again.
  • Thread one will continue with the original value of the variable.

So we need to synchronize the threads.

In c++ you can use the Boost library, they have a RAII implementation of a mutex. That means that the lock will end when it goes out of scope. What the mutex basically does is tell you operating system that a thread is locked.
So you might have:

  • Thread one tells your OS to lock the mutex.
  • Your OS will do that and knows that the mutex is locked by, and exclusive to thread one.
  • Thread two might ask to lock the mutex.
  • The Os knows the mutex is exclusive to thread one.
  • The Os will put thread two on hold untill thread one releases the mutex.

The DeadLock :eek:

This is a very nasty situation to be in, it is like an infinite loop. It originates like this:

  • Thread one locks resource 1.
  • Thread two locks resource 2.
  • Thread one needs resource 2.
  • Since resource two is locked, the thread cannot continue and your program stalls.

There is no remedy for this except careful programming.

I don’t know any more so i hope this was helpfull. I haven’t done any threaded programming myself because it is cumbersome to deal with and difficult as well. Performance gains will probably outweigh the cost when you program a game-engine but otherwise i wouldn’t bother unless i knew it would be the end of all my troubles.


Wendie 99

yeah so thats the reasonably obvious stuff - what isn’t apparent to me is what all the different kinds of locks are about, good rules of thumb to follow, and useful debugging techniques (coz debugging multiple threads seems like 10 times harder than straight up linear program debugging).

incidentally, threading is almost essential if you’re doing UI - esp if the UI is going to be doing compute intensive operations. so if it interacts with geometry, or large amounts of animation data etc… without threading you just have a shitty non-responsive app

Ok well, i’m just a student so that was as far as my knowledge goes. I googled some more info on threads and it basically made my head explode so i’m bailing outta here :smiley:

Thanks for the info about UI programming, i did not know that.

Thread safely :laugh:


Mature Cams

>> thread safely

lolz - i try, but i’m too much a n00b to do so - hence this “thread”.

anyhoo, thanks for trying. hopefully someone here can teach us all a thing or two.

this has got some good info in it…

I think .NET has a built in thread manager. Not sure if it’s something you could use or not.
http://lonerobot.net/?p=50