From e4eeedbd6faaef380b58236745856b50cebf4461 Mon Sep 17 00:00:00 2001 From: chai Date: Tue, 7 Aug 2018 00:17:24 +0800 Subject: *update --- src/libjin/Thread/Thread.h | 129 +++++++++++++++++++++++++++------------------ 1 file changed, 77 insertions(+), 52 deletions(-) (limited to 'src/libjin/Thread/Thread.h') diff --git a/src/libjin/Thread/Thread.h b/src/libjin/Thread/Thread.h index 4996705..aeb0d9e 100644 --- a/src/libjin/Thread/Thread.h +++ b/src/libjin/Thread/Thread.h @@ -17,54 +17,48 @@ namespace jin { namespace thread { - /* - * 互斥锁(英语:英语:Mutual exclusion,缩写 Mutex)是一种用于多线程编程中,防止两条线程同时对同一公共资源 - * 比如全局变量)进行读写的机制。该目的通过将代码切片成一个一个的临界区域(critical section)达成。临界区域 - * 指的是一块对公共资源进行访问的代码,并非一种机制或是算法。一个程序、进程、线程可以拥有多个临界区域,但是并 - * 不一定会应用互斥锁。需要此机制的资源的例子有:旗标、队列、计数器、中断处理程序等用于在多条并行运行的代码间 - * 传递数据、同步状态等的资源。维护这些资源的同步、一致和完整是很困难的,因为一条线程可能在任何一个时刻被暂停 - * 休眠)或者恢复(唤醒)。 - */ + /** + * 互斥锁(英语:英语:Mutual exclusion,缩写 Mutex)是一种用于多线程编程中,防止两条线程同时对同一公共资源 + * 比如全局变量)进行读写的机制。该目的通过将代码切片成一个一个的临界区域(critical section)达成。临界区域 + * 指的是一块对公共资源进行访问的代码,并非一种机制或是算法。一个程序、进程、线程可以拥有多个临界区域,但是并 + * 不一定会应用互斥锁。需要此机制的资源的例子有:旗标、队列、计数器、中断处理程序等用于在多条并行运行的代码间 + * 传递数据、同步状态等的资源。维护这些资源的同步、一致和完整是很困难的,因为一条线程可能在任何一个时刻被暂停 + * 休眠)或者恢复(唤醒)。 + */ 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. - */ + /** + * 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: - union Value + + union Variant { - enum - { - INTEGER, - REAL, - BOOL, - STRING, - POINTER - } type; - int integer; - float real; bool boolean; + char character; const char* cstring; void* pointer; + float real; - Value() {}; - Value(int i) : integer(i), type(INTEGER){}; - Value(float r) : real(r), type(REAL) {}; - Value(bool b) : boolean(b), type(BOOL) {}; - Value(const char* cstr) : cstring(cstr), type(STRING) {}; - Value(void* p) : pointer(p), type(POINTER) {}; + Variant() {}; + Variant(int i) : integer(i) {}; + Variant(float f) : real(f) {}; + Variant(bool b) : boolean(b) {}; + Variant(char c) : character(c) {}; + Variant(const char* s) : cstring(s) {}; + Variant(void* p) : pointer(p) {}; }; private: @@ -73,36 +67,36 @@ namespace thread public: ThreadData(Mutex*, Conditional*); ~ThreadData(); - bool exist(const std::string& name); - void set(std::string name, Value value); - Value get(std::string name); - void remove(std::string name); + bool exist(int slot); + void set(int slot, Variant value); + Variant get(int slot); + void remove(int slot); Conditional* condition; Mutex* mutex; + static const int SLOT_ERROR = -1; + static const int SLOT_WARN = -2; + private: - std::map share; // threads shared value - std::map sharevalue; + std::map share; // threads shared value }; public: - typedef int(ThreadRunner)(void* /*ThreadData*/); + typedef void(ThreadRunner)(Thread* thread); Thread(const std::string name, ThreadRunner threadfuncs); ~Thread(); bool start(); void wait(); - void send(std::string name, Value value); - bool receive(std::string name); - Value fetch(std::string name); - Value demand(std::string name); + void send(int slot, Variant value); + bool receive(int slot); + Variant fetch(int slot); + Variant demand(int slot); const char* getName(); bool isRunning(); - //void kill(); - //Value peek(); - - private: void lock(); void unlock(); + + private: #if JIN_THREAD_SDL SDL_Thread* handle; // SDL thread #elif JIN_THREAD_CPP @@ -112,9 +106,40 @@ namespace thread Conditional* condition; // condition variable ThreadRunner* threadRunner; // thread function ThreadData* common; // threads common data - const std::string name; // thread name + const std::string name; // thread name, for debugging purposes + /** + * https://stackoverflow.com/questions/149932/naming-conventions-for-threads + * + * Use short names because they don't make the lines in a log file too long. + * + * Create names where the important part is at the beginning. Log viewers in a + * graphical user interface tend to have tables with columns, and the thread + * column is usually small or will be made small by you to read everything else. + * + * Do not use the word "thread" in the thread name because it is obvious. + * + * Make the thread names easily grep-able. Avoid similar sounding thread names + * + * If you have several threads of the same nature, enumerate them with IDs that + * are unique to one execution of the application or one log file, whichever fits + * your logging habits. + * + * Avoid generalizations like "WorkerThread" (how do you name the next 5 worker + * threads?), "GUIThread" (which GUI? is it for one window? for everything?) or + * "Calculation" (what does it calculate?). + * + * If you have a test group that uses thread names to grep your application's log + * files, do not rename your threads after some time. Your testers will hate you for + * doing so. Thread names in well-tested applications should be there to stay. + * + * When you have threads that service a network connection, try to include the target + * network address in the thread name (e.g. channel_123.212.123.3). Don't forget about + * enumeration though if there are multiple connections to the same host. + */ bool running; // running + static int ThreadFunciton(void* p); + }; } // thread -- cgit v1.1-26-g67d0