Vijay
Vijay

Reputation: 67231

How do I start to use multithread programming?

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?

Upvotes: 13

Views: 17200

Answers (11)

Vivek
Vivek

Reputation: 473

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

http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html

Upvotes: 8

pierrotlefou
pierrotlefou

Reputation: 40721

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

Upvotes: 1

David Joyner
David Joyner

Reputation: 23177

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.

Upvotes: 0

timB33
timB33

Reputation: 1987

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

Upvotes: 0

Dr. Watson
Dr. Watson

Reputation: 3820

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.

Upvotes: 2

mr grumpy
mr grumpy

Reputation: 1553

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

Upvotes: 1

Gambrinus
Gambrinus

Reputation: 2136

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.

Upvotes: 1

Rohit Banga
Rohit Banga

Reputation: 18918

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

Upvotes: 0

Roman Nikitchenko
Roman Nikitchenko

Reputation: 13046

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).

Upvotes: 0

sharkin
sharkin

Reputation: 12498

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.

Upvotes: 0

Dror Helper
Dror Helper

Reputation: 30790

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

Upvotes: 8

Related Questions