Skip to content

Instantly share code, notes, and snippets.

@asukharev
Created January 10, 2013 23:12
Show Gist options
  • Save asukharev/4506612 to your computer and use it in GitHub Desktop.
Save asukharev/4506612 to your computer and use it in GitHub Desktop.
Thread.h
/*
* Thread.h
*
* Created on: 09.01.2013
* Author: asuk
*/
#ifndef THREAD_H_
#define THREAD_H_
#include <condition_variable>
#include <mutex>
#include <thread>
class Thread
{
protected:
bool notified_;
bool done_;
std::mutex mutex_;
std::condition_variable cond_var_;
public:
Thread(void)
{
done_ = false;
notified_ = false;
}
virtual
~Thread()
{
}
;
void
Start()
{
std::thread producer([&]()
{
cond_var_.notify_one();
std::unique_lock<std::mutex> lock(mutex_);
notified_ = true;
done_ = Worker();
cond_var_.notify_one();
});
std::unique_lock < std::mutex > lock(mutex_);
while (!done_)
{
while (!notified_)
{ // loop to avoid spurious wakeups
cond_var_.wait(lock);
}
Callback();
notified_ = false;
}
// Thread is done
producer.join();
}
virtual bool
Worker()
{
return true;
}
;
virtual void
Callback()
{
}
;
};
#endif /* THREAD_H_ */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment