trantor
Non-blocking I/O cross-platform TCP network library, using C++14
Loading...
Searching...
No Matches
EventLoopThread.h
Go to the documentation of this file.
1
14
15#pragma once
16
17#include <trantor/net/EventLoop.h>
19#include <trantor/exports.h>
20#include <mutex>
21#include <thread>
22#include <memory>
23#include <condition_variable>
24#include <future>
25
26namespace trantor
27{
32class TRANTOR_EXPORT EventLoopThread : NonCopyable
33{
34 public:
35 explicit EventLoopThread(const std::string &threadName = "EventLoopThread");
36 ~EventLoopThread();
37
42 void wait();
43
50 {
51 return loop_.get();
52 }
53
59 void run();
60
61 private:
62 // With C++20, use std::atomic<std::shared_ptr<EventLoop>>
63 std::shared_ptr<EventLoop> loop_;
64 std::mutex loopMutex_;
65
66 std::string loopThreadName_;
67 void loopFuncs();
68 std::promise<std::shared_ptr<EventLoop>> promiseForLoopPointer_;
69 std::promise<int> promiseForRun_;
70 std::promise<int> promiseForLoop_;
71 std::once_flag once_;
72 std::thread thread_;
73};
74
75} // namespace trantor
void wait()
Wait for the event loop to exit.
EventLoop * getLoop() const
Get the pointer of the event loop of the thread.
Definition EventLoopThread.h:49
void run()
Run the event loop of the thread. This method doesn't block the current thread.
As the name implies, this class represents an event loop that runs in a particular thread....
Definition EventLoop.h:56
Definition EventLoop.h:34