I'm using boost 1.53 on windows 7. I have a main loop which runs some checks and when events are received, results in callbacks that add tasks onto a queue. I then pull a task off the queue, wish to start a thread (and only one thread) to handle the task, and continue to loop checking for certain events. And each time I go through the loop, I want to check to see if the current thread has completed before pulling off another task to process. My problem is that I'm not sure how to write the thread in to do this and not join because I have to keep looping to check for certain events. void my_callback(...); MyTask *task; while (true) { event_checks(); switch(state) { case STATE1: if (!queue.empty()) task = queue.front(); // ... break; case STATE2: // check if thread is still running, otherwise, start new thread boost::thread(&MyTask::do_it, task); // ... break; // ... } I tried things similar to the below but couldn't get that quite right. boost::thread t; while (true) { // ... if (t.timed_join(0) == false) { t = boost::thread(&MyTask::do_it, task); } // ... Any help much appreciated.
I'm using boost 1.53 on windows 7. I have a main loop which runs some checks and when events are received, results in callbacks that add tasks onto a queue. I then pull a task off the queue, wish to start a thread (and only one thread) to handle the task, and continue to loop checking for certain events. And each time I go through the loop, I want to check to see if the current thread has completed before pulling off another task to process.
My problem is that I'm not sure how to write the thread in to do this and not join because I have to keep looping to check for certain events.
You can use timed_join, as in your example, but note that the thread might exit anytime. Instead of complicated management of tasks and threads, why would't you use asio::io_service? Just run it in 1 thread, and post your tasks to it. When io_service doesn't have any "active" work, its thread would be idle. //setup io_serivce before the loop asio::io_service io_service; // give it some "work", to prevent premature exit shared_ptrasio::io_service::work work(new asio::io_service::work(io_service)); boost::thread t(&asio::io_service::run, &io_service); t.detach(); //... // from within your loop, post tasks io_service.post(yourFunctor); // yourFunctor will be executed in the separate thread
In trying to set this up before the loop, I'm getting the following error.
boost::asio::io_service worker_service;
shared_ptrboost::asio::io_service::work
work(new boost::asio::io_service::work(worker_service));
boost::thread worker_thread(&boost::asio::io_service::run,
worker_service));
worker_thread.detach();
while (1) {
// ...
worker_service.post(task);
}
2>taskm.cpp(454): error C2059: syntax error : ')'
2>taskm.cpp(454): error C2664:
'boost::thread::threadboost::asio::io_service&(const
boost::thread::attributes &,F)' : cannot convert parameter 1 from
'overloaded-function' to 'const boost::thread::attributes &'
2> with
2> [
2> F=boost::asio::io_service &
2> ]
2> Reason: cannot convert from 'overloaded-function' to 'const
boost::thread::attributes'
2> No constructor could take the source type, or constructor
overload resolution was ambiguous
On Tue, Jul 2, 2013 at 1:09 AM, Igor R
I'm using boost 1.53 on windows 7. I have a main loop which runs some checks and when events are received, results in callbacks that add tasks onto a queue. I then pull a task off the queue, wish to start a thread (and only one thread) to handle the task, and continue to loop checking for certain events. And each time I go through the loop, I want to check to see if the current thread has completed before pulling off another task to process.
My problem is that I'm not sure how to write the thread in to do this and not join because I have to keep looping to check for certain events.
You can use timed_join, as in your example, but note that the thread might exit anytime. Instead of complicated management of tasks and threads, why would't you use asio::io_service? Just run it in 1 thread, and post your tasks to it. When io_service doesn't have any "active" work, its thread would be idle.
//setup io_serivce before the loop asio::io_service io_service; // give it some "work", to prevent premature exit shared_ptrasio::io_service::work work(new asio::io_service::work(io_service)); boost::thread t(&asio::io_service::run, &io_service); t.detach(); //... // from within your loop, post tasks io_service.post(yourFunctor); // yourFunctor will be executed in the separate thread _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Never mind, still learning boost.
On Tue, Jul 2, 2013 at 4:12 PM, SRD wrote: In trying to set this up before the loop, I'm getting the following error. boost::asio::io_service worker_service; shared_ptrboost::asio::io_service::work
work(new boost::asio::io_service::work(worker_service)); boost::thread worker_thread(&boost::asio::io_service::run,
worker_service));
worker_thread.detach(); while (1) {
// ...
worker_service.post(task); } 2>taskm.cpp(454): error C2059: syntax error : ')'
2>taskm.cpp(454): error C2664:
'boost::thread::threadboost::asio::io_service&(const
boost::thread::attributes &,F)' : cannot convert parameter 1 from
'overloaded-function' to 'const boost::thread::attributes &'
2> with
2> [
2> F=boost::asio::io_service &
2> ]
2> Reason: cannot convert from 'overloaded-function' to 'const
boost::thread::attributes'
2> No constructor could take the source type, or constructor
overload resolution was ambiguous On Tue, Jul 2, 2013 at 1:09 AM, Igor R I'm using boost 1.53 on windows 7. I have a main loop which runs some
checks
and when events are received, results in callbacks that add tasks onto a
queue. I then pull a task off the queue, wish to start a thread (and
only
one thread) to handle the task, and continue to loop checking for
certain
events. And each time I go through the loop, I want to check to see if
the
current thread has completed before pulling off another task to process. My problem is that I'm not sure how to write the thread in to do this
and
not join because I have to keep looping to check for certain events. You can use timed_join, as in your example, but note that the thread
might exit anytime.
Instead of complicated management of tasks and threads, why would't
you use asio::io_service? Just run it in 1 thread, and post your tasks
to it. When io_service doesn't have any "active" work, its thread
would be idle. //setup io_serivce before the loop
asio::io_service io_service;
// give it some "work", to prevent premature exit
shared_ptrasio::io_service::work work(new
asio::io_service::work(io_service));
boost::thread t(&asio::io_service::run, &io_service);
t.detach();
//...
// from within your loop, post tasks
io_service.post(yourFunctor); // yourFunctor will be executed in the
separate thread
_______________________________________________
Boost-users mailing list
Boost-users@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
Igor R
-
SRD