You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
1.5 KiB
72 lines
1.5 KiB
//
|
|
// Created by zhaohe on 19-5-21.
|
|
//
|
|
|
|
#pragma once
|
|
#include <atomic>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <list>
|
|
#include <map>
|
|
#include <memory>
|
|
#include <mutex>
|
|
#include <set>
|
|
#include <sstream>
|
|
#include <string>
|
|
#include <thread>
|
|
#include <vector>
|
|
//
|
|
#include "iflytopcpp/core/basic/signal/signal.hpp"
|
|
#include "iflytopcpp/core/spdlogfactory/logger.hpp"
|
|
#include "iflytopcpp/core/zexception/zexception.hpp"
|
|
namespace iflytop {
|
|
namespace core {
|
|
using namespace std;
|
|
class Thread;
|
|
class Thread {
|
|
ENABLE_LOGGER(Thread);
|
|
|
|
public:
|
|
private:
|
|
string name;
|
|
pthread_t id = 0;
|
|
function<void()> run;
|
|
std::mutex lock_;
|
|
unique_ptr<std::thread> workThread;
|
|
atomic_bool hasJointd = {false};
|
|
atomic_bool exitFlag = {false};
|
|
bool waitingForJoin = false; /*当前线程是否运行退出等待回收*/
|
|
Singal signal;
|
|
|
|
Thread(std::function<void()> run);
|
|
|
|
public:
|
|
Thread(string name, function<void()> run);
|
|
void wake();
|
|
void join();
|
|
bool isWaitingForJoin() { return waitingForJoin; }
|
|
bool getExitFlag() { return exitFlag; }
|
|
pthread_t getId() const;
|
|
|
|
void sleep() { signal.sleep(); }
|
|
void sleepForMs(int64_t ms) { signal.sleep_for_us(ms * 1000); }
|
|
|
|
~Thread();
|
|
|
|
private:
|
|
const shared_ptr<zexception> &getExitException() const;
|
|
};
|
|
class ThisThread {
|
|
Thread *m_thread = NULL;
|
|
|
|
public:
|
|
ThisThread();
|
|
pthread_t getId() const { return pthread_self(); }
|
|
bool getExitFlag();
|
|
void sleep();
|
|
void sleepForMs(int64_t ms);
|
|
void wake();
|
|
};
|
|
|
|
} // namespace core
|
|
} // namespace iflytop
|