class SimpleThread : public PlatformThread::Delegate {
public:
class Options {
public:
Options() : stack_size_(0) { }
~Options() { }
// We use the standard compiler-supplied copy constructor.
// A custom stack size, or 0 for the system default.
void set_stack_size(size_t size) { stack_size_ = size; }
size_t stack_size() const { return stack_size_; }
private:
size_t stack_size_;
};
// Create a SimpleThread. |options| should be used to manage any specific
// configuration involving the thread creation and management.
// Every thread has a name, in the form of |name_prefix|/TID, for example
// "my_thread/321". The thread will not be created until Start() is called.
explicit SimpleThread(const std::string& name_prefix)
: name_prefix_(name_prefix), name_(name_prefix),
thread_(), event_(true, false), tid_(0), joined_(false) { }
SimpleThread(const std::string& name_prefix, const Options& options)
: name_prefix_(name_prefix), name_(name_prefix), options_(options),
thread_(), event_(true, false), tid_(0), joined_(false) { }
virtual ~SimpleThread();
virtual void Start();
virtual void Join();
// We follow the PlatformThread Delegate interface.
virtual void ThreadMain();
// Subclasses should override the Run method.
virtual void Run() = 0;
// Return the thread name prefix, or "unnamed" if none was supplied.
std::string name_prefix() { return name_prefix_; }
// Return the completed name including TID, only valid after Start().
std::string name() { return name_; }
// Return the thread id, only valid after Start().
PlatformThreadId tid() { return tid_; }
// Return True if Start() has ever been called.
bool HasBeenStarted() { return event_.IsSignaled(); }
// Return True if Join() has evern been called.
bool HasBeenJoined() { return joined_; }
private:
const std::string name_prefix_;
std::string name_;
const Options options_;
PlatformThreadHandle thread_; // PlatformThread handle, invalid after Join!
WaitableEvent event_; // Signaled if Start() was ever called.
PlatformThreadId tid_; // The backing thread's id.
bool joined_; // True if Join has been called.
};