Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <thread>
- #include <functional>
- #include <memory.h>
- #include "TQueueTemplate.h"
- using namespace std;
- typedef function<void()> Callback;
- class Active {
- public:
- Active();
- ~Active();
- void send(Callback Msg);
- Active(const Active&) = delete;
- Active& operator=(const Active&) = delete;
- private:
- void doExit() {
- mRunning = false;
- }
- void run();
- static void run_(void *ths);
- private:
- std::thread mThread;
- bool mRunning;
- TQueueTemplate<Callback> mMsgQueue;
- };
- Active::Active() :
- mThread(Active::run_, this), mRunning(true) {
- }
- Active::~Active() {
- send(bind(&Active::doExit, this));
- mThread.join();
- }
- void Active::send(Callback Msg) {
- mMsgQueue.put(Msg);
- }
- void Active::run() {
- std::cout << "Active" << std::endl;
- while (mRunning) {
- Callback Cb = mMsgQueue.get();
- Cb();
- }
- }
- void Active::run_(void *ths) {
- reinterpret_cast<Active *>(ths)->run();
- }
- /*********************************************************************************************************************/
- void func() {
- cout << "func" << endl;
- }
- void func2(int i){
- cout << "func: " << i << endl;
- }
- /*********************************************************************************************************************/
- int main() {
- Active TestActive;
- TestActive.send(func);
- TestActive.send(std::bind(func2, 2));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement