Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Threading tutorial for C++?

61 views
Skip to first unread message

markspace

unread,
Oct 19, 2012, 2:11:02 PM10/19/12
to
Hey all, I've got a little personal project to learn a bit about
multithreading in C++, and I was wondering if anyone would like to give
me a few pointers (no pun intended).

What would you recommend as a good tutorial? Threading seems to be a
bit in flux, still, since standards have been long in coming (no flames
please) and so I was wondering from a practical standpoint what you'd
recommend.

Let's assume a Unix/Linux environment. I'll tackle Windows later.

I have a recommendation for a Boost tutorial, although the article is
really old.

<http://www.drdobbs.com/cpp/the-boostthreads-library/184401518>

There's of course a lot of old pthreads tutorials. I wonder if pthreads
has been obviated by now, however.

<http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html>
<http://codebase.eu/tutorial/posix-threads-c/>

So I'm also wondering how prevalent c++11 is, in your practical opinion.

<http://solarianprogrammer.com/2011/12/16/cpp-11-thread-tutorial/>

What should I tackle first? Some articles I researched seem to say
multithreading can't truly (i.e., correctly) be implemented in a
library. So is pthreads really relevant any more?

<http://www.hpl.hp.com/personal/Hans_Boehm/c++mm/>
<http://www.hpl.hp.com/techreports/2004/HPL-2004-209.html>

Note especially the second link there.

So where should a newb start? Suggestions?




Öö Tiib

unread,
Oct 19, 2012, 2:55:38 PM10/19/12
to
On Friday, 19 October 2012 21:11:06 UTC+3, markspace wrote:
> What should I tackle first? Some articles I researched seem to say
> multithreading can't truly (i.e., correctly) be implemented in a
> library. So is pthreads really relevant any more?

If you want the knowledge gained to have best longevity then you should perhaps study std::thread of c++11 as first thing.

If you want to start more narrow and more practical then take boost::asio first. It deals with one very important topic: asynchronous communication. It uses boost::thread that is somewhat similar to std::thread.

Robert Wessel

unread,
Oct 19, 2012, 3:03:53 PM10/19/12
to
On Fri, 19 Oct 2012 11:11:02 -0700, markspace <-@.> wrote:

>Hey all, I've got a little personal project to learn a bit about
>multithreading in C++, and I was wondering if anyone would like to give
>me a few pointers (no pun intended).
>
>What would you recommend as a good tutorial? Threading seems to be a
>bit in flux, still, since standards have been long in coming (no flames
>please) and so I was wondering from a practical standpoint what you'd
>recommend.
>
>Let's assume a Unix/Linux environment. I'll tackle Windows later.
>
>I have a recommendation for a Boost tutorial, although the article is
>really old.
>
><http://www.drdobbs.com/cpp/the-boostthreads-library/184401518>


The Boost stuff adds some higher level abstractions which can be
useful. It also considerably abstracts the OS's threading API, so
stuff written to Boost.Thread is portable to any system supporting
that library (which include most *nix and Windows).


>There's of course a lot of old pthreads tutorials. I wonder if pthreads
>has been obviated by now, however.
>
><http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html>
><http://codebase.eu/tutorial/posix-threads-c/>


No, pthreads is more-or-less the native threads interface for *nix,
and flavors of pthreads are available on a number of other systems.


>So I'm also wondering how prevalent c++11 is, in your practical opinion.
>
><http://solarianprogrammer.com/2011/12/16/cpp-11-thread-tutorial/>


The standard is only a few months old, and full implementations are
few (although recent versions of GCC are pretty close). OTOH, there's
considerable development, and I'd expect (approximately) full C++11
implementations to become common in the next couple of years. C++11
threads are largely based on Boost.Thread (with some changes and
limitations!), so conversion from that will often be reasonably
straight-forward.


>What should I tackle first? Some articles I researched seem to say
>multithreading can't truly (i.e., correctly) be implemented in a
>library. So is pthreads really relevant any more?
>
><http://www.hpl.hp.com/personal/Hans_Boehm/c++mm/>
><http://www.hpl.hp.com/techreports/2004/HPL-2004-209.html>


Correct - threading cannot be generally implemented as (only) a
library. Doing it properly requires that the compiler, the OS, and
some other bits of the tool chain participate. And the compilers and
whatnot *do*. So if you're using pthreads under Linux, both GCC and
Linux are already involved in the process, so it's *not* just the
pthreads library.


>Note especially the second link there.
>
>So where should a newb start? Suggestions?


Eh... Either pthreads or Boost.Threads, with the edge going to the
latter. With Boost.Threads everything will be much more C++ friendly,
and you have the advantages of portability, but you're a few extra
steps away from what's going on at the system level. You'll also find
more pthreads expertise in the world. Going the C++11 route is
probably plausible too, although you'll be a bit of an explorer at
this point.

Jorgen Grahn

unread,
Oct 20, 2012, 3:31:18 AM10/20/12
to
On Fri, 2012-10-19, markspace wrote:
> Hey all, I've got a little personal project to learn a bit about
> multithreading in C++, and I was wondering if anyone would like to give
> me a few pointers (no pun intended).

My first advice would be to learn when *not* to use it, and instead go
for separate processes or event multiplexing using select(2),
non-blocking I/O and so on. Most use of threads I see in production
code is our of ignorance and/or to hide design flaws. Most such code
is also buggy, and works more or less by accident.

(But I'm unusually negative. I acknowledge that there *are* valid
uses for threads.)

Eric Raymond: "Threads - Threat or Menace?"
http://www.catb.org/esr/writings/taoup/html/ch07s03.html#id2923889

...
> Let's assume a Unix/Linux environment. I'll tackle Windows later.

There's a difference between that and "I'm not interested in Windows
at all". You may have to start over when/if you look at Windows.

...

> There's of course a lot of old pthreads tutorials. I wonder if pthreads
> has been obviated by now, however.

Pthreads are alive and well, and won't go away anytime soon. You
could use that API, but use it in a modern way. I suspect that we've
found better patterns for thread communication since those tutorials
were written.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

ptyxs

unread,
Oct 20, 2012, 9:05:14 AM10/20/12
to Jorgen Grahn
Le 20/10/2012 09:31, Jorgen Grahn a écrit :
> On Fri, 2012-10-19, markspace wrote:
>> Hey all, I've got a little personal project to learn a bit about
>> multithreading in C++, and I was wondering if anyone would like to give
>> me a few pointers (no pun intended).

Probably the best thing to do : study the recent (C++11 compliant) book
by Anthony Williams
C++ Concurrency In Action

Ptyxs


Luca Risolia

unread,
Oct 20, 2012, 9:20:04 AM10/20/12
to
I agree with you. It's one of the best books about concurrency in C++
and about concurrency in general.

markspace

unread,
Oct 20, 2012, 12:14:31 PM10/20/12
to
On 10/20/2012 6:05 AM, ptyxs wrote:
> Le 20/10/2012 09:31, Jorgen Grahn a ᅵcrit :
>> On Fri, 2012-10-19, markspace wrote:
>>> Hey all, I've got a little personal project to learn a bit about
>>> multithreading in C++, and I was wondering if anyone would like to give
>>> me a few pointers (no pun intended).
>
> Probably the best thing to do : study the recent (C++11 compliant) book
> by Anthony Williams
> C++ Concurrency In Action


Thank you! An excellent suggestion.



markspace

unread,
Oct 20, 2012, 12:17:17 PM10/20/12
to
On 10/19/2012 12:03 PM, Robert Wessel wrote:

> Eh... Either pthreads or Boost.Threads, with the edge going to the
> latter. With Boost.Threads everything will be much more C++ friendly,
> and you have the advantages of portability, but you're a few extra
> steps away from what's going on at the system level. You'll also find
> more pthreads expertise in the world. Going the C++11 route is
> probably plausible too, although you'll be a bit of an explorer at
> this point.
>


Thanks to you and Oo Tiib (sorry, I can't do umlats), some good advice
from you two.


0 new messages