boost::format concurrency issues on mac intel

I am experiencing some strange errors in a multi-threaded environment on MAC OS X, dual core. I am receiving segfaults, bus errors and illegal instruction crashes regularily with simple calls to boost::format operations. Disabling one of the cores on my machine will completely remove the errors (just fyi). If I add a mutex lock around the calls to boost::format in my sample code (inside the for loop of threadRun) then no problems occur. The following code can be used to reproduce the crash. It occurs perhaps one out of every 10 runs. It can crash (viewed by CrashReporter) in multiple places, mainly in ::parse(), and operator%. I compile with -O0. If it would help, I can post the logs from CrashReporter. #include <stdio.h> #include <stdlib.h> #include <string> #include <boost/thread.hpp> #include <boost/bind.hpp> #include <boost/format.hpp> #define NUM_THREAD 10 #define NUM_ITER 100 void threadRun() { for( int i = 0; i < NUM_ITER; i++ ) { boost::format fmt( "format %1% %2% %3%" ); fmt % "Some message" % 10 % "another str"; } } int main() { boost::thread *threads[NUM_THREAD]; for( int i = 0; i < NUM_THREAD; i++ ) { threads[i] = new boost::thread( &threadRun ); } for( int i = 0; i < NUM_THREAD; i++ ) { threads[i]->join(); delete( threads[i] ); } return 0; } Regards, Matthew

The following are two excerpts from the CrashReporter log (template parameters edited out for brevity). Might anyone have any hint or clue as to why we only see this on MAC OS X x86 and not on any other of our platforms? Exception: EXC_BAD_ACCESS (0x0001) Codes: KERN_PROTECTION_FAILURE (0x0002) at 0x00000148 Thread 1 Crashed: 0 libSystem.B.dylib 0x901509c2 swap_m88k_thread_state_xrf_t + 965 1 NodeConsole 0x001514a1 void boost::io::detail::put<...>(char const* const&, boost::io::detail::format_item<..> const&, boost::basic_format<..>::string_type&, char::internal_streambuf_t&, std::locale*) + 37 (feed_args.hpp:134) 2 NodeConsole 0x00151d3f void boost::io::detail::distribute<..>(boost::basic_format<..>&, char const* const&) + 279 (feed_args.hpp:239) 3 NodeConsole 0x00151d96 boost::basic_format<..>& boost::io::detail::feed<..>(boost::basic_format<..>&, char const* const&) + 46 (feed_args.hpp:252) 4 NodeConsole 0x00151e3c boost::basic_format<..>& boost::basic_format<..>::operator%<char const*>(char const* const&) + 24 (format_class.hpp:64) 5 NodeConsole 0x000050db Node::App::CreateSingletons(char const*) + 521 (App.cpp:378) Exception: EXC_BAD_INSTRUCTION (0x0002) Thread 1 Crashed: 0 dyld 0x8fe12fd4 stub_binding_helper_interface + 82 1 NodeConsole 0x0014e85a std::_Bvector_base<std::allocator<bool> >::_Bvector_base[not-in-charge](std::allocator<bool> const&) + 42 (stl_bvector.h:377) 2 NodeConsole 0x0014e880 std::vector<bool, std::allocator<bool> >::vector[in-charge](std::allocator<bool> const&) + 24 (stl_bvector.h:627) 3 NodeConsole 0x00155df7 boost::basic_format<char, std::char_traits<char>, std::allocator<char> >::basic_format[in-charge](char const*) + 79 (format_implementation.hpp:30) 4 NodeConsole 0x000050b8 Node::App::CreateSingletons(char const*) + 486 (App.cpp:377)

I apologize for replying to myself twice now in an empty thread. But the problem was 'fixed' by compiling the code using the latest GCC 4.2.0 (still in only pre-release stage 3). We were previously using GCC 4.0.1. Just in case anyone else encounters the same error.
participants (1)
-
Matthew Edwards