Boost logo

Boost Users :

Subject: Re: [Boost-users] [boost] [thread] thread handling question
From: Andreas Wehrmann (a.wehrmann_at_[hidden])
Date: 2016-06-02 04:29:48


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_at_[hidden]
> 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 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