aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libjin/Thread/Thread.cpp38
-rw-r--r--libjin/Thread/Thread.h36
-rw-r--r--test/03Thread/threadtest.cpp19
3 files changed, 46 insertions, 47 deletions
diff --git a/libjin/Thread/Thread.cpp b/libjin/Thread/Thread.cpp
index 9caae87..374778b 100644
--- a/libjin/Thread/Thread.cpp
+++ b/libjin/Thread/Thread.cpp
@@ -142,30 +142,30 @@ namespace thread
{
}
- void Thread::ThreadData::set(std::string name, Value value)
+ void Thread::ThreadData::set(int slot, Variant value)
{
Lock l(mutex);
- share[name] = value;
+ share[slot] = value;
}
- Thread::Value Thread::ThreadData::get(std::string name)
+ Thread::Variant Thread::ThreadData::get(int slot)
{
Lock l(mutex);
- return share[name];
+ return share[slot];
}
- bool Thread::ThreadData::exist(const std::string& name)
+ bool Thread::ThreadData::exist(int slot)
{
Lock l(mutex);
- return share.count(name) == 1;
+ return share.count(slot) == 1;
}
- void Thread::ThreadData::remove(std::string name)
+ void Thread::ThreadData::remove(int slot)
{
Lock l(mutex);
- if (exist(name))
+ if (exist(slot))
{
- share.erase(name);
+ share.erase(slot);
}
}
@@ -253,26 +253,26 @@ namespace thread
mutex->unlock();
}
- void Thread::send(std::string name, Value value)
+ void Thread::send(int slot, Variant value)
{
lock();
- common->set(name, value);
+ common->set(slot, value);
unlock();
condition->broadcast();
}
- bool Thread::receive(std::string name)
+ bool Thread::receive(int slot)
{
- return common->exist(name);
+ return common->exist(slot);
}
- Thread::Value Thread::fetch(std::string name)
+ Thread::Variant Thread::fetch(int slot)
{
- Thread::Value v = common->get(name);
+ Thread::Variant v = common->get(slot);
return v;
}
- Thread::Value Thread::demand(std::string name)
+ Thread::Variant Thread::demand(int slot)
{
/**
* pthread_mutex_lock(mtx);
@@ -285,13 +285,13 @@ namespace thread
* pthread_mutex_unlock(mtx);
*/
lock();
- while (!common->exist(name))
+ while (!common->exist(slot))
{
- if (common->exist("error"))
+ if (common->exist(ThreadData::SLOT_ERROR))
return 0;
condition->wait(mutex);
}
- Thread::Value v = common->get(name);
+ Thread::Variant v = common->get(slot);
unlock();
return v;
}
diff --git a/libjin/Thread/Thread.h b/libjin/Thread/Thread.h
index 1be1e9b..72e7085 100644
--- a/libjin/Thread/Thread.h
+++ b/libjin/Thread/Thread.h
@@ -42,7 +42,8 @@ namespace thread
class Thread
{
public:
- union Value
+
+ union Variant
{
int integer;
bool boolean;
@@ -50,12 +51,12 @@ namespace thread
const char* cstring;
void* pointer;
- Value() {};
- Value(int i) : integer(i) {};
- Value(bool b) : boolean(b) {};
- Value(char c) : character(c) {};
- Value(const char* s) : cstring(s) {};
- Value(void* p) : pointer(p) {};
+ Variant() {};
+ Variant(int i) : integer(i) {};
+ Variant(bool b) : boolean(b) {};
+ Variant(char c) : character(c) {};
+ Variant(const char* s) : cstring(s) {};
+ Variant(void* p) : pointer(p) {};
};
private:
@@ -64,15 +65,18 @@ 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<std::string, Value> share; // threads shared value
+ std::map<int, Variant> share; // threads shared value
};
public:
@@ -81,10 +85,10 @@ namespace thread
~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 lock();
diff --git a/test/03Thread/threadtest.cpp b/test/03Thread/threadtest.cpp
index 08eb69d..a792e53 100644
--- a/test/03Thread/threadtest.cpp
+++ b/test/03Thread/threadtest.cpp
@@ -29,7 +29,7 @@ void onDraw()
}
-int thread2Runner(Thread* t)
+void thread2Runner(Thread* t)
{
int i = 0;
while (true)
@@ -37,22 +37,15 @@ int thread2Runner(Thread* t)
if (i++ == 1000000000)
{
i = 0;
- cout << (char*)(t->demand("test").pointer);
+ cout << (char*)(t->demand(1).pointer);
}
}
- return 0;
}
-Thread t("Thread 2", thread2Runner);
-
void sendFunc(void* p)
{
- t.send("test", (void*)"hello_");
-}
-
-int thread3Runner(void* p)
-{
- return 1;
+ Thread* t = (Thread*)p;
+ t->send(1, "hello_");
}
int main(int argc, char* argv[])
@@ -76,8 +69,10 @@ int main(int argc, char* argv[])
wndSetting.resizable = false;
wnd->init(&wndSetting);
+ Thread t("Count", thread2Runner);
+
t.start();
- timers.after(2000, sendFunc, nullptr);
+ timers.after(2000, sendFunc, &t);
game->run();