Correct some threading issues

- prototype avoiding a lock with __thread in g++
 - pass -pthread and -lpthread when building to get threading actually working
This commit is contained in:
Jason Turner 2012-06-22 14:18:44 -06:00
parent d55439a7ac
commit f6e53dd42d
2 changed files with 19 additions and 1 deletions

View File

@ -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})

View File

@ -7,6 +7,8 @@
#ifndef CHAISCRIPT_THREADING_HPP_
#define CHAISCRIPT_THREADING_HPP_
#include <unordered_map>
#ifndef CHAISCRIPT_NO_THREADS
#include <thread>
#include <mutex>
@ -86,6 +88,7 @@ namespace chaiscript
private:
std::shared_ptr<T> get_tls() const
{
unique_lock<mutex> 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<T> *m_data = 0;
if (!m_data) { m_data = new std::shared_ptr<T>(new T()); }
return *m_data;
*/
}
mutable mutex m_mutex;
mutable std::map<std::thread::id, std::shared_ptr<T> > m_instances;
mutable std::unordered_map<std::thread::id, std::shared_ptr<T> > m_instances;
};
#else