Boost logo

Boost Users :

From: justin_michel (justin_michel_at_[hidden])
Date: 2003-03-11 12:08:06


I can't explain it either. It seems to make no sense at all, and I'm
not sure if it is limited to calling from a JNI thread or just a
DLL. I will try to make a complete and simple test case to reproduce
the problem, but in the meantime I made the changes in thread.cpp
and everything seems to be working now. I'm even using condition
variables in my own code. Here's the changes I had to make to get my
code working:

Index: libs/thread/src/thread.cpp
===================================================================
RCS file: /cvsroot/boost/boost/libs/thread/src/thread.cpp,v
retrieving revision 1.11
diff -u -r1.11 thread.cpp
--- libs/thread/src/thread.cpp 4 Feb 2003 23:22:58 -0000 1.11
+++ libs/thread/src/thread.cpp 11 Mar 2003 16:56:59 -0000
@@ -27,32 +27,22 @@
 
 #include "timeconv.inl"
 
+#include <memory>
+
 namespace {
 
 class thread_param
 {
 public:
     thread_param(const boost::function0<void>& threadfunc)
- : m_threadfunc(threadfunc), m_started(false)
- {
- }
- void wait()
+ : m_threadfunc(threadfunc)
     {
- boost::mutex::scoped_lock scoped_lock(m_mutex);
- while (!m_started)
- m_condition.wait(scoped_lock);
     }
- void started()
- {
- boost::mutex::scoped_lock scoped_lock(m_mutex);
- m_started = true;
- m_condition.notify_one();
+ boost::function0<void> getfunc() {
+ return m_threadfunc;
     }
-
- boost::mutex m_mutex;
- boost::condition m_condition;
- const boost::function0<void>& m_threadfunc;
- bool m_started;
+private:
+ const boost::function0<void> m_threadfunc;
 };
 
 } // unnamed namespace
@@ -66,16 +56,12 @@
         static OSStatus thread_proxy(void* param)
 #endif
     {
- try
- {
- thread_param* p = static_cast<thread_param*>(param);
- boost::function0<void> threadfunc = p->m_threadfunc;
- p->started();
- threadfunc();
- }
- catch (...)
- {
- }
+ thread_param* p = static_cast<thread_param*>(param);
+ if (p != 0) {
+ boost::function0<void> threadfunc = p->getfunc();
+ delete p;
+ threadfunc();
+ }
 #if defined(BOOST_HAS_MPTASKS)
         ::boost::detail::thread_cleanup();
 #endif
@@ -105,15 +91,16 @@
 thread::thread(const function0<void>& threadfunc)
     : m_joinable(true)
 {
- thread_param param(threadfunc);
+ std::auto_ptr<thread_param> param(new thread_param(threadfunc));
 #if defined(BOOST_HAS_WINTHREADS)
- m_thread = reinterpret_cast<void*>(_beginthreadex(0, 0,
&thread_proxy,
- &param, 0, &m_id));
- if (!m_thread)
- throw thread_resource_error();
+ m_thread = reinterpret_cast<HANDLE>(_beginthreadex(0, 0,
&thread_proxy,
+ param.get(), 0, &m_id));
+ if (m_thread == 0) {
+ throw thread_resource_error();
+ }
 #elif defined(BOOST_HAS_PTHREADS)
     int res = 0;
- res = pthread_create(&m_thread, 0, &thread_proxy, &param);
+ res = pthread_create(&m_thread, 0, &thread_proxy, param);
     if (res != 0)
         throw thread_resource_error();
 #elif defined(BOOST_HAS_MPTASKS)
@@ -127,7 +114,7 @@
     lStatus = MPCreateQueue(&m_pJoinQueueID);
     if (lStatus != noErr) throw thread_resource_error();
 
- lStatus = MPCreateTask(&thread_proxy, &param, 0UL,
m_pJoinQueueID, NULL,
+ lStatus = MPCreateTask(&thread_proxy, param, 0UL,
m_pJoinQueueID, NULL,
         NULL, 0UL, &m_pTaskID);
     if (lStatus != noErr)
     {
@@ -136,7 +123,7 @@
         throw thread_resource_error();
     }
 #endif
- param.wait();
+ param.release();
 }
 
 thread::~thread()


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