Skip to content

Commit 70effdd

Browse files
committed
fix
test=develop
1 parent 64e7688 commit 70effdd

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

paddle/fluid/framework/threadpool_test.cc

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ limitations under the License. */
1919

2020
namespace framework = paddle::framework;
2121

22-
void do_sum(framework::ThreadPool* pool, std::atomic<int>* sum, int cnt) {
23-
std::vector<std::future<void>> fs;
22+
void do_sum(std::vector<std::future<void>>* fs, std::mutex* mu,
23+
std::atomic<int>* sum, int cnt) {
2424
for (int i = 0; i < cnt; ++i) {
25-
fs.push_back(framework::Async([sum]() { sum->fetch_add(1); }));
25+
std::lock_guard<std::mutex> l(*mu);
26+
fs->push_back(framework::Async([sum]() { sum->fetch_add(1); }));
2627
}
2728
}
2829

@@ -40,17 +41,21 @@ TEST(ThreadPool, ConcurrentInit) {
4041
}
4142

4243
TEST(ThreadPool, ConcurrentRun) {
43-
framework::ThreadPool* pool = framework::ThreadPool::GetInstance();
4444
std::atomic<int> sum(0);
4545
std::vector<std::thread> threads;
46+
std::vector<std::future<void>> fs;
47+
std::mutex fs_mu;
4648
int n = 50;
4749
// sum = (n * (n + 1)) / 2
4850
for (int i = 1; i <= n; ++i) {
49-
std::thread t(do_sum, pool, &sum, i);
51+
std::thread t(do_sum, &fs, &fs_mu, &sum, i);
5052
threads.push_back(std::move(t));
5153
}
5254
for (auto& t : threads) {
5355
t.join();
5456
}
57+
for (auto& t : fs) {
58+
t.wait();
59+
}
5560
EXPECT_EQ(sum, ((n + 1) * n) / 2);
5661
}

0 commit comments

Comments
 (0)