4646#include < MuTwkApp/EventType.h>
4747#include < MuTwkApp/MuInterface.h>
4848#include < boost/algorithm/string.hpp>
49- #include < boost/thread.hpp>
5049#include < half.h>
50+ #include < thread>
5151#include < sstream>
5252#include < stdexcept>
5353#include < iostream>
@@ -58,24 +58,7 @@ namespace TwkApp
5858
5959 // Thread safety tracking
6060 // Default constructor creates "not-a-thread" ID
61- static boost::thread::id s_mainThreadId;
62-
63- // Helper function for direct cout printing
64- static PyObject* unsafe_mu_print (PyObject* args)
65- {
66- size_t nargs = PyTuple_Size (args);
67- if (nargs >= 1 )
68- {
69- PyObject* arg = PyTuple_GetItem (args, 0 );
70- if (PyUnicode_Check (arg))
71- {
72- const char * str = PyUnicode_AsUTF8 (arg);
73- if (str)
74- cout << str;
75- }
76- }
77- Py_RETURN_NONE;
78- }
61+ static std::thread::id s_mainThreadId;
7962
8063 Mu::FunctionObject* createFunctionObjectFromPyObject (const Mu::FunctionType* t, PyObject* pyobj)
8164 {
@@ -303,30 +286,12 @@ namespace TwkApp
303286 return NULL ;
304287 }
305288
306- // Thread safety check - initialize main thread ID on first call
307- boost::thread::id currentThreadId = boost::this_thread::get_id ();
308- if (s_mainThreadId == boost::thread::id ()) // Check if uninitialized
309- // (default constructed)
289+ // Thread safety check
290+ if (std::this_thread::get_id () != s_mainThreadId)
310291 {
311- s_mainThreadId = currentThreadId;
312- }
313-
314- if (currentThreadId != s_mainThreadId)
315- {
316- // fix mu print commands from python to at least not crash from
317- // non-main thread
318- // (because python print is often used to debug python code, so
319- // we'll tolerate this because print is likely redirected to the RV
320- // console)
321- if (self->function ->fullyQualifiedName () == " extra_commands._print" )
322- {
323- return unsafe_mu_print (args);
324- }
292+ PyErr_SetString (PyExc_RuntimeError, " Mu is not thread-safe. Mu functions must be called from the main thread" );
325293
326- // this cout will probably get redirected to the RV console, or go
327- // to the terminal window.
328- cout << " WARNING: Mu " << self->function ->fullyQualifiedName () << " () called from non-main thread, will eventually crash "
329- << " (Mu isn't thread-safe)." << endl;
294+ return nullptr ;
330295 }
331296
332297 size_t nargs = PyTuple_Size (args);
@@ -530,9 +495,6 @@ namespace TwkApp
530495
531496 PyTypeObject* pyMuSymbolType () { return &type; }
532497
533- void initPyMuSymbolType ()
534- {
535- //
536- }
498+ void initPyMuSymbolType () { s_mainThreadId = std::this_thread::get_id (); }
537499
538500} // namespace TwkApp
0 commit comments