Help on multithreading

Status
Not open for further replies.

Xanrel

Ars Tribunus Militum
1,617
I'm trying to learn a bit about multithreading because I want to write a program that deals with multiple users concurrently. I've figured out how to spawn a thread using pthread_create() or clone() (I'm using linux), but I don't yet understand how to do things like locking variables and preventing deadlocks, which makes spawning a thread totally useless. So I'm looking for something to read (preferably free) that will explain how it all works. I tried looking around for something, but most of what I found basically said "we'll talk about it, but you won't find the details here" and left me with the concepts and not the syntax.
 

MrHat

Ars Scholae Palatinae
952
Hrm. Get a book, you'll be glad you did. There's an Addison/Wesley one for about $30 that's pretty thorough (the one with the spools of thread on the cover). I don't have it with me right now, or I could give you the exact title/author.

Subtle details are the part of multithreading that will make you pull your hair out. For instance, I spent hours once tracking down a memory leak that was triggered by a debugging macro. Apparently, some genius decided that fprintf should be a thread cancellation point, and I had a debug message between the acquisition of a resource and its release. Of course, it didn't occur to me to turn debugging off to make it disappear. Grr.

Get the book. :p

Edit: If you just want syntax, look at the manual pages. pthread_create, pthread_mutex_init, pthread_cond_init, pthread_join, pthread_cancel, etc. should all be in the libc documentation.
 

Midlands

Ars Praetorian
535
Subscriptor
One perspective is to learn multithreaded programming using Java. Java provides a small set of synchronization primitives (basically just java.lang.Thread, synchronized (Object) { }, Object.wait(), and Object.notify()) that make learning this stuff a lot easier than it would be otherwise.

If you pick it up with Java, you can easily transfer the knowledge to the wider (and much hairier) world of C and C++, where primitives vary by OS and where it's much easier to screw yourself. Most generalized synchronization problems can be solved easily with Java's primitives, and it gives you many facilities (stack traces are invaluable, for one) that make the whole job a lot more straightforward.


Beyond that, yes, definitely pick up a book. Doing serious systems programming in a multithreaded environment is one of the single most conceptually complex tasks you can undertake in programming, and it will pay over and over to have a solid understanding backing you up. And don't just get 'Synchronized Programming With C++' or whatever -- get a real book that will teach you the fundamentals. Learn about deadlocks, starvation, producer/consumer relationships, and memory models; Andy Tanenbaum's Modern Operating Systems has some great chapters on this stuff, among others.
 

green_buddy

Wise, Aged Ars Veteran
126
Originally posted by Midlands:
One perspective is to learn multithreaded programming using Java. Java provides a small set of synchronization primitives (basically just java.lang.Thread, synchronized (Object) { }, Object.wait(), and Object.notify()) that make learning this stuff a _lot_ easier than it would be otherwise.

... Learn about deadlocks, starvation, producer/consumer relationships...

The Java Tutorial (Threads) is a great place to start to check this out. They've got a very complete, yet simplistic explanation of all the different multithreading fundamentals stated above.

-green
 

MrHat

Ars Scholae Palatinae
952
Hey now, Stroustrup does that too

Really? I read most of that thing a year ago, and I must have completely glossed over the quotes.

Time to reread, methinks. :D


If you are working in the *NIX world, look also into fork()'ing, as it's the other way to handle concurrent users.

And it's much easier too, as there's (usually, but with many exceptions) no sharing of memory or locking to do, and everything passes through the kernel's IPC mechanisms.
 
Status
Not open for further replies.