[boost] [thread] thread handling question
Hi, this may be a very dumb question… My boost version is 1.56.0. In my program I use a database logging technique. To avoid blocking the main thread, I coded this: int main() { while(bDoCalculations == true) { // calculate some stuff m_Logger->doLogging(); } } void CLoggingData::doLogging() { std::vector<float>* Result; // fill Result boost::thread (&CLoggingData::doStoreData, this, Result); } void CLoggingData::doStoreData(std::vector<float>* dataVector) { // pass pointer to database writer instance delete dataVector; } I do not use join() in the doLogging method because I don’t want the main thread to block if the database is busy and will not respond immediately. But then, after a long run I get a boost::thread_resource_error exception. You may say: Of course, you never join your thread and after a few thousands new threads you blow your stack. Well, but what can I do to get my desired behavior? In the documentation I found this which I think of might help: You can use a thread_joiner to ensure that the thread has been joined at the thread destructor. int main() { boost::thread t(my_func); boost::thread_joiner g(t); // do someting else } // here the thread_joiner destructor will join the thread before it is destroyed. But where is the thread_joiner class implemented? Or do I have to add something inside my doStoreData method to end the thread properly? Thanks in advance, Markus Gesendet mit AquaMail für Android http://www.aqua-mail.com
On 06/02/2016 10:07 AM, Markus Pieper wrote:
Hi, this may be a very dumb question… My boost version is 1.56.0.
In my program I use a database logging technique. To avoid blocking the main thread, I coded this:
int main() { while(bDoCalculations == true) { // calculate some stuff m_Logger->doLogging(); } }
void CLoggingData::doLogging() { std::vector<float>* Result; // fill Result boost::thread (&CLoggingData::doStoreData, this, Result); }
void CLoggingData::doStoreData(std::vector<float>* dataVector) { // pass pointer to database writer instance delete dataVector; }
I do not use join() in the doLogging method because I don’t want the main thread to block if the database is busy and will not respond immediately.
But then, after a long run I get a boost::thread_resource_error exception. You may say: Of course, you never join your thread and after a few thousands new threads you blow your stack.
Well, but what can I do to get my desired behavior? In the documentation I found this which I think of might help:
You can use a thread_joiner to ensure that the thread has been joined at the thread destructor.
int main() { boost::thread t(my_func); boost::thread_joiner g(t); // do someting else } // here the thread_joiner destructor will join the thread before it is destroyed.
But where is the thread_joiner class implemented? Or do I have to add something inside my doStoreData method to end the thread properly?
Thanks in advance, Markus
Gesendet mit AquaMail für Android http://www.aqua-mail.com
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Hey there, I had a similar problem. My main thread issues "tasks" which are to run asynchonously. The main thread may request many tasks to be executed. Once a task is finished, the result is reported back to the main thread, the proper task thread is joined and the next task will be started in a new thread. Initially I forgot to join the task threads which sooner or later (depending on how much was going on) lead to the exact same problem that you described (the OS keeps some info around when a thread ends; joining the thread will release the info). Here's basically what I did: 1. Keep a list of all ongoing/pending tasks 2. When a task is done, let it report back to main thread 3. When main thread receives such a "finished" message, it looks through the list to find out what task is done and joins the proper thread instance. In my opinion you need to do something like the above anyway, because when the app is to shut down while tasks are still running you need to wait for them to "finish" anyway. Best regards, Andreas
Nice idea!
I implemented such mechanism and my program runs now overnight, so I can
see tomorrow if it worked...
Gesendet mit AquaMail für Android
http://www.aqua-mail.com
Am 2. Juni 2016 10:30:28 vorm. schrieb Andreas Wehrmann
On 06/02/2016 10:07 AM, Markus Pieper wrote:
Hi, this may be a very dumb question… My boost version is 1.56.0.
In my program I use a database logging technique. To avoid blocking the main thread, I coded this:
int main() { while(bDoCalculations == true) { // calculate some stuff m_Logger->doLogging(); } }
void CLoggingData::doLogging() { std::vector<float>* Result; // fill Result boost::thread (&CLoggingData::doStoreData, this, Result); }
void CLoggingData::doStoreData(std::vector<float>* dataVector) { // pass pointer to database writer instance delete dataVector; }
I do not use join() in the doLogging method because I don’t want the main thread to block if the database is busy and will not respond immediately.
But then, after a long run I get a boost::thread_resource_error exception. You may say: Of course, you never join your thread and after a few thousands new threads you blow your stack.
Well, but what can I do to get my desired behavior? In the documentation I found this which I think of might help:
You can use a thread_joiner to ensure that the thread has been joined at the thread destructor.
int main() { boost::thread t(my_func); boost::thread_joiner g(t); // do someting else } // here the thread_joiner destructor will join the thread before it is destroyed.
But where is the thread_joiner class implemented? Or do I have to add something inside my doStoreData method to end the thread properly?
Thanks in advance, Markus
Gesendet mit AquaMail für Android http://www.aqua-mail.com
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Hey there,
I had a similar problem. My main thread issues "tasks" which are to run asynchonously. The main thread may request many tasks to be executed. Once a task is finished, the result is reported back to the main thread, the proper task thread is joined and the next task will be started in a new thread.
Initially I forgot to join the task threads which sooner or later (depending on how much was going on) lead to the exact same problem that you described (the OS keeps some info around when a thread ends; joining the thread will release the info).
Here's basically what I did:
1. Keep a list of all ongoing/pending tasks 2. When a task is done, let it report back to main thread 3. When main thread receives such a "finished" message, it looks through the list to find out what task is done and joins the proper thread instance.
In my opinion you need to do something like the above anyway, because when the app is to shut down while tasks are still running you need to wait for them to "finish" anyway.
Best regards, Andreas
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Thanks, it worked!
Gesendet mit AquaMail für Android
http://www.aqua-mail.com
Am 2. Juni 2016 10:30:28 vorm. schrieb Andreas Wehrmann
On 06/02/2016 10:07 AM, Markus Pieper wrote:
Hi, this may be a very dumb question… My boost version is 1.56.0.
In my program I use a database logging technique. To avoid blocking the main thread, I coded this:
int main() { while(bDoCalculations == true) { // calculate some stuff m_Logger->doLogging(); } }
void CLoggingData::doLogging() { std::vector<float>* Result; // fill Result boost::thread (&CLoggingData::doStoreData, this, Result); }
void CLoggingData::doStoreData(std::vector<float>* dataVector) { // pass pointer to database writer instance delete dataVector; }
I do not use join() in the doLogging method because I don’t want the main thread to block if the database is busy and will not respond immediately.
But then, after a long run I get a boost::thread_resource_error exception. You may say: Of course, you never join your thread and after a few thousands new threads you blow your stack.
Well, but what can I do to get my desired behavior? In the documentation I found this which I think of might help:
You can use a thread_joiner to ensure that the thread has been joined at the thread destructor.
int main() { boost::thread t(my_func); boost::thread_joiner g(t); // do someting else } // here the thread_joiner destructor will join the thread before it is destroyed.
But where is the thread_joiner class implemented? Or do I have to add something inside my doStoreData method to end the thread properly?
Thanks in advance, Markus
Gesendet mit AquaMail für Android http://www.aqua-mail.com
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Hey there,
I had a similar problem. My main thread issues "tasks" which are to run asynchonously. The main thread may request many tasks to be executed. Once a task is finished, the result is reported back to the main thread, the proper task thread is joined and the next task will be started in a new thread.
Initially I forgot to join the task threads which sooner or later (depending on how much was going on) lead to the exact same problem that you described (the OS keeps some info around when a thread ends; joining the thread will release the info).
Here's basically what I did:
1. Keep a list of all ongoing/pending tasks 2. When a task is done, let it report back to main thread 3. When main thread receives such a "finished" message, it looks through the list to find out what task is done and joins the proper thread instance.
In my opinion you need to do something like the above anyway, because when the app is to shut down while tasks are still running you need to wait for them to "finish" anyway.
Best regards, Andreas
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Le 02/06/2016 à 10:07, Markus Pieper a écrit :
Hi, this may be a very dumb question… My boost version is 1.56.0.
In my program I use a database logging technique. To avoid blocking the main thread, I coded this:
int main() { while(bDoCalculations == true) { // calculate some stuff m_Logger->doLogging(); } }
void CLoggingData::doLogging() { std::vector<float>* Result; // fill Result boost::thread (&CLoggingData::doStoreData, this, Result); }
void CLoggingData::doStoreData(std::vector<float>* dataVector) { // pass pointer to database writer instance delete dataVector; }
I do not use join() in the doLogging method because I don’t want the main thread to block if the database is busy and will not respond immediately.
But then, after a long run I get a boost::thread_resource_error exception. You may say: Of course, you never join your thread and after a few thousands new threads you blow your stack.
Well, but what can I do to get my desired behavior? In the documentation I found this which I think of might help:
You can use a thread_joiner to ensure that the thread has been joined at the thread destructor.
int main() { boost::thread t(my_func); boost::thread_joiner g(t); // do someting else } // here the thread_joiner destructor will join the thread before it is destroyed.
But where is the thread_joiner class implemented? Or do I have to add something inside my doStoreData method to end the thread properly? Sorry, the documentation is erroneous. It should be boost::scoped_thread<>.
http://www.boost.org/doc/libs/1_56_0/doc/html/thread/ScopedThreads.html HTH, Vicente
I implemented the thread pool because we can do some other stuff with the
threads.
But we use the scoped threads in another area now.
Thanks for the advice!
Markus
Gesendet mit AquaMail für Android
http://www.aqua-mail.com
Am 3. Juni 2016 12:47:24 vorm. schrieb "Vicente J. Botet Escriba"
Le 02/06/2016 à 10:07, Markus Pieper a écrit :
Hi, this may be a very dumb question… My boost version is 1.56.0.
In my program I use a database logging technique. To avoid blocking the main thread, I coded this:
int main() { while(bDoCalculations == true) { // calculate some stuff m_Logger->doLogging(); } }
void CLoggingData::doLogging() { std::vector<float>* Result; // fill Result boost::thread (&CLoggingData::doStoreData, this, Result); }
void CLoggingData::doStoreData(std::vector<float>* dataVector) { // pass pointer to database writer instance delete dataVector; }
I do not use join() in the doLogging method because I don’t want the main thread to block if the database is busy and will not respond immediately.
But then, after a long run I get a boost::thread_resource_error exception. You may say: Of course, you never join your thread and after a few thousands new threads you blow your stack.
Well, but what can I do to get my desired behavior? In the documentation I found this which I think of might help:
You can use a thread_joiner to ensure that the thread has been joined at the thread destructor.
int main() { boost::thread t(my_func); boost::thread_joiner g(t); // do someting else } // here the thread_joiner destructor will join the thread before it is destroyed.
But where is the thread_joiner class implemented? Or do I have to add something inside my doStoreData method to end the thread properly? Sorry, the documentation is erroneous. It should be boost::scoped_thread<>.
http://www.boost.org/doc/libs/1_56_0/doc/html/thread/ScopedThreads.html
HTH, Vicente
---------- _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
On 2/06/2016 20:07, Markus Pieper wrote:
Hi, this may be a very dumb question… My boost version is 1.56.0.
In my program I use a database logging technique. To avoid blocking the main thread, I coded this: [...] But then, after a long run I get a boost::thread_resource_error exception. You may say: Of course, you never join your thread and after a few thousands new threads you blow your stack.
For this sort of application, you might want to consider using a threadpool. You can either use one provided by your target OS API, or use Boost.Asio. You can ignore most of the I/O stuff and just use an io_service as a threadpool manager by giving it an io_service::work so it doesn't stop until you're done, feed it as many threads as you want to concurrently run work, and then just post jobs to it whenever something comes up.
participants (4)
-
Andreas Wehrmann
-
Gavin Lambert
-
Markus Pieper
-
Vicente J. Botet Escriba