Boost logo

Boost Users :

Subject: Re: [Boost-users] Thread object pointers.
From: Nathan Crookston (nathan.crookston_at_[hidden])
Date: 2010-08-03 20:04:00


Hi Albert,

On Tue, Aug 3, 2010 at 3:10 PM, Albert Schueller <schuelaw_at_[hidden]> wrote:
> When I run LISTING 1 it appears that the threads are acting
> independently as I would hope.  When I run LISTING 2, it seems that the
> threads are not independent, but rather thrd1 does its job, followed by
> thrd2, followed by thrd3.
>
> Am I not locking properly?
>

Could it be that:
>  for(i=0;i<num_thrds;i++) {
>   thrd[i] = new boost::thread(count(i+1));
>   thrd[i]->join();
>  }

Should be:

for(i=0; i < num_thrds; i++)
  thrd[i] = new boost::thread(count(i+1));

for(i=0; i < num_thrds; i++)
  thrd[i]->join();

Calling join prior to creating the next thread would cause the
sequential execution you were seeing.

HTH,

Nate


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net