views:

633

answers:

11

I am a beginner on Stack Overflow. I am working on a Unix platform in C/C++. Knowing basic programming in these regards how could I start with multithreading?

Multithreading seems to be very interesting and I want to grow my knowledge in this regard.

How could I get started with multithreading and what are the best techniques/books/ebooks/articles available to grab the concepts as early as possible?

+6  A: 

Study regarding pthread, mutexes and try to implement same that will be beneficial for you.

http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html
Vivek
yolinux is an awesome site with great information regarding what you need.
ChadNC
For stl containers also yolinux consist of very handy information for learning.
Vivek
+1  A: 

I think that the Wikipedia article Multithreading give you a quick overview and by following the external links you'll get a good overview of the topic. After that - or additionally - you could read Tanenbaum's Operating Systems: Design and Implementation (great book by the way). But the most important thing is - in my opinion - to get your hands on it. So just download a sample application from... let's say The Code Project or whatever website you'll find and play around with it. See how the application differs if you use locks or what happens if two threads try to access the same resource and how often this will occur, etc. By that I think you'll get the hang of it pretty quickly. And it's fun to evaluate and play around with techniques that are new to oneself.

Gambrinus
+4  A: 

Learning multi threading programming has two parts:

  1. How to write multi threading applications
  2. How to use the available API (pthread)

Learning multi-threaded programming is harder, thre's a good article published in the Linux Journal that will help you understand the basic principles.

To better understand pThreads I suggest reading this tutorial - POSIX Threads Programming

There is also a good book by O'rielly called PThreads Programming

Dror Helper
I agree with the O'Rielly book... I really clarified my understanding of multi-threaded programming by reading it, even though I ended up using a framework to help with some of the details in C++.
Caleb Huitt - cjhuitt
A: 

Maybe a bit controversial, but multithreading really cklicked for me when I attempted to solve a coding puzzle once.

The puzzle was about writing thread safe code without using mutexes. My first attempts were miserable but when I finally got it, it was like learning to ride a bike - I've never felt unsure about concurrency ever since.

Some times I've even stumbled upon programmers who have read books on the subject, but fails to understand simple things like the fact that a primitive assignment may sometimes not be an atomic operation.

sharkin
Modicom
Was the puzzle's solution a lock-free design, or did it use a locking mechanism (such as Dekker's algorithm) other than a mutex (AKA dijkstra)? if it was a lock-free type of solution, would you mind posting the problem and solution somehow?
San Jacinto
@San Jancinto: I actually don't remember such details about the puzzle. I also regret not saving it back then, this was about 8-9 years ago and the forum where it was posted doesn't exist anymore.
sharkin
Thanks anyway :)
San Jacinto
A: 

If you want to study the details try reading Advanced Programming in the UNIX Environment. or start with Computer Systems: A Programmer's Perspective.

iamrohitbanga
A: 

Everything depends on you goals. There is plenty of code and articles with common multi-threading problems solved based on POSIX threads framework (I see number of recommendations of good articles here). The main question is what do you want to build. For some tasks it is not recommended to go multi-threading at all.

Here is book "Foundations of Multithreaded, Parallel, and Distributed Programming" which is related to discussed topic and which I'd like to recommend. The most significant advantage of it is 'relatively easy to read' style but no hard linkage to POSIX threads ideology (which is common problem).

Roman Nikitchenko
A: 

Something else to try: http://www.threadingbuildingblocks.org

mr grumpy
+2  A: 

If you're getting started with multithreading, my advice would be to first review and better understand I/O on your system. Understand blocking vs. non-blocking I/O, signaling, asynchronous routines, callbacks et cetera. I/O is probably one, if not the primary, reason for adding multithreading to your programs. With that knowledge you can then pick up a book on pthreads or java threads, or wrap your mind around the Boost threads library or another threading library for your favorite technology.

Dr. Watson
A: 

since it's UNIX why not start with processes and IPC comms? i.e. message queues, shared memory and mutexes.

queBurro
A: 

Studying various library frameworks and O/S facilities is a good way to understand low-level concurrency. Examples you find there can get you started writing concurrent code in a short amount of time.

After you debug your way through a few deadlocks and shared memory corruption issues, you will find that you need some tools for reasoning about and decomposing your concurrency problems. I personally like Herb Sutter's The Pillars of Concurrency article as a starting point. The idea is to better understand why you need concurrency. Are you looking for improved response time, parallel calculation, some combination of reasons?

Understanding the "why" will lead you to a better "what". From there you can always spider out to different low-level approaches: Active Objects, monitor objects, message passing, etc. As you said, there is a lot to know in this field.

David Joyner
A: 

Hi,

I found this tutorial very informative and clearly-written. Hope it is helpful.

pierr