The_Journey
December 30th, 2010, 05:39 PM
Hi, so I created a test program as such:
#include <boost/thread.hpp>
#include <iostream>
class Thread1
{
public:
void operator() ()
{
for (int i = 0; i < 100; ++i)
{
std::cout << "From Thread1: " << i << std::endl;
}
}
};
class Thread2
{
public:
void operator() ()
{
for (int i = 0; i < 100; ++i)
{
std::cout << "From Thread2: " << i << std::endl;
}
}
};
int main(int argc, char* argv[])
{
Thread1 hr1;
Thread1 hr2;
boost::thread* thr1 = new boost::thread(hr1);
boost::thread* thr2 = new boost::thread(hr2);
boost::thread_group thr_group;
thr_group.add_thread(thr1);
thr_group.add_thread(thr2);
thr_group.join_all();
}
The output I got was:
From Thread2: 0
From Thread2: ...
From Thread2: 99
From Thread1: 0
From Thread1: ...
From Thread1: 99
Can anyone explain to me why the output isn't random? From what I know, the output should be a mix between Thread1 and Thread2, but it isn't.
Am I doing something wrong?
Thank you.
#include <boost/thread.hpp>
#include <iostream>
class Thread1
{
public:
void operator() ()
{
for (int i = 0; i < 100; ++i)
{
std::cout << "From Thread1: " << i << std::endl;
}
}
};
class Thread2
{
public:
void operator() ()
{
for (int i = 0; i < 100; ++i)
{
std::cout << "From Thread2: " << i << std::endl;
}
}
};
int main(int argc, char* argv[])
{
Thread1 hr1;
Thread1 hr2;
boost::thread* thr1 = new boost::thread(hr1);
boost::thread* thr2 = new boost::thread(hr2);
boost::thread_group thr_group;
thr_group.add_thread(thr1);
thr_group.add_thread(thr2);
thr_group.join_all();
}
The output I got was:
From Thread2: 0
From Thread2: ...
From Thread2: 99
From Thread1: 0
From Thread1: ...
From Thread1: 99
Can anyone explain to me why the output isn't random? From what I know, the output should be a mix between Thread1 and Thread2, but it isn't.
Am I doing something wrong?
Thank you.