lichao
2021-03-30 8cbb55f3066f71f5a4328193414d4555c87e96be
src/pubsub.h
@@ -18,12 +18,37 @@
#ifndef PUBSUB_4KGRA997
#define PUBSUB_4KGRA997
#include "shm.h"
#include "shm_queue.h"
#include <thread>
#include <atomic>
#include <mutex>
#include <vector>
#include <unordered_map>
#include <set>
namespace bhome_shm {
bool Subscribe(const std::string &topic);
// publish/subcribe manager.
class BusManager
{
    SharedMemory &shm_;
    ShmMsgQueue busq_;
    std::atomic<bool> run_;
    std::vector<std::thread> workers_;
    std::mutex mutex_;
    typedef std::set<MQId> Clients;
    std::unordered_map<std::string, Clients> records_;
    bool StopNoLock();
    void OnMsg(const BHMsg &msg);
public:
    BusManager(SharedMemory &shm);
    ~BusManager();
    bool Start(const int nworker = 2);
    bool Stop();
};
} // namespace bhome_shm
#endif // end of include guard: PUBSUB_4KGRA997