Thread object pointers.

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

Tue Aug 3 14:40:40 PDT 2010 It's amazing how you stare at code for an hour, finally give up, post a message to a mailing list, and 5 minutes later solve the problem. The difference between the two listings is in the join()'s. In the first the three thread objects are created and then the three join()'s executed. In the second, it's thrd[0], join(), thrd[1], join(), thrd[2], join(). I'm not entirely sure why this matters, but it did fix the problem. Hope somebody finds this little diversion useful... Albert
/////////////////////////////////////////////////////////////////////////// //LISTING 1
#include
#include #include <iostream> #include #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
#include #include <iostream> #include #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];
// Change this:
for(i=0;i
join(); }
// To this:
for(i=0;i
// deallocate thread objects for(i=0;i
return 0; }

On 8/3/2010 4:42 PM, Albert Schueller wrote:
Tue Aug 3 14:40:40 PDT 2010
It's amazing how you stare at code for an hour, finally give up, post a message to a mailing list, and 5 minutes later solve the problem.
The difference between the two listings is in the join()'s. In the first the three thread objects are created and then the three join()'s executed. In the second, it's thrd[0], join(), thrd[1], join(), thrd[2], join(). I'm not entirely sure why this matters, but it did fix the problem.
It matters because that's precisely what "join" is supposed to do...... halt the calling thread until the "joined" thread is done.

It matters because that's precisely what "join" is supposed to do...... halt the calling thread until the "joined" thread is done.
Why, in LISTING 1, doesn't thrd1.join(); thrd2.join(); thrd3.join(); do the same thing, i.e. hold until thrd1 is complete, then do thrd2, then thrd3? Albert

On Tue, 3 Aug 2010 17:22:37 -0700, Albert Schueller
It matters because that's precisely what "join" is supposed to do...... halt the calling thread until the "joined" thread is done.
Why, in LISTING 1, doesn't
thrd1.join(); thrd2.join(); thrd3.join();
do the same thing, i.e. hold until thrd1 is complete, then do thrd2, then thrd3?
Albert
It does, but AFTER starting all there threads. Listing 1 does a: Start1 Start2 Starr3 Join1 Join2 Join3 Listing 2 does Start1 Join1 Start2 Join2 Start3 Join3 Richard Damon

On 8/3/2010 4:10 PM, Albert Schueller wrote:
int main(int argc, char* argv[]) { int i; int num_thrds = 3; boost::thread *thrd[4];
for(i=0;i
join();
^^^^ that's your problem.
join won't return until the thread finishes
you need 3 loops.
first loop, call new, just like you were.
then
for i=0;i

Hi Albert,
On Tue, Aug 3, 2010 at 3:10 PM, Albert Schueller
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
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

On Tue, Aug 3, 2010 at 3:10 PM, Albert Schueller
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
#include #include <iostream> #include #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
#include #include <iostream> #include #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
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
// deallocate thread objects for(i=0;i
return 0; }
participants (5)
-
Albert Schueller
-
Eric J. Holtman
-
Nathan Crookston
-
OvermindDL1
-
Richard Damon