Boost logo

Boost Users :

Subject: Re: [Boost-users] [Thread] io_service post is unsuccessful
From: Dylan Klomparens (dylan.klomparens_at_[hidden])
Date: 2009-10-28 16:49:16


Rutger,

I didn't know it would exit when it ran out of work. Your solution worked
perfectly! I added that line and Service.run() no longer exits prematurely.
Thanks for your insight!

-- Dylan

For anyone who might try to do something similar in the future, here is the
complete code listing that works properly.

#include <iostream>
#include <boost/asio.hpp>
#include <boost/thread/thread.hpp>
using namespace std;

boost::asio::io_service Service;

void OnSignal()
{
    cout << "Got timing signal" << endl;
}

void TimingSignalThread()
{
    boost::this_thread::disable_interruption di;
    while(!boost::this_thread::interruption_requested())
    {
        boost::this_thread::sleep(boost::posix_time::millisec(500));
        Service.post(&OnSignal);
    }
}

void ServiceRunner()
{
    *boost::asio::io_service::work RunForever(Service);*
    Service.run();
}

int main()
{
    boost::thread TimingThread(&TimingSignalThread);
    boost::thread ServiceThread(&ServiceRunner);
    cin.get();
    TimingThread.interrupt();
    TimingThread.join();
    Service.stop();
    ServiceThread.join();
    cout << "Joined all threads." << endl;
    return 0;
}

On Wed, Oct 28, 2009 at 4:42 PM, Rutger ter Borg <rutger_at_[hidden]> wrote:

> Dylan Klomparens wrote:
>
> > My question is, why does my call to Service.post always fail? I know it
> is
> > failing because the function that I post never gets called. There is not
> > explicit return code or thrown exception to indicate that there is a
> > problem.
> >
>
> An io_service keeps running as long as it has work to do. You can force an
> io_service to keep running by attaching a work object. Try adding
>
> boost::asio::io_service::work work(Service);
>
> just below main, before the call to Service.run().
>
> Cheers,
>
> Rutger
>
>
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net