[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Threads in C++

Rafael Cunha de Almeida

11/26/2008 2:04:00 PM

I have some experience with pthreads in C and now I feel the need to
use threads in C++. I suppose pthreads could work, but I'm sure there's
a better way. Do you have suggestions of what I should use? I have a
CPU intensive application that can be easily divided into threads, each
thread will be very busy doing calculations and, as the end result, I
expect a value out of each of the threads.
9 Answers

.rhavin grobert

11/26/2008 2:37:00 PM

0

On 26 Nov., 15:03, Rafael Cunha de Almeida <rafa...@dcc.ufmg.br>
wrote:
> I have some experience with pthreads in C and now I feel the need to
> use threads in C++. I suppose pthreads could work, but I'm sure there's
> a better way. Do you have suggestions of what I should use? I have a
> CPU intensive application that can be easily divided into threads, each
> thread will be very busy doing calculations and, as the end result, I
> expect a value out of each of the threads.

as far as im informed, there is no platform-independent way of using
threads in c++

sanjay

11/26/2008 2:53:00 PM

0

On Nov 26, 7:36 pm, ".rhavin grobert" <cl...@yahoo.de> wrote:
> On 26 Nov., 15:03, Rafael Cunha de Almeida <rafa...@dcc.ufmg.br>
> wrote:
>
> > I have some experience with pthreads in C and now I feel the need to
> > use threads in C++. I suppose pthreads could work, but I'm sure there's
> > a better way. Do you have suggestions of what I should use? I have a
> > CPU intensive application that can be easily divided into threads, each
> > thread will be very busy doing calculations and, as the end result, I
> > expect a value out of each of the threads.
>
> as far as im informed, there is no platform-independent way of using
> threads in c++

ACE - framework includes support for threads and is available for many
platforms..

Pierre Yves

11/26/2008 2:55:00 PM

0

I've been using pthreads and it's working well. You should have a look
to the "Advanced Linux Programming" book (available online) which has
been a great reference.

Otherwise, I've used earlier on CommonC++ libraries. You can have
platform independent threads then.

PY

..rhavin grobert previously wrote the following e-mail:
> On 26 Nov., 15:03, Rafael Cunha de Almeida <rafa...@dcc.ufmg.br>
> wrote:
>> I have some experience with pthreads in C and now I feel the need to
>> use threads in C++. I suppose pthreads could work, but I'm sure there's
>> a better way. Do you have suggestions of what I should use? I have a
>> CPU intensive application that can be easily divided into threads, each
>> thread will be very busy doing calculations and, as the end result, I
>> expect a value out of each of the threads.
>
> as far as im informed, there is no platform-independent way of using
> threads in c++

Maxim Yegorushkin

11/26/2008 3:07:00 PM

0

On Nov 26, 2:03 pm, Rafael Cunha de Almeida <rafa...@dcc.ufmg.br>
wrote:
> I have some experience with pthreads in C and now I feel the need to
> use threads in C++. I suppose pthreads could work, but I'm sure there's
> a better way. Do you have suggestions of what I should use? I have a
> CPU intensive application that can be easily divided into threads, each
> thread will be very busy doing calculations and, as the end result, I
> expect a value out of each of the threads.

You can take a look at cross-platform Intel Threading Building Blocks.

--
Max

Boogie

11/26/2008 3:08:00 PM

0

On Nov 26, 3:03 pm, Rafael Cunha de Almeida <rafa...@dcc.ufmg.br>
wrote:
> I have some experience with pthreads in C and now I feel the need to
> use threads in C++. I suppose pthreads could work, but I'm sure there's
> a better way. Do you have suggestions of what I should use? I have a
> CPU intensive application that can be easily divided into threads, each
> thread will be very busy doing calculations and, as the end result, I
> expect a value out of each of the threads.

Maybe try boost::thread ?
I've used them and I think they're ok.

Boogie

Jeff Schwab

11/26/2008 4:10:00 PM

0

Rafael Cunha de Almeida wrote:
> I have some experience with pthreads in C and now I feel the need to
> use threads in C++. I suppose pthreads could work, but I'm sure there's
> a better way. Do you have suggestions of what I should use? I have a
> CPU intensive application that can be easily divided into threads, each
> thread will be very busy doing calculations and, as the end result, I
> expect a value out of each of the threads.

TBB (which Maxim suggested) is made for this. The Intel developers
refer to the management of individual threads as the "assembly language"
of concurrent programming, and provide a much higher-level,
task-oriented architecture with a respectably modern C++ interface. You
get your choice of free or commercial license.

If you just need a few extra threads for specific purposes (e.g. keeping
a GUI responsive, or waiting on a socket), Boost.Threads (as Boogie
suggested) should be fine. The upcoming C++ standard does include
standardized support for threads, which (AIUI) are similar to Boost
threads. Alternatively, wrapping pthreads in simple classes isn't too hard.

Matt P. Dz.

11/26/2008 5:50:00 PM

0

On 11/26/2008 3:36 PM, .rhavin grobert wrote:
> On 26 Nov., 15:03, Rafael Cunha de Almeida <rafa...@dcc.ufmg.br>
> wrote:
>> I have some experience with pthreads in C and now I feel the need to
>> use threads in C++. I suppose pthreads could work, but I'm sure there's
>> a better way. Do you have suggestions of what I should use? I have a
>> CPU intensive application that can be easily divided into threads, each
>> thread will be very busy doing calculations and, as the end result, I
>> expect a value out of each of the threads.
>
> as far as im informed, there is no platform-independent way of using
> threads in c++

Shared memory setting:
OpenMP
http://en.wikipedia.org/w...
http://www.o...
// platform-independent, supported "by default" by GCC and MS Visual
Studio Pro (and many others)

Someone also mentioned Boost.Threads -- I have no experience with this
one, but from the look at documentation & examples it looks much
lower-level than OpenMP (less abstract) and hence the code might be less
elegant/more tedious to write and maintain. YMMV, though.

Distributed memory setting:
MPI
Boost.MPI
http://www.boost.org/doc/libs/1_37_0/doc/htm...
// includes links to MPI implementations -- you'll need one

Best,

Matt

Juha Nieminen

11/27/2008 10:29:00 PM

0

Rafael Cunha de Almeida wrote:
> I have some experience with pthreads in C and now I feel the need to
> use threads in C++. I suppose pthreads could work, but I'm sure there's
> a better way. Do you have suggestions of what I should use? I have a
> CPU intensive application that can be easily divided into threads, each
> thread will be very busy doing calculations and, as the end result, I
> expect a value out of each of the threads.

Two possibilities, both of them rather portable (assuming compiler has
support, of course):

1) The boost threads library.
2) OpenMP.

Maxim Yegorushkin

11/28/2008 10:02:00 AM

0

On Nov 27, 10:29 pm, Juha Nieminen <nos...@thanks.invalid> wrote:
> Rafael Cunha de Almeida wrote:
>
> > I have some experience with pthreads in C and now I feel the need to
> > use threads in C++. I suppose pthreads could work, but I'm sure there's
> > a better way. Do you have suggestions of what I should use? I have a
> > CPU intensive application that can be easily divided into threads, each
> > thread will be very busy doing calculations and, as the end result, I
> > expect a value out of each of the threads.
>
>   Two possibilities, both of them rather portable (assuming compiler has
> support, of course):
>
> 1) The boost threads library.

boost::threads provides a subset of pthreads, so conceptually they are
pretty much the same.

The author mentioned CPU intensive application. In this case, to make
the application scale well with the number of available CPU cores, it
will need to use platform specific calls to find out the number of
available cores to create an appropriate number of worker threads. So,
it may be a bit too low-level.

> 2) OpenMP.

3) Cilk++ http://www.cilk.com/multicore-products/cilk-solution...
4) Threading Building Blocks http://www.threadingbuildingb...

--
Max