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)
31
31
EventLoopThread::~EventLoopThread ()
32
32
{
33
33
run ();
34
- if (loop_)
34
+ std::shared_ptr<EventLoop> loop;
35
35
{
36
- loop_->quit ();
36
+ std::unique_lock<std::mutex> lk (loopMutex_);
37
+ loop = loop_;
38
+ }
39
+ if (loop)
40
+ {
41
+ loop->quit ();
37
42
}
38
43
if (thread_.joinable ())
39
44
{
@@ -51,14 +56,18 @@ void EventLoopThread::loopFuncs()
51
56
#ifdef __linux__
52
57
::prctl (PR_SET_NAME, loopThreadName_.c_str());
53
58
#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);
57
63
auto f = promiseForRun_.get_future ();
58
64
(void )f.get ();
59
- loop. loop ();
65
+ loop-> loop ();
60
66
// LOG_DEBUG << "loop out";
61
- loop_ = nullptr ;
67
+ {
68
+ std::unique_lock<std::mutex> lk (loopMutex_);
69
+ loop_ = nullptr ;
70
+ }
62
71
}
63
72
64
73
void EventLoopThread::run ()
Original file line number Diff line number Diff line change @@ -48,7 +48,7 @@ class TRANTOR_EXPORT EventLoopThread : NonCopyable
48
48
*/
49
49
EventLoop *getLoop () const
50
50
{
51
- return loop_;
51
+ return loop_. get () ;
52
52
}
53
53
54
54
/* *
@@ -59,10 +59,13 @@ class TRANTOR_EXPORT EventLoopThread : NonCopyable
59
59
void run ();
60
60
61
61
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
+
63
66
std::string loopThreadName_;
64
67
void loopFuncs ();
65
- std::promise<EventLoop * > promiseForLoopPointer_;
68
+ std::promise<std::shared_ptr< EventLoop> > promiseForLoopPointer_;
66
69
std::promise<int > promiseForRun_;
67
70
std::promise<int > promiseForLoop_;
68
71
std::once_flag once_;
You can’t perform that action at this time.
0 commit comments