Skip to content

Commit 55f822e

Browse files
More dev and debugging
1 parent fdcb4e4 commit 55f822e

3 files changed

Lines changed: 16 additions & 8 deletions

File tree

‎include/queue/wait_queue.hpp‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,13 @@ class wait_queue {
263263
// modifying methods
264264

265265
/**
266-
* @brief Request the @c wait_queue to stop processing.
266+
* @brief Request the @c wait_queue to stop processing, unless a @c std::stop_token
267+
* was passed in to a constructor.
267268
*
268-
* When this method is called, all waiting reader threaders will be notified.
269+
* If a @c std::stop_token was passed into a constructor, a @c request_stop must
270+
* be performed external to the @c wait_queue and this method has no effect.
271+
*
272+
* For an internal @c std::stop_token, all waiting reader threaders will be notified.
269273
* Subsequent @c push operations will return @c false.
270274
*/
271275
bool request_stop() noexcept {

‎test/CMakeLists.txt‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,13 @@ CPMAddPackage ( "gh:martinmoene/ring-span-lite@0.6.0" )
2121
CPMAddPackage ( NAME circular_buffer
2222
URL https://github.com/JustasMasiulis/circular_buffer/archive/refs/heads/master.zip )
2323

24+
set ( CMAKE_THREAD_PREFER_PTHREAD TRUE )
25+
set ( THREADS_PREFER_PTHREAD_FLAG TRUE )
26+
find_package(Threads REQUIRED)
27+
2428
# link dependencies
25-
target_link_libraries ( wait_queue_test PRIVATE wait_queue ring-span-lite circular_buffer Catch2::Catch2WithMain )
29+
target_link_libraries ( wait_queue_test PRIVATE
30+
Threads::Threads wait_queue ring-span-lite circular_buffer Catch2::Catch2WithMain )
2631

2732
enable_testing()
2833

‎test/wait_queue_test.cpp‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ template <typename T, typename Q>
113113
void read_func (Q& wq, test_set<T>& s, std::mutex& mut) {
114114
while (true) {
115115
std::optional<set_elem<T> > opt_elem = wq.wait_and_pop();
116-
if (!opt_elem) { // empty element means close has been called
116+
if (!opt_elem) { // empty element means request stop has been called
117117
return;
118118
}
119119
std::lock_guard<std::mutex> lk(mut);
@@ -157,12 +157,12 @@ bool threaded_test(Q& wq, int num_readers, int num_writers, int slice, const T&
157157
std::this_thread::sleep_for(std::chrono::seconds(1));
158158
std::lock_guard<std::mutex> lk(mut);
159159
if (s.size() == tot) {
160+
wq.request_stop(); // tell the readers it's done
160161
done = true;
161162
}
162163
}
163-
wq.request_stop();
164164

165-
// join readers; since wait queue is closed they should all join immediately
165+
// join readers; since wait queue is stopped they should all join immediately
166166
for (auto& thr : rd_thrs) {
167167
thr.join();
168168
}
@@ -338,7 +338,7 @@ std::size_t vv_pop_func(vv_wq& wq, std::size_t exp) {
338338
std::size_t n { 0u };
339339
while (n < exp) {
340340
auto res = wq.wait_and_pop();
341-
if (!res) { // queue has been closed
341+
if (!res) { // queue has been stopped
342342
break;
343343
}
344344
REQUIRE (*res == data1);
@@ -413,7 +413,6 @@ SCENARIO ( "Threaded wait queue, deque int",
413413
REQUIRE ( threaded_test(wq, 1, 1, 100, 44) );
414414

415415
// Parameters are 5 reader, 3 writer threads, 1000 slice
416-
// threads will be created and joined
417416
REQUIRE ( threaded_test(wq, 5, 3, 1000, 1212) );
418417
// Parameters are 60 reader, 40 writer threads, 5000 slice
419418
REQUIRE ( threaded_test(wq, 60, 40, 5000, 5656) );

0 commit comments

Comments
 (0)