#include "Thread.h" Thread::Thread(): m_isstop(true), m_autoDelete(false) { } Thread::~Thread() { if(!m_isstop) { SDL_KillThread(m_t); m_t = 0; } for(std::map::iterator mutex = m_mutexs.begin();mutex != m_mutexs.end();mutex++) SDL_DestroyMutex(mutex->second); } bool Thread::start() { if(m_isstop) { m_t = SDL_CreateThread(Thread::ThreadInit, this); if(m_t != 0) { m_isstop = false; return true; } } return false; } void Thread::stop() { if(!m_isstop) { SDL_KillThread(m_t); m_t = 0; } m_isstop = true; } void Thread::join() { SDL_WaitThread(m_t, 0); m_isstop = true; } void Thread::setAutoDelete(bool autoDelete) { m_autoDelete = autoDelete; } int Thread::ThreadInit(void* param) { Thread *t(reinterpret_cast(param)); t->run(); if(t->m_autoDelete) delete t; return 0; } bool Thread::threadRunning() { return m_isstop; } bool Thread::createLockMutex(std::string name) { std::map::iterator mutex = m_mutexs.find(name); if(m_mutexs.find(name) == m_mutexs.end()) { m_mutexs[name] = SDL_CreateMutex(); mutex = m_mutexs.find(name); } if(SDL_mutexP(mutex->second)==-1) return false; return true; } bool Thread::lockMutex(std::string name) { std::map::iterator mutex = m_mutexs.find(name); if(m_mutexs.find(name) == m_mutexs.end()) return false; if(SDL_mutexP(mutex->second)==-1) return false; return true; } bool Thread::unlockMutex(std::string name) { std::map::iterator mutex = m_mutexs.find(name); if(m_mutexs.find(name) == m_mutexs.end()) return false; if(SDL_mutexV(mutex->second)==-1) return false; return true; }