File tree Expand file tree Collapse file tree 2 files changed +22
-10
lines changed
Expand file tree Collapse file tree 2 files changed +22
-10
lines changed Original file line number Diff line number Diff line change @@ -31,9 +31,14 @@ EventLoopThread::EventLoopThread(const std::string &threadName)
3131EventLoopThread::~EventLoopThread ()
3232{
3333 run ();
34- if (loop_)
34+ std::shared_ptr<EventLoop> loop;
3535 {
36- loop_->quit ();
36+ std::unique_lock<std::mutex> lk (loopMutex_);
37+ loop = loop_;
38+ }
39+ if (loop)
40+ {
41+ loop->quit ();
3742 }
3843 if (thread_.joinable ())
3944 {
@@ -51,14 +56,18 @@ void EventLoopThread::loopFuncs()
5156#ifdef __linux__
5257 ::prctl (PR_SET_NAME, loopThreadName_.c_str());
5358#endif
54- thread_local static EventLoop loop;
55- loop.queueInLoop ([this ]() { promiseForLoop_.set_value (1 ); });
56- promiseForLoopPointer_.set_value (&loop);
59+ thread_local static std::shared_ptr<EventLoop> loop =
60+ std::make_shared<EventLoop>();
61+ loop->queueInLoop ([this ]() { promiseForLoop_.set_value (1 ); });
62+ promiseForLoopPointer_.set_value (loop);
5763 auto f = promiseForRun_.get_future ();
5864 (void )f.get ();
59- loop. loop ();
65+ loop-> loop ();
6066 // LOG_DEBUG << "loop out";
61- loop_ = nullptr ;
67+ {
68+ std::unique_lock<std::mutex> lk (loopMutex_);
69+ loop_ = nullptr ;
70+ }
6271}
6372
6473void EventLoopThread::run ()
Original file line number Diff line number Diff line change @@ -48,7 +48,7 @@ class TRANTOR_EXPORT EventLoopThread : NonCopyable
4848 */
4949 EventLoop *getLoop () const
5050 {
51- return loop_;
51+ return loop_. get () ;
5252 }
5353
5454 /* *
@@ -59,10 +59,13 @@ class TRANTOR_EXPORT EventLoopThread : NonCopyable
5959 void run ();
6060
6161 private:
62- EventLoop *loop_;
62+ // With C++20, use std::atomic<std::shared_ptr<EventLoop>>
63+ std::shared_ptr<EventLoop> loop_;
64+ std::mutex loopMutex_;
65+
6366 std::string loopThreadName_;
6467 void loopFuncs ();
65- std::promise<EventLoop * > promiseForLoopPointer_;
68+ std::promise<std::shared_ptr< EventLoop> > promiseForLoopPointer_;
6669 std::promise<int > promiseForRun_;
6770 std::promise<int > promiseForLoop_;
6871 std::once_flag once_;
You can’t perform that action at this time.
0 commit comments