FW: [Boost-users] [threads] Using boost::mutex raises access violation exception

I posted this to the user's list (no answer till now), but later came to the conclusion that developer's list might be a better place. Any help on th issue appreciated. Vaclav -----Original Post------ 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}; 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; } If line A and line B are commented-out then the error is gone. The project is a multithreaded console application, both boost.threads and rtl are static-linked. Caller stack is: :7c812a5b kernel32.RaiseException + 0x52 :004129aa ___raiseDebuggerException + 0x1A :00412a84 ; ___raiseDebuggerException :7c9037bf ntdll.RtlConvertUlongToLargeInteger + 0x7a :7c90378b ntdll.RtlConvertUlongToLargeInteger + 0x46 :7c90eafa ntdll.KiUserExceptionDispatcher + 0xe :0041FA2B boost::mutex::do_unlock(this=NULL, =:0012FDE8) :0041E3A2 boost::detail::thread::lock_ops<boost::mutex>::unlock(m=NULL, state=:0012FDE8) :0041E12D boost::condition::do_wait<boost::mutex>(this=:0012FEA4, mutex=NULL) :0041DE2E boost::condition::wait<boost::detail::thread::scoped_lock<boost::mutex>
(this=:0012FEA4, lock=:0012FE5C) :0041D42F __UNNS__thread_01c827c2c2c9169a::thread_param::wait(this=:0012FE9C) :0041D201 boost::thread::thread(this=:00000FCC, threadfunc=:00000FC8) :00000001 :0012fe01
I put mutex.cpp in my directory and BCB2006 locates the error to be here: void mutex::do_unlock() { if (m_critical_section) //------------- Access violation HERE release_critical_section(m_mutex); else release_mutex(m_mutex); } The access violation is raised when thread::thread constructor is called. Looking at the caller stack I noticed that mutex parameter is NULL from some point and it results in this==NULL for the last call. Note that this mutex parameter is not the mutex "console" object I use in the program. It is a mutex that boost.threads library uses during thread initialization. On the other hand there is some dependance on mutex "console" because when I comment it out the error is gone. I maked console a pointer with appropriate changes to source and created it using new but it did not help. Am I missing something? Any help appreciated. Does anybody have experiences with boost::thread and mutex under BCB(2006,2007...)? Vaclav

v2cechura wrote:
boost::xtime xt = {2}; boost::thread::sleep(xt);
I presume that you want this to sleep for 2 seconds. I doubt that this is the reason why your program crashes, but note that xtime should be an absolute time, not relative (at least, in the version that I'm using). You need something like: struct boost::xtime xt; xtime_get(&xt, boost::TIME_UTC); xt.sec += 2; boost::thread::sleep(xt); Phil.

Thank you for the note. It's goot to know. But as you wrote this doesn't seem to be the cause of the crash, because when I stop using the mutex then the crash is gone. Vaclav -----Original Message----- From: boost-bounces@lists.boost.org [mailto:boost-bounces@lists.boost.org] On Behalf Of Phil Endecott Sent: Friday, November 23, 2007 5:09 PM To: boost@lists.boost.org Subject: Re: [boost] [threads] Using boost::mutex raises access violationexception v2cechura wrote:
boost::xtime xt = {2}; boost::thread::sleep(xt);
I presume that you want this to sleep for 2 seconds. I doubt that this is the reason why your program crashes, but note that xtime should be an absolute time, not relative (at least, in the version that I'm using). You need something like: struct boost::xtime xt; xtime_get(&xt, boost::TIME_UTC); xt.sec += 2; boost::thread::sleep(xt); Phil. _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

I have experienced a similar issue with boost::mutex when the mutex was instantiated from DllMain (in a DLL). The Windows implementation uses a Critical Section. I have found that under many startup / shutdown cases the InitializeCriticalSection returns null. I cannot find documentation that states that this happens. The solution is to create the boost::mutex in another manner. Hence if you stop the boost::mutex from being a global variable I think the problem will disappear. I hope this helps, Neil Groves -----Original Message----- From: boost-bounces@lists.boost.org [mailto:boost-bounces@lists.boost.org] On Behalf Of v2cechura Sent: 23 November 2007 14:31 To: boost@lists.boost.org Subject: [boost] FW: [Boost-users] [threads] Using boost::mutex raises access violation exception I posted this to the user's list (no answer till now), but later came to the conclusion that developer's list might be a better place. Any help on th issue appreciated. Vaclav -----Original Post------ 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}; 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; } If line A and line B are commented-out then the error is gone. The project is a multithreaded console application, both boost.threads and rtl are static-linked. Caller stack is: :7c812a5b kernel32.RaiseException + 0x52 :004129aa ___raiseDebuggerException + 0x1A :00412a84 ; ___raiseDebuggerException :7c9037bf ntdll.RtlConvertUlongToLargeInteger + 0x7a :7c90378b ntdll.RtlConvertUlongToLargeInteger + 0x46 :7c90eafa ntdll.KiUserExceptionDispatcher + 0xe :0041FA2B boost::mutex::do_unlock(this=NULL, =:0012FDE8) :0041E3A2 boost::detail::thread::lock_ops<boost::mutex>::unlock(m=NULL, state=:0012FDE8) :0041E12D boost::condition::do_wait<boost::mutex>(this=:0012FEA4, mutex=NULL) :0041DE2E boost::condition::wait<boost::detail::thread::scoped_lock<boost::mutex>
(this=:0012FEA4, lock=:0012FE5C) :0041D42F __UNNS__thread_01c827c2c2c9169a::thread_param::wait(this=:0012FE9C) :0041D201 boost::thread::thread(this=:00000FCC, threadfunc=:00000FC8) :00000001 :0012fe01
I put mutex.cpp in my directory and BCB2006 locates the error to be here: void mutex::do_unlock() { if (m_critical_section) //------------- Access violation HERE release_critical_section(m_mutex); else release_mutex(m_mutex); } The access violation is raised when thread::thread constructor is called. Looking at the caller stack I noticed that mutex parameter is NULL from some point and it results in this==NULL for the last call. Note that this mutex parameter is not the mutex "console" object I use in the program. It is a mutex that boost.threads library uses during thread initialization. On the other hand there is some dependance on mutex "console" because when I comment it out the error is gone. I maked console a pointer with appropriate changes to source and created it using new but it did not help. Am I missing something? Any help appreciated. Does anybody have experiences with boost::thread and mutex under BCB(2006,2007...)? Vaclav _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

As I wrote in my post, I tried to allocate the mutex in main using "new" but it did not helped. And the cause seems to be in the thread initialization process long before my mutex is used. Actually the mutex that causes the crash is an mutex object that boost::thread creates during startup. Vaclav -----Original Message----- From: boost-bounces@lists.boost.org [mailto:boost-bounces@lists.boost.org] On Behalf Of Neil Groves Sent: Saturday, November 24, 2007 11:41 AM To: boost@lists.boost.org Subject: Re: [boost] FW: [Boost-users] [threads] Using boost::mutexraises access violation exception I have experienced a similar issue with boost::mutex when the mutex was instantiated from DllMain (in a DLL). The Windows implementation uses a Critical Section. I have found that under many startup / shutdown cases the InitializeCriticalSection returns null. I cannot find documentation that states that this happens. The solution is to create the boost::mutex in another manner. Hence if you stop the boost::mutex from being a global variable I think the problem will disappear. I hope this helps, Neil Groves -----Original Message----- From: boost-bounces@lists.boost.org [mailto:boost-bounces@lists.boost.org] On Behalf Of v2cechura Sent: 23 November 2007 14:31 To: boost@lists.boost.org Subject: [boost] FW: [Boost-users] [threads] Using boost::mutex raises access violation exception I posted this to the user's list (no answer till now), but later came to the conclusion that developer's list might be a better place. Any help on th issue appreciated. Vaclav -----Original Post------ 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}; 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; } If line A and line B are commented-out then the error is gone. The project is a multithreaded console application, both boost.threads and rtl are static-linked. Caller stack is: :7c812a5b kernel32.RaiseException + 0x52 :004129aa ___raiseDebuggerException + 0x1A :00412a84 ; ___raiseDebuggerException :7c9037bf ntdll.RtlConvertUlongToLargeInteger + 0x7a :7c90378b ntdll.RtlConvertUlongToLargeInteger + 0x46 :7c90eafa ntdll.KiUserExceptionDispatcher + 0xe :0041FA2B boost::mutex::do_unlock(this=NULL, =:0012FDE8) :0041E3A2 boost::detail::thread::lock_ops<boost::mutex>::unlock(m=NULL, state=:0012FDE8) :0041E12D boost::condition::do_wait<boost::mutex>(this=:0012FEA4, mutex=NULL) :0041DE2E boost::condition::wait<boost::detail::thread::scoped_lock<boost::mutex>
(this=:0012FEA4, lock=:0012FE5C) :0041D42F __UNNS__thread_01c827c2c2c9169a::thread_param::wait(this=:0012FE9C) :0041D201 boost::thread::thread(this=:00000FCC, threadfunc=:00000FC8) :00000001 :0012fe01
I put mutex.cpp in my directory and BCB2006 locates the error to be here: void mutex::do_unlock() { if (m_critical_section) //------------- Access violation HERE release_critical_section(m_mutex); else release_mutex(m_mutex); } The access violation is raised when thread::thread constructor is called. Looking at the caller stack I noticed that mutex parameter is NULL from some point and it results in this==NULL for the last call. Note that this mutex parameter is not the mutex "console" object I use in the program. It is a mutex that boost.threads library uses during thread initialization. On the other hand there is some dependance on mutex "console" because when I comment it out the error is gone. I maked console a pointer with appropriate changes to source and created it using new but it did not help. Am I missing something? Any help appreciated. Does anybody have experiences with boost::thread and mutex under BCB(2006,2007...)? Vaclav _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

Vaclav, It was not clear in your original post that the mutex was created from main. Since you have identified that it is an internal mutex in boost::thread that triggers the issue I suggest altering either the construction time of the mutex in boost::thread or replacing the boost::mutex with a replacement mutex based upon an atomic integer. This would diagnose the problem definitively. I would do this myself, but I have never experienced the problem since Borland C++ is not one of my compilers that I use. I hope this helps pinpoint the problem. Regards, Neil Groves -----Original Message----- From: boost-bounces@lists.boost.org [mailto:boost-bounces@lists.boost.org] On Behalf Of Vaclav Cechura Sent: 24 November 2007 11:47 To: boost@lists.boost.org Subject: Re: [boost] FW: [Boost-users] [threads] Using boost::mutexraises access violation exception As I wrote in my post, I tried to allocate the mutex in main using "new" but it did not helped. And the cause seems to be in the thread initialization process long before my mutex is used. Actually the mutex that causes the crash is an mutex object that boost::thread creates during startup. Vaclav -----Original Message----- From: boost-bounces@lists.boost.org [mailto:boost-bounces@lists.boost.org] On Behalf Of Neil Groves Sent: Saturday, November 24, 2007 11:41 AM To: boost@lists.boost.org Subject: Re: [boost] FW: [Boost-users] [threads] Using boost::mutexraises access violation exception I have experienced a similar issue with boost::mutex when the mutex was instantiated from DllMain (in a DLL). The Windows implementation uses a Critical Section. I have found that under many startup / shutdown cases the InitializeCriticalSection returns null. I cannot find documentation that states that this happens. The solution is to create the boost::mutex in another manner. Hence if you stop the boost::mutex from being a global variable I think the problem will disappear. I hope this helps, Neil Groves -----Original Message----- From: boost-bounces@lists.boost.org [mailto:boost-bounces@lists.boost.org] On Behalf Of v2cechura Sent: 23 November 2007 14:31 To: boost@lists.boost.org Subject: [boost] FW: [Boost-users] [threads] Using boost::mutex raises access violation exception I posted this to the user's list (no answer till now), but later came to the conclusion that developer's list might be a better place. Any help on th issue appreciated. Vaclav -----Original Post------ 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}; 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; } If line A and line B are commented-out then the error is gone. The project is a multithreaded console application, both boost.threads and rtl are static-linked. Caller stack is: :7c812a5b kernel32.RaiseException + 0x52 :004129aa ___raiseDebuggerException + 0x1A :00412a84 ; ___raiseDebuggerException :7c9037bf ntdll.RtlConvertUlongToLargeInteger + 0x7a :7c90378b ntdll.RtlConvertUlongToLargeInteger + 0x46 :7c90eafa ntdll.KiUserExceptionDispatcher + 0xe :0041FA2B boost::mutex::do_unlock(this=NULL, =:0012FDE8) :0041E3A2 boost::detail::thread::lock_ops<boost::mutex>::unlock(m=NULL, state=:0012FDE8) :0041E12D boost::condition::do_wait<boost::mutex>(this=:0012FEA4, mutex=NULL) :0041DE2E boost::condition::wait<boost::detail::thread::scoped_lock<boost::mutex>
(this=:0012FEA4, lock=:0012FE5C) :0041D42F __UNNS__thread_01c827c2c2c9169a::thread_param::wait(this=:0012FE9C) :0041D201 boost::thread::thread(this=:00000FCC, threadfunc=:00000FC8) :00000001 :0012fe01
I put mutex.cpp in my directory and BCB2006 locates the error to be here: void mutex::do_unlock() { if (m_critical_section) //------------- Access violation HERE release_critical_section(m_mutex); else release_mutex(m_mutex); } The access violation is raised when thread::thread constructor is called. Looking at the caller stack I noticed that mutex parameter is NULL from some point and it results in this==NULL for the last call. Note that this mutex parameter is not the mutex "console" object I use in the program. It is a mutex that boost.threads library uses during thread initialization. On the other hand there is some dependance on mutex "console" because when I comment it out the error is gone. I maked console a pointer with appropriate changes to source and created it using new but it did not help. Am I missing something? Any help appreciated. Does anybody have experiences with boost::thread and mutex under BCB(2006,2007...)? Vaclav _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

I am not sure if I got your suggestions right. Should I try to alter the code of boost's own class thread_param which seems to be the place where the exception occurs? I traced the code in the debbuger and this is the place when the exception occurs: My thread is going to be created in thread_group::create_thread. The constructor thread::thread() calls thread_param::wait ("param.wait()"). The "wait" function uses a scoped_lock for internal synchronisation (see the code): void wait() { boost::mutex::scoped_lock scoped_lock(m_mutex); while (!m_started) m_condition.wait(scoped_lock); } And It looks like the exception occurs in the call to scoped_lock::unlock() when scoped_lock object in thread_param::wait is being destroyed: template <typename Mutex> class scoped_lock : private noncopyable { public: typedef Mutex mutex_type; explicit scoped_lock(Mutex& mx, bool initially_locked=true) : m_mutex(mx), m_locked(false) { if (initially_locked) lock(); } ~scoped_lock() { if (m_locked) unlock(); } The problem seems to be related to the use of mutex. When I stop using mutexes the problem disappears. Vaclav -----Original Message----- From: boost-bounces@lists.boost.org [mailto:boost-bounces@lists.boost.org] On Behalf Of Neil Groves Sent: Sunday, November 25, 2007 12:05 PM To: boost@lists.boost.org Subject: Re: [boost] FW: [Boost-users] [threads]Using boost::mutexraises access violation exception Vaclav, It was not clear in your original post that the mutex was created from main. Since you have identified that it is an internal mutex in boost::thread that triggers the issue I suggest altering either the construction time of the mutex in boost::thread or replacing the boost::mutex with a replacement mutex based upon an atomic integer. This would diagnose the problem definitively. I would do this myself, but I have never experienced the problem since Borland C++ is not one of my compilers that I use. I hope this helps pinpoint the problem. Regards, Neil Groves -----Original Message----- From: boost-bounces@lists.boost.org [mailto:boost-bounces@lists.boost.org] On Behalf Of Vaclav Cechura Sent: 24 November 2007 11:47 To: boost@lists.boost.org Subject: Re: [boost] FW: [Boost-users] [threads] Using boost::mutexraises access violation exception As I wrote in my post, I tried to allocate the mutex in main using "new" but it did not helped. And the cause seems to be in the thread initialization process long before my mutex is used. Actually the mutex that causes the crash is an mutex object that boost::thread creates during startup. Vaclav -----Original Message----- From: boost-bounces@lists.boost.org [mailto:boost-bounces@lists.boost.org] On Behalf Of Neil Groves Sent: Saturday, November 24, 2007 11:41 AM To: boost@lists.boost.org Subject: Re: [boost] FW: [Boost-users] [threads] Using boost::mutexraises access violation exception I have experienced a similar issue with boost::mutex when the mutex was instantiated from DllMain (in a DLL). The Windows implementation uses a Critical Section. I have found that under many startup / shutdown cases the InitializeCriticalSection returns null. I cannot find documentation that states that this happens. The solution is to create the boost::mutex in another manner. Hence if you stop the boost::mutex from being a global variable I think the problem will disappear. I hope this helps, Neil Groves -----Original Message----- From: boost-bounces@lists.boost.org [mailto:boost-bounces@lists.boost.org] On Behalf Of v2cechura Sent: 23 November 2007 14:31 To: boost@lists.boost.org Subject: [boost] FW: [Boost-users] [threads] Using boost::mutex raises access violation exception I posted this to the user's list (no answer till now), but later came to the conclusion that developer's list might be a better place. Any help on th issue appreciated. Vaclav -----Original Post------ 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}; 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; } If line A and line B are commented-out then the error is gone. The project is a multithreaded console application, both boost.threads and rtl are static-linked. Caller stack is: :7c812a5b kernel32.RaiseException + 0x52 :004129aa ___raiseDebuggerException + 0x1A :00412a84 ; ___raiseDebuggerException :7c9037bf ntdll.RtlConvertUlongToLargeInteger + 0x7a :7c90378b ntdll.RtlConvertUlongToLargeInteger + 0x46 :7c90eafa ntdll.KiUserExceptionDispatcher + 0xe :0041FA2B boost::mutex::do_unlock(this=NULL, =:0012FDE8) :0041E3A2 boost::detail::thread::lock_ops<boost::mutex>::unlock(m=NULL, state=:0012FDE8) :0041E12D boost::condition::do_wait<boost::mutex>(this=:0012FEA4, mutex=NULL) :0041DE2E boost::condition::wait<boost::detail::thread::scoped_lock<boost::mutex>
(this=:0012FEA4, lock=:0012FE5C) :0041D42F __UNNS__thread_01c827c2c2c9169a::thread_param::wait(this=:0012FE9C) :0041D201 boost::thread::thread(this=:00000FCC, threadfunc=:00000FC8) :00000001 :0012fe01
I put mutex.cpp in my directory and BCB2006 locates the error to be here: void mutex::do_unlock() { if (m_critical_section) //------------- Access violation HERE release_critical_section(m_mutex); else release_mutex(m_mutex); } The access violation is raised when thread::thread constructor is called. Looking at the caller stack I noticed that mutex parameter is NULL from some point and it results in this==NULL for the last call. Note that this mutex parameter is not the mutex "console" object I use in the program. It is a mutex that boost.threads library uses during thread initialization. On the other hand there is some dependance on mutex "console" because when I comment it out the error is gone. I maked console a pointer with appropriate changes to source and created it using new but it did not help. Am I missing something? Any help appreciated. Does anybody have experiences with boost::thread and mutex under BCB(2006,2007...)? Vaclav _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

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

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@lists.boost.org [mailto:boost-bounces@lists.boost.org] On Behalf Of Anthony Williams Sent: Monday, November 26, 2007 9:00 AM To: boost@lists.boost.org 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
participants (5)
-
Anthony Williams
-
Neil Groves
-
Phil Endecott
-
v2cechura
-
Vaclav Cechura