c++ - Pass by reference vs. Pass by pointer: Boost threads and deadline timer -


i have simple program outputs increasing integers in span of 1 second using boost libraries:

#include <iostream> #include <boost/thread/thread.hpp> #include <boost/asio.hpp> using namespace std;   void func1(bool* done) {     float i=0;     while (!(*done))     {         cout << << " ";         i++;     }     return; }  void timer(bool* done, boost::thread* thread) {     boost::asio::io_service io;     boost::asio::deadline_timer timer(io, boost::posix_time::seconds(1));     timer.wait();     *done = true;     return; }  int main() {     bool done = false;      boost::thread thread1(func1, &done);     boost::thread thread2(timer, &done, &thread1);     thread2.join();     thread1.join(); } 

that iteration of code works, had had bool defined in main function passed reference functions func1 , thread. i.e.:

void func1(bool& done) /*...*/ while (!(done)) /* ... */ void timer(bool& done, boost::thread* thread) /*...*/ done = true; 

with thread definitions:

    boost::thread thread1(func1, done);     boost::thread thread2(timer, done, &thread1); 

when execute that, loop within func1() never terminates! i've added breakpoint @ return of timer(), , ide (ms vc++ express 2010) indicates bool done indeed has value of true, within func1().

any insight why happening?

to pass argument reference, use boost:ref:

boost::thread thread1(func1, boost::ref(done));


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -