Boost logo

Boost Users :

Subject: Re: [Boost-users] [boost] [thread] thread handling question
From: Vicente J. Botet Escriba (vicente.botet_at_[hidden])
Date: 2016-06-02 18:46:43


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 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