diff options
author | chai <chaifix@163.com> | 2018-08-06 13:12:35 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2018-08-06 13:12:35 +0800 |
commit | b17dc97d5aafe741e4139a82456dc04d7cf47dde (patch) | |
tree | 9c0f86c2ca7fda0dadd9cb094b48a1ff7ba851b3 | |
parent | bd848890aa59da667feede96910105c3adab349e (diff) |
*update
-rw-r--r-- | libjin/Thread/Thread.cpp | 42 | ||||
-rw-r--r-- | libjin/Thread/Thread.h | 61 | ||||
-rw-r--r-- | libjin/Time/Timer.cpp | 8 | ||||
-rw-r--r-- | test/03Thread/threadtest.cpp | 23 |
4 files changed, 88 insertions, 46 deletions
diff --git a/libjin/Thread/Thread.cpp b/libjin/Thread/Thread.cpp index e77daa3..ae6498d 100644 --- a/libjin/Thread/Thread.cpp +++ b/libjin/Thread/Thread.cpp @@ -142,22 +142,26 @@ namespace thread { } - void Thread::ThreadData::set(std::string name, Value* value) + void Thread::ThreadData::set(std::string name, Value value) { - if (share.count(name) != 0); + //if (share.count(name) != 0); share[name] = value; } - Thread::Value* Thread::ThreadData::get(std::string name) + Thread::Value Thread::ThreadData::get(std::string name) { - if (share.count(name) != 0) - return nullptr; return share[name]; } + bool Thread::ThreadData::exist(const std::string& name) + { + return share.count(name) == 1; + } + void Thread::ThreadData::remove(std::string name) { - if (share.count(name) == 0) + Lock l(mutex); + if (exist(name)) { share.erase(name); } @@ -193,7 +197,7 @@ namespace thread return running; }; - bool Thread::start(void* p) + bool Thread::start() { Lock l(mutex); if (running) @@ -205,7 +209,7 @@ namespace thread #endif } #if JIN_THREAD_SDL - handle = SDL_CreateThread(threadRunner, name.c_str(), (void*)p); + handle = SDL_CreateThread(threadRunner, name.c_str(), this); #elif JIN_THREAD_CPP handle = new std::thread(); #endif @@ -239,7 +243,7 @@ namespace thread mutex->unlock(); } - void Thread::set(std::string name, Value* value) + void Thread::send(std::string name, Value value) { lock(); common->set(name, value); @@ -247,22 +251,28 @@ namespace thread condition->broadcast(); } - Thread::Value* Thread::get(std::string name) + bool Thread::receive(std::string name) { - Thread::Value* v = common->get(name); + return common->exist(name); + } + + Thread::Value Thread::fetch(std::string name) + { + Thread::Value v = common->get(name); return v; } - Thread::Value* Thread::demand(std::string name) + Thread::Value Thread::demand(std::string name) { - Thread::Value* v = common->get(name); - while (!v) + lock(); + while (!common->exist(name)) { - if (common->get("error")) + if (common->exist("error")) return 0; condition->wait(mutex); - v = common->get(name); } + Thread::Value v = common->get(name); + unlock(); return v; } diff --git a/libjin/Thread/Thread.h b/libjin/Thread/Thread.h index 2e86d73..5b849cf 100644 --- a/libjin/Thread/Thread.h +++ b/libjin/Thread/Thread.h @@ -17,10 +17,28 @@ namespace jin { namespace thread { - + /* + * ӢӢMutual exclusionд Mutexһڶ̱߳Уֹ߳ͬʱͬһԴ + * ȫֱждĻơĿͨƬһһٽcritical sectionɡٽ + * ָһԹԴзʵĴ룬һֻƻ㷨һ̡߳̿ӵжٽDz + * һӦûҪ˻ƵԴУꡢСжϴڶеĴ + * ݡͬ״̬ȵԴάЩԴͬһºǺѵģΪһ߳̿κһʱ̱ͣ + * ߣָѣ + */ class Mutex; class Conditional; + /* + * Thread:demand Receive a message from a thread. Wait for the message to exist before returning. + * Thread:getName Get the name of a thread. + * Thread:kill Forcefully terminate the thread. + * Thread:peek Receive a message from a thread, but leave it in the message box. + * Thread:receive Receive a message from a thread. + * Thread:send Send a message. + * Thread:set Set a value. + * Thread:start Starts the thread. + * Thread:wait Wait for a thread to finish. + */ class Thread { public: @@ -30,6 +48,12 @@ namespace thread bool boolean; char character; void* pointer; + + Value() {}; + Value(int i) : integer(i) {}; + Value(bool b) : boolean(b) {}; + Value(char c) : character(c) {}; + Value(void* p) : pointer(p) {}; }; private: @@ -38,43 +62,46 @@ namespace thread public: ThreadData(Mutex*, Conditional*); ~ThreadData(); - void set(std::string name, Value* value); - Value* get(std::string name); + bool exist(const std::string& name); + void set(std::string name, Value value); + Value get(std::string name); void remove(std::string name); Conditional* condition; Mutex* mutex; private: - std::map<std::string, Value*> share; // threads shared value + std::map<std::string, Value> share; // threads shared value }; public: typedef int(ThreadRunner)(void* /*ThreadData*/); Thread(const std::string name, ThreadRunner threadfuncs); ~Thread(); - bool start(void* paramters); + bool start(); void wait(); - void set(std::string name, Value* value); - Value* get(std::string name); - Value* demand(std::string name); + void send(std::string name, Value value); + bool receive(std::string name); + Value fetch(std::string name); + Value demand(std::string name); const char* getName(); bool isRunning(); - void lock(); void unlock(); - + //void kill(); + //Value peek(); + private: #if JIN_THREAD_SDL - SDL_Thread* handle; // SDL thread + SDL_Thread* handle; // SDL thread #elif JIN_THREAD_CPP - std::thread* handle; // cpp thread + std::thread* handle; // cpp thread #endif - Mutex* mutex; // mutex variable - Conditional* condition; // condition variable + Mutex* mutex; // mutex variable + Conditional* condition; // condition variable ThreadRunner* threadRunner; // thread function - ThreadData* common; // threads common data - std::string name; // thread name - bool running; + ThreadData* common; // threads common data + std::string name; // thread name + bool running; // running }; diff --git a/libjin/Time/Timer.cpp b/libjin/Time/Timer.cpp index 1520828..cfdb4bd 100644 --- a/libjin/Time/Timer.cpp +++ b/libjin/Time/Timer.cpp @@ -23,14 +23,16 @@ namespace time void Timers::update(int ms) { std::vector<Timer*>::iterator it = timers.begin(); - for (; it != timers.end(); ++it) + for (; it != timers.end(); ) { if (!(*it)->process(ms)) { Timer* t = *it; timers.erase(it); delete t; + return; } + ++it; } } @@ -56,8 +58,8 @@ namespace time : type(t) , duration(d) , count(c) - , tickdown(0) - , countdown(0) + , tickdown(d) + , countdown(c) , callback(f) , paramters(p) { diff --git a/test/03Thread/threadtest.cpp b/test/03Thread/threadtest.cpp index d5d75e4..5d03893 100644 --- a/test/03Thread/threadtest.cpp +++ b/test/03Thread/threadtest.cpp @@ -1,4 +1,5 @@ #include <iostream> +#include <functional> #include "jin.h" using namespace std; @@ -30,22 +31,29 @@ void onDraw() int thread2Runner(void* p) { - char* s = (char*)p; + Thread* t = (Thread*)p; int i = 0; while (true) { if (i++ == 1000000000) { i = 0; - cout << s; + cout << (char*)(t->demand("test").pointer); } } return 0; } -int thread3Runner(void* p) +Thread t("Thread 2", thread2Runner); + +void sendFunc(void* p) { + t.send("test", (void*)"hello_"); +} +int thread3Runner(void* p) +{ + return 1; } int main(int argc, char* argv[]) @@ -69,13 +77,8 @@ int main(int argc, char* argv[]) wndSetting.resizable = false; wnd->init(&wndSetting); - timers.every(1000, [](void* p)-> void{ - cout << 1; - }, nullptr); - - Thread t("Thread 2", thread2Runner); - char* str = "_OK"; - t.start(str); + t.start(); + timers.after(2000, sendFunc, nullptr); game->run(); |