File tree Expand file tree Collapse file tree 1 file changed +15
-13
lines changed Expand file tree Collapse file tree 1 file changed +15
-13
lines changed Original file line number Diff line number Diff line change @@ -70,23 +70,25 @@ ThreadPool::~ThreadPool() {
70
70
71
71
void ThreadPool::TaskLoop () {
72
72
while (true ) {
73
- std::unique_lock<std::mutex> lock (mutex_) ;
73
+ Task task ;
74
74
75
- scheduled_.wait (
76
- lock, [this ] { return !this ->tasks_ .empty () || !this ->running_ ; });
75
+ {
76
+ std::unique_lock<std::mutex> lock (mutex_);
77
+ scheduled_.wait (
78
+ lock, [this ] { return !this ->tasks_ .empty () || !this ->running_ ; });
77
79
78
- if (!running_ && tasks_.empty ()) {
79
- return ;
80
- }
80
+ if (!running_ && tasks_.empty ()) {
81
+ return ;
82
+ }
81
83
82
- if (tasks_.empty ()) {
83
- PADDLE_THROW (" This thread has no task to Run" );
84
- }
84
+ if (tasks_.empty ()) {
85
+ PADDLE_THROW (" This thread has no task to Run" );
86
+ }
85
87
86
- // pop a task from the task queue
87
- auto task = std::move (tasks_.front ());
88
- tasks_.pop ();
89
- lock. unlock ();
88
+ // pop a task from the task queue
89
+ task = std::move (tasks_.front ());
90
+ tasks_.pop ();
91
+ }
90
92
91
93
// run the task
92
94
task ();
You can’t perform that action at this time.
0 commit comments