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()
47
47
{
48
48
if (loopThreadVector_.size () > 0 )
49
49
{
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 ();
54
53
return loop;
55
54
}
56
55
return nullptr ;
Original file line number Diff line number Diff line change 18
18
#include < trantor/exports.h>
19
19
#include < vector>
20
20
#include < memory>
21
+ #include < atomic>
21
22
22
23
namespace trantor
23
24
{
@@ -87,6 +88,6 @@ class TRANTOR_EXPORT EventLoopThreadPool : NonCopyable
87
88
88
89
private:
89
90
std::vector<std::shared_ptr<EventLoopThread>> loopThreadVector_;
90
- size_t loopIndex_;
91
+ std::atomic< size_t > loopIndex_{ 0 } ;
91
92
};
92
93
} // namespace trantor
You can’t perform that action at this time.
0 commit comments