Skip to content

Commit f6bd578

Browse files
committed
reduce the number of recheck times for the sleep function
1 parent 7a536b6 commit f6bd578

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

include/libipc/rw_lock.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ inline void yield(K& k) noexcept {
7272
++k;
7373
}
7474

75-
template <std::size_t N = 4096, typename K, typename F>
75+
template <std::size_t N = 32, typename K, typename F>
7676
inline void sleep(K& k, F&& f) {
7777
if (k < static_cast<K>(N)) {
7878
std::this_thread::yield();
@@ -84,7 +84,7 @@ inline void sleep(K& k, F&& f) {
8484
++k;
8585
}
8686

87-
template <std::size_t N = 4096, typename K>
87+
template <std::size_t N = 32, typename K>
8888
inline void sleep(K& k) {
8989
sleep<N>(k, [] {
9090
std::this_thread::sleep_for(std::chrono::milliseconds(1));

test/test_sync.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,33 @@ TEST(Sync, Condition) {
160160
cond.broadcast();
161161

162162
for (auto &t : test_conds) t.join();
163+
}
164+
165+
TEST(Sync, ConditionRobust) {
166+
printf("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 1\n");
167+
ipc::sync::condition cond {"test-cond"};
168+
printf("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 2\n");
169+
ipc::sync::mutex lock {"test-mutex"};
170+
printf("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 3\n");
171+
lock.lock();
172+
std::thread unlock {[] {
173+
printf("WWWWWWWWWWWWWWWWWWWWWWWWWWWWWW 1\n");
174+
ipc::sync::condition cond {"test-cond"};
175+
printf("WWWWWWWWWWWWWWWWWWWWWWWWWWWWWW 2\n");
176+
ipc::sync::mutex lock {"test-mutex"};
177+
printf("WWWWWWWWWWWWWWWWWWWWWWWWWWWWWW 3\n");
178+
{
179+
std::lock_guard<ipc::sync::mutex> guard {lock};
180+
}
181+
std::this_thread::sleep_for(std::chrono::seconds(1));
182+
printf("WWWWWWWWWWWWWWWWWWWWWWWWWWWWWW 4\n");
183+
cond.notify();
184+
printf("WWWWWWWWWWWWWWWWWWWWWWWWWWWWWW 5\n");
185+
}};
186+
printf("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 4\n");
187+
cond.wait(lock);
188+
printf("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 5\n");
189+
lock.unlock();
190+
printf("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 6\n");
191+
unlock.join();
163192
}

0 commit comments

Comments
 (0)