Boost logo

Boost Users :

Subject: Re: [Boost-users] Thread object pointers.
From: OvermindDL1 (overminddl1_at_[hidden])
Date: 2010-08-03 20:20:32


On Tue, Aug 3, 2010 at 3:10 PM, Albert Schueller <schuelaw_at_[hidden]> wrote:
> Tue Aug  3 14:10:04 PDT 2010
>
> Hi all,
>
> I'm trying to get up to speed on libboost's thread class using the
> tutorial information here:
>
> http://www.drdobbs.com/cpp/184401518
>
> I'm curious why the two programs below seem to behave differently.  In
> LISTING 1, three threads are declared as thrd1, thrd2, and thrd3 explicitly
> and each loops 10 times with random length sleeps.
>
> LISTING 2 is identical, except for the fact the three thread variables
> are allocated to boost::thread pointers.
>
> 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?
>
> Any help would be appreciated.
>
> Albert
>
> ///////////////////////////////////////////////////////////////////////////
> //LISTING 1
>
> #include <boost/thread/thread.hpp>
> #include <boost/thread/mutex.hpp>
> #include <iostream>
> #include <unistd.h>
> #include <cstdlib>
>
> boost::mutex io_mutex;
>
> struct count
> {
>  count(int id) : id(id) { }
>
>  void operator()()
>  {
>    int upause;
>    for (int i = 0; i < 10; ++i)
>    {
>      upause = rand()%1000000;
>      usleep(upause);
>      boost::mutex::scoped_lock
>        lock(io_mutex);
>      std::cout << id << ": "
>        << i << " paused " << upause << std::endl;
>    }
>  }
>
>  int id;
> };
>
> int main(int argc, char* argv[])
> {
>  boost::thread thrd1(count(1));
>  boost::thread thrd2(count(2));
>  boost::thread thrd3(count(3));
>  thrd1.join();
>  thrd2.join();
>  thrd3.join();
>  return 0;
> }
>
>
> ///////////////////////////////////////////////////////////////////////////
> //LISTING 2
>
> #include <boost/thread/thread.hpp>
> #include <boost/thread/mutex.hpp>
> #include <iostream>
> #include <unistd.h>
> #include <cstdlib>
>
> boost::mutex io_mutex;
>
> struct count
> {
>  count(int id) : id(id) { }
>
>  void operator()()
>  {
>    int upause;
>    for (int i = 0; i < 10; ++i)
>    {
>      upause = rand()%1000000;
>      usleep(upause);
>      boost::mutex::scoped_lock
>        lock(io_mutex);
>      std::cout << id << ": "
>        << i << "paused (" << upause << ")" << std::endl;
>    }
>  }
>
>  int id;
> };
>
> int main(int argc, char* argv[])
> {
>  int i;
>  int num_thrds = 3;
>  boost::thread *thrd[4];
>

I would imagine it is because of this part:
>  for(i=0;i<num_thrds;i++) {
>   thrd[i] = new boost::thread(count(i+1));
>   thrd[i]->join();
>  }

You are creating a thread, then immediately joining it, which means
that it needs to finish before the join() call completes, maybe do
this instead:
 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();
 }

>
>  // deallocate thread objects
>  for(i=0;i<num_thrds;i++) {
>    delete thrd[i];
>  }
>
>  return 0;
> }


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