Multi-Threaded Application Development - Starting Out

Status
Not open for further replies.
For those of you who seem to be the programming gods of the forum, I've been searching for a while a good source to start out learning about multithreaded applications. Most of the sites I see have a good textual explination of spin-locks, semephores, etc...but after reading them I feel empty that I can't see what sort of code does this. I mean, simple examples like fibonacci calculations, or stuff like that, using multi-threads.

Does anyone have any leads or good sources. Not just sites, books / articles are welcome too. Thanks.

-Wallace
 

Coleman

Ars Legatus Legionis
16,790
Subscriptor
*trying to visualize multi-threaded fibonacci*
I wouldn't think that it would be a good example task to make multi-threaded. It seems(just at a glance to me) that you'd have too much blocking since the next calculation is dependent on the results of the previous. And it's not really a branching calc so branch prediction wouldn't help either.

Although I suppose you could make a couple of calculation threads that utilize memory interface threads...hmm optimized memory access for fibinocci calculations?

Hey look at how far off the original topic we've gotten. Oops.

-J
 

earl

Well-known member
3,066
In general, and this is just my opinion, multi-threading is used far too often and it tends to cause problems. I much prefer multiple processes and using IPC. The one common exception is user interfaces: worker threads are often nice to allow GUIs to remain responsive to users while work is being done.

The problem with threads is the fact that they remove separate memory spaces allowing you to corrupt memory and giving you all sorts of potential race conditions. Using multiple processes with explicit (as opposed to implicit in threads) IPC prevents most of these problems. If you later need to make multiple processes into threads for speed reasons, this is then easier as you have already identified every area where you have to communicate.

-earl-
 

Stardog

Ars Tribunus Militum
2,951
Unless he means the Fibonacci calculations run in one thread while the GUI is monitored by another. That would actually be very helpful in illustrating the basic hows and whys of multithreading. I do stuff like that for a couple of apps I wrote that do some heavy math so I can maintain control of my window while the heavy lifting occurs in the background.
 

Ember75

Ars Scholae Palatinae
696
The problem with threads is the fact that they remove separate memory spaces allowing you to corrupt memory and giving you all sorts of potential race conditions. Using multiple processes with explicit (as opposed to implicit in threads) IPC prevents most of these problems. If you later need to make multiple processes into threads for speed reasons, this is then easier as you have already identified every area where you have to communicate.

I am a novice programmer so you'll have to give me a little latitude. But I thought (from reading information linked in the thread) that the whole point of using threads instead of seperate processes was to avoid the extra storage and communication overhead associated with seperate processes and IPC.

Although I can see your point in regards to sloppy programmers trampling all over their shared memory area and changing data that was not meant to be changed.......threads are only a tool and are only as safe and efficient as the person implementing/using them.
 

earl

Well-known member
3,066
Right, but threads are hard to get right and are notoriously hard to debug. And for novice programmers, that's a bad combination.

Compare the overhead of IPC (using for multiple processes) and thread synchronization objects (mutexes, semaphores, signals, etc.) The difference in overhead isn't that great IMO.

And (although I don't like making this argument), computers are now so powerful for most tasks that the overhead involved with multiple processes isn't a problem. Especially for only a few processes. Also, you have to take into consideration the design of windows, where processes are far heavier than they are under Unix.

And as I mentioned before, multiple processes forces programmers to decide exactly where communication has to take place as you have to make the communication explicit. Therefore, they work nicely as development aids, especially since they are worlds easier to debug. If you later decide to switch to threads for speed reasons (more likely to be a problem under windows), you already have marked for you everywhere that the processes need to commmunicate -- making it far easier to switch in your thread synchronization objects.

-earl-
 

-Triple-

Ars Tribunus Angusticlavius
8,554
I have an idea for a simple task that shows the basics of multithreading... he could try and write a chat client/server (I know it's hackneyed, but it works well) with basic C-runtime Winsock (not MFC) or *nix socket functions. Spin off a thread to accept connections, threads for client I/O, etc.. Would that be good for him, or too complex?
 

DaisyMan

Ars Tribunus Militum
2,135
earl, I would think that any time you're responding to many external stimuli in a short period of time, such as when spawning a *something* to respond to dynamic website request, threads would seem quite preferable to processes in my mind, since the processes would have to be torn up and ripped down repeatedly. And if there is a lot of data which is associated with whatevern you're processing, sending it hither and thither would seem less efficient than synchronizing access to the same data.

Ideally, it's best if you can let something else manage the threading and synchronization granularity for you, such as is the case with COM+ or MTS objects. You write semi-state-agnostic components, and let the backend database or COM itself decide when to lock and unlock access to the various bits.

If you do have to get down and dirty with threads, I'd just like to point out that Delphi's TThread system abstracts away a lot of the headaches associated with synchronization, and is also an easy way to port the threading through to Kylix without having to work out the different API calls.
 

Ember75

Ars Scholae Palatinae
696
Earl,
I can see where you're coming from on the matter. Actually this is all theoretical for me anyway as I've never written a multi-threaded application nor one that uses multiple processes communicating between one another.

Actually I'm mostly just trying to get a feel for the subject matter. I appreciate your input to the thread.

Oh and DaisyMan throws out some good points as well....along the lines of what I was trying to contribute in my own inexperienced way...hehehe.

P.S what's Kylix?
 

earl

Well-known member
3,066
DaisyMan: in terms of quick response, one would use either preallocated thread/process pools, so the point would be moot. It does come down to a question of just how much data sharing has to be done. In the classic example of an http server, not much, so it works out nicely for process pools. If you have to share a lot of data, threads can work out better. But I stand by my earlier contention that it's easier to eat the overhead of writing processes during development and once you have it working, changing to a threaded model.

Threads do work out very nicely for some things: doing work for GUIs (as I mentioned), or blocking system calls (like berkeley sockets). Using threads tends to be less complex than nonblocking or asynchronous I/O -- so just spawn a recieve thread that puts data into an internal buffer and block on recv/recvfrom/select. You can create a nice reader/writer model, where there is only a limited touching point between the two threads so all you have to protect is access to your shared buffer.

And I disagree that threads or even processes work out nicely for programs that are very math heavy. Most of the point of threads/processes is to avoid halting processing while waiting -- on a blocking system call, I/O, for input, etc. If you are in a tight loop doing math, you won't gain any efficiency from threads/processes because you will normally be using your full quanta instead of yielding because you blocked.

Kylix is a version of Delphi for linux.

-earl-
 
The man asked for references. Let's give him some instead of getting up on our soapboxes.

Here's a fine book with actual Win32 API calls and explanations:

http://www.amazon.com/exec/obidos/ASIN/0201442345/qid=1006992863/sr=8-1/ref=sr_8_3_1/103-5342177-3766209

_Multithreading Applications in Win32: The Complete Guide to Threads_, by Beveridge et. al.

This book has clear, concise explanations of when NOT to use threads, when to use threads, and how to synchronize everything. Lean the difference between a mutex and a semaphore... There is a used copy available for less than $30. I've read this book and highly recommend it.

$0.02,
tribble
 

DaisyMan

Ars Tribunus Militum
2,135
Oh yeah, references! Personally, I like the ancient Advanced Windows by Jeffrey Richter I've had around here for years. He just seems to have an excellent understanding of the issues, and really reenforces them well.

Now, back to the SB :D ...

I didn't think most people who would be using processes for multiprocessing are really thinking first of preallocation. In the UNIX world, for instance, I was under the impression that the fork call, with all of its associated overhead in copying the memory, handles, and other state bits, was quite popular.

I suppose process pools, if utilized, could improve matter somewhat. But it just seems so messy have all those zombie processes hanging around, as opposed to the more encapsulated thread pool model. The COM+ model is still my fave, though. :)

Kylix is Delphi for Linux. Its CLX library a great example of cross-platform abstraction. There's talk about a Mac version, as well; not that I own one, but it's nice to know ;)
 
Status
Not open for further replies.