File tree Expand file tree Collapse file tree 2 files changed +5
-5
lines changed Expand file tree Collapse file tree 2 files changed +5
-5
lines changed Original file line number Diff line number Diff line change @@ -47,10 +47,9 @@ EventLoop *EventLoopThreadPool::getNextLoop()
4747{
4848 if (loopThreadVector_.size () > 0 )
4949 {
50- EventLoop *loop = loopThreadVector_[loopIndex_]->getLoop ();
51- ++loopIndex_;
52- if (loopIndex_ >= loopThreadVector_.size ())
53- loopIndex_ = 0 ;
50+ size_t index = loopIndex_.fetch_add (1 , std::memory_order_relaxed);
51+ EventLoop *loop =
52+ loopThreadVector_[index % loopThreadVector_.size ()]->getLoop ();
5453 return loop;
5554 }
5655 return nullptr ;
Original file line number Diff line number Diff line change 1818#include < trantor/exports.h>
1919#include < vector>
2020#include < memory>
21+ #include < atomic>
2122
2223namespace trantor
2324{
@@ -87,6 +88,6 @@ class TRANTOR_EXPORT EventLoopThreadPool : NonCopyable
8788
8889 private:
8990 std::vector<std::shared_ptr<EventLoopThread>> loopThreadVector_;
90- size_t loopIndex_;
91+ std::atomic< size_t > loopIndex_{ 0 } ;
9192};
9293} // namespace trantor
You can’t perform that action at this time.
0 commit comments