Here is a small and simple example. He tried and seems to be working fine.
#include <iostream>
#include <boost/thread.hpp>
namespace this_thread = boost::this_thread;
int a = 0;
int b = 0;
int c = 0;
class BaseThread
{
public:
BaseThread()
{ }
virtual ~BaseThread()
{ }
void operator()()
{
try
{
for (;;)
{
this_thread::interruption_point();
DoStuff();
}
}
catch (boost::thread_interrupted)
{
}
}
protected:
virtual void DoStuff() = 0;
};
class ThreadA : public BaseThread
{
protected:
virtual void DoStuff()
{
a += 1000;
this_thread::sleep(boost::posix_time::milliseconds(500));
}
};
class ThreadB : public BaseThread
{
protected:
virtual void DoStuff()
{
b++;
this_thread::sleep(boost::posix_time::milliseconds(100));
}
};
int main()
{
ThreadA thread_a_instance;
ThreadB thread_b_instance;
boost::thread threadA = boost::thread(thread_a_instance);
boost::thread threadB = boost::thread(thread_b_instance);
for (int i = 0; i < 40; i++)
{
c = a + b;
std::cout << c << std::endl;
this_thread::sleep(boost::posix_time::milliseconds(250));
}
threadB.interrupt();
threadB.join();
threadA.interrupt();
threadA.join();
}
source
share