From c8f9cd5530bff6cdb657885a3f9bd69cfbd50231 Mon Sep 17 00:00:00 2001 From: chai Date: Mon, 6 Aug 2018 21:51:39 +0800 Subject: *update --- build/libjin/libjin.vcxproj | 1 + build/libjin/libjin.vcxproj.filters | 6 ++++++ libjin/Thread/Thread.cpp | 23 ++++++++++++++++++++++- libjin/Thread/Thread.h | 4 +++- libjin/Utils/Proxy/lock.h | 4 ++++ test/03Thread/threadtest.cpp | 3 +-- 6 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 libjin/Utils/Proxy/lock.h diff --git a/build/libjin/libjin.vcxproj b/build/libjin/libjin.vcxproj index 4f90bf7..d2f70d0 100644 --- a/build/libjin/libjin.vcxproj +++ b/build/libjin/libjin.vcxproj @@ -121,6 +121,7 @@ + diff --git a/build/libjin/libjin.vcxproj.filters b/build/libjin/libjin.vcxproj.filters index 7115265..261d25d 100644 --- a/build/libjin/libjin.vcxproj.filters +++ b/build/libjin/libjin.vcxproj.filters @@ -100,6 +100,9 @@ {5de638b5-bb1a-4b71-aaee-1ca813ce3a95} + + {84e2ae2f-5cec-4904-9611-330a0362a288} + @@ -406,6 +409,9 @@ Utils\XML + + Utils\Proxy + diff --git a/libjin/Thread/Thread.cpp b/libjin/Thread/Thread.cpp index ae6498d..81db72c 100644 --- a/libjin/Thread/Thread.cpp +++ b/libjin/Thread/Thread.cpp @@ -144,17 +144,20 @@ namespace thread void Thread::ThreadData::set(std::string name, Value value) { + Lock l(mutex); //if (share.count(name) != 0); share[name] = value; } Thread::Value Thread::ThreadData::get(std::string name) { + Lock l(mutex); return share[name]; } bool Thread::ThreadData::exist(const std::string& name) { + Lock l(mutex); return share.count(name) == 1; } @@ -169,6 +172,14 @@ namespace thread ////////////////////////////////////////////////////////////////////// + int Thread::ThreadFunciton(void* p) + { + Thread* thread = (Thread*)p; + if (thread->threadRunner != nullptr) + thread->threadRunner(thread); + return 0; + } + Thread::Thread(const std::string tname, ThreadRunner runner) : name(tname) , running(false) @@ -209,7 +220,7 @@ namespace thread #endif } #if JIN_THREAD_SDL - handle = SDL_CreateThread(threadRunner, name.c_str(), this); + handle = SDL_CreateThread(ThreadFunciton, name.c_str(), this); #elif JIN_THREAD_CPP handle = new std::thread(); #endif @@ -264,6 +275,16 @@ namespace thread Thread::Value Thread::demand(std::string name) { + /** + * pthread_mutex_lock(mtx); + * while(pass == 0) + * { + * pthread_mutex_unlock(mtx); + * pthread_cond_just_wait(cv); + * pthread_mutex_lock(mtx); + * } + * pthread_mutex_unlock(mtx); + */ lock(); while (!common->exist(name)) { diff --git a/libjin/Thread/Thread.h b/libjin/Thread/Thread.h index 5b849cf..b2087eb 100644 --- a/libjin/Thread/Thread.h +++ b/libjin/Thread/Thread.h @@ -74,7 +74,7 @@ namespace thread }; public: - typedef int(ThreadRunner)(void* /*ThreadData*/); + typedef int(ThreadRunner)(Thread* thread); Thread(const std::string name, ThreadRunner threadfuncs); ~Thread(); bool start(); @@ -103,6 +103,8 @@ namespace thread std::string name; // thread name bool running; // running + static int ThreadFunciton(void* p); + }; } // thread diff --git a/libjin/Utils/Proxy/lock.h b/libjin/Utils/Proxy/lock.h new file mode 100644 index 0000000..29194a1 --- /dev/null +++ b/libjin/Utils/Proxy/lock.h @@ -0,0 +1,4 @@ +class Lock +{ + +}; \ No newline at end of file diff --git a/test/03Thread/threadtest.cpp b/test/03Thread/threadtest.cpp index 5d03893..08eb69d 100644 --- a/test/03Thread/threadtest.cpp +++ b/test/03Thread/threadtest.cpp @@ -29,9 +29,8 @@ void onDraw() } -int thread2Runner(void* p) +int thread2Runner(Thread* t) { - Thread* t = (Thread*)p; int i = 0; while (true) { -- cgit v1.1-26-g67d0