Created
January 10, 2013 23:12
-
-
Save asukharev/4506612 to your computer and use it in GitHub Desktop.
Thread.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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