diff --git a/CMakeLists.txt b/CMakeLists.txt index 9c5be8a..b95f0e5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -102,6 +102,11 @@ endif() if (CMAKE_HOST_UNIX) list(APPEND LIBS "dl") + + if (MULTITHREAD_SUPPORT_ENABLED) + list(APPEND LIBS "pthread") + add_definitions(-pthread) + endif() endif(CMAKE_HOST_UNIX) list(APPEND LIBS ${READLINE_LIB}) diff --git a/include/chaiscript/chaiscript_threading.hpp b/include/chaiscript/chaiscript_threading.hpp index a955e2b..2821cfc 100644 --- a/include/chaiscript/chaiscript_threading.hpp +++ b/include/chaiscript/chaiscript_threading.hpp @@ -7,6 +7,8 @@ #ifndef CHAISCRIPT_THREADING_HPP_ #define CHAISCRIPT_THREADING_HPP_ +#include + #ifndef CHAISCRIPT_NO_THREADS #include #include @@ -86,6 +88,7 @@ namespace chaiscript private: std::shared_ptr get_tls() const { + unique_lock lock(m_mutex); auto itr = m_instances.find(std::this_thread::get_id()); @@ -97,10 +100,20 @@ namespace chaiscript m_instances.insert(std::make_pair(std::this_thread::get_id(), new_instance)); return new_instance; + + + /* + static __thread std::shared_ptr *m_data = 0; + + if (!m_data) { m_data = new std::shared_ptr(new T()); } + + return *m_data; + */ } + mutable mutex m_mutex; - mutable std::map > m_instances; + mutable std::unordered_map > m_instances; }; #else