I would put to use RAII management of my thread: (this is covered in Multithreaded C++: Part 3: RAII And Threads)
I would update the destructor of this class to automatically shutdown the thread when it is called:
~threaded_class() { //Warning this stop call will fail if stop had been called previously, //see Multithreaded C++: Part 3: RAII And Threads for a more robust //example stop(); }
Then I would place the object at the appropriate scope so that its lifetime was what I needed it to be.
int main() { threaded_class fibcalc; //Do stuff doSomethingWithFibCalc(fibcalc); //When main() exits fibcalc is popped from the stack, its destructor is called and the //Thread is automatically stopped and joined. }
I feel compelled to point out that this example is mostly academic. It's a fun way to illustrate the use of boost threads and mutexes, but it's a very inefficient way of calculating the Fibonacci sequence. A method utilising a cache or even calculating the sequence statically at compile time is a much better option.
Recent comments
10 sec ago
2 hours 55 min ago
2 hours 55 min ago
13 hours 9 min ago
14 hours 37 min ago
14 hours 37 min ago
14 hours 37 min ago
21 hours 54 min ago
2 weeks 3 days ago
4 weeks 4 days ago