Boost logo

Boost :

From: v2cechura (v2cechura_at_[hidden])
Date: 2007-11-26 06:39:02


Using tg.join_all() did not help.

Note that main() in my sample waits for console input before it returns and
the exception is issued during thread startup made by boost own code (not my
code) called by tg.create_threas().

I also tried modifying the code to allocate and destroy mutex "console"
dynamically in main() which did not help either.

Vaclav

> -----Original Message-----
> From: boost-bounces_at_[hidden]
> [mailto:boost-bounces_at_[hidden]] On Behalf Of Anthony Williams
> Sent: Monday, November 26, 2007 9:00 AM
> To: boost_at_[hidden]
> Subject: Re: [boost]FW: [Boost-users] [threads] Using
> boost::mutex raises access violation exception
>
> v2cechura <v2cechura <at> atlas.cz> writes:
> > I just recently installed and built boost library (1.34.1) using
> > bcbboost. I wanted to try the boost::thread class. This simple code
> > gives me "Access Violation" in Borland C++ Builder 2006 when
> > boost::mutex is
> > used:
> >
> >
> //--------------------------------------------------------------------
> > ------
> > -
> > #include <iostream>
> > #include <boost/thread.hpp>
> >
> //--------------------------------------------------------------------
> > ------
> > -
> > boost::xtime xt = {2};
>
> Phil already pointed out the problem with this timeout.
>
> > boost::mutex console; // line A
> >
> > void thr() // int n, boost::mutex & display {
> > for (int i = 0; i < 10; i++)
> > {
> > {
> > boost::mutex::scoped_lock l(console); // line B
> > std::cout << "thr" << 1 << ":" << i <<
> std::endl;
> > }
> > boost::thread::sleep(xt);
> > } // end for
> > }
> >
> > int main(int argc, char* argv[])
> > {
> > boost::thread_group tg;
> > tg.create_thread(&thr);
> >
> > char c;
> > std::cin >> c ;
> > return 0;
> > }
>
> This thread will be detached if it has not finished when the
> closing brace of
> main() is reached. In that case, it will be running
> concurrently with the destructors of global objects,
> including the mutex "console", and the mutexes internal to
> the thread library. If you access an object after (or during)
> its destructor run, then you've got undefined behaviour.
>
> Try adding "tg.join_all()" before the return.
>
> > If line A and line B are commented-out then the error is gone.
>
> That makes sense --- you're not accessing global state other
> than the iostreams, so you're OK until the process exits.
>
> Anthony
>
> _______________________________________________
> Unsubscribe & other changes:
> http://lists.boost.org/mailman/listinfo.cgi/boost
>
>
>
>
>


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk