mirror of
https://github.com/yanyiwu/cppjieba.git
synced 2025-07-18 00:00:12 +08:00
43 lines
825 B
C++
43 lines
825 B
C++
#ifndef LIMONP_THREAD_HPP
|
|
#define LIMONP_THREAD_HPP
|
|
|
|
#include "HandyMacro.hpp"
|
|
#include "NonCopyable.hpp"
|
|
|
|
namespace limonp {
|
|
class IThread: NonCopyable {
|
|
private:
|
|
pthread_t thread_;
|
|
bool isStarted;
|
|
bool isJoined;
|
|
public:
|
|
IThread(): isStarted(false), isJoined(false) {
|
|
}
|
|
virtual ~IThread() {
|
|
if(isStarted && !isJoined) {
|
|
LIMONP_CHECK(!pthread_detach(thread_));
|
|
}
|
|
};
|
|
public:
|
|
virtual void run() = 0;
|
|
void start() {
|
|
LIMONP_CHECK(!isStarted);
|
|
LIMONP_CHECK(!pthread_create(&thread_, NULL, worker_, this));
|
|
isStarted = true;
|
|
}
|
|
void join() {
|
|
LIMONP_CHECK(!isJoined);
|
|
LIMONP_CHECK(!pthread_join(thread_, NULL));
|
|
isJoined = true;
|
|
}
|
|
private:
|
|
static void * worker_(void * data) {
|
|
IThread * ptr = (IThread* ) data;
|
|
ptr->run();
|
|
return NULL;
|
|
}
|
|
};
|
|
}
|
|
|
|
#endif
|