|
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