Skip to content

Commit acd34d0

Browse files
Moving files from utility-rack to here
1 parent acf8193 commit acd34d0

4 files changed

Lines changed: 532 additions & 0 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Copyright (c) 2024 by Cliff Green
2+
#
3+
# https://github.com/connectivecpp/wait-queue
4+
#
5+
# I'm still learning CMake, so improvement suggestions are always welcome.
6+
#
7+
# Distributed under the Boost Software License, Version 1.0.
8+
# (See accompanying file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9+
10+
cmake_minimum_required ( VERSION 3.14 FATAL_ERROR )
11+
12+
project ( wait_queue
13+
LANGUAGES CXX
14+
DESCRIPTION "A multi producer / multi consumer thread safe wait queue"
15+
HOMEPAGE "https://githug.com/connectivecpp/wait-queue/" )
16+
17+
option ( WAIT_QUEUE_BUILD_TESTS "Build unit tests" OFF )
18+
option ( WAIT_QUEUE_BUILD_EXAMPLES "Build examples" OFF )
19+
option ( WAIT_QUEUE_INSTALL "Install header only library" OFF )
20+
21+
# add library targets
22+
23+
add_library ( wait_queue INTERFACE )
24+
add_library ( queue::wait_queue ALIAS wait_queue )
25+
26+
# configure library target
27+
28+
target_include_directories ( wait_queue INTERFACE
29+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/queue/>
30+
$<INSTALL_INTERFACE:include/queue/> )
31+
target_compile_features ( wait_queue INTERFACE cxx_std_20 )
32+
33+
# check to build unit tests
34+
if ( ${WAIT_QUEUE_BUILD_TESTS} )
35+
enable_testing()
36+
add_subdirectory ( test )
37+
endif ()
38+
39+
# check to build example code
40+
if ( ${WAIT_QUEUE_BUILD_EXAMPLES } )
41+
add_subdirectory ( example )
42+
endif ()
43+
44+
# check to install
45+
if ( ${WAIT_QUEUE_INSTALL } )
46+
set ( CPACK_RESOURCE_FILE_LICENSE ${CMAKE_CURRENT_SOURCE_DIR}/LICENSE.txt )
47+
include ( CPack )
48+
endif ()
49+
50+
# end of file
51+

‎cmake/download_cpm.cmake‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
# copied from CPM.cmake GitHub site
3+
# download CPM.cmake
4+
5+
file(
6+
DOWNLOAD
7+
https://github.com/cpm-cmake/CPM.cmake/releases/download/v0.39.0/CPM.cmake
8+
${CMAKE_CURRENT_BINARY_DIR}/cmake/CPM.cmake
9+
)
10+
include(${CMAKE_CURRENT_BINARY_DIR}/cmake/CPM.cmake)

‎example/wait_queue_example.cpp‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/** @file
2+
*
3+
* @brief Example code demonstrating use of @c chops::wait_queue.
4+
* See @c threaded_wait_shared_demo.cpp for multithreaded example.
5+
*
6+
* @ingroup example_module
7+
*
8+
* @author Thurman Gillespy
9+
*
10+
* Copyright (c)2019 by Thurman Gillespy
11+
* 3/22/19
12+
*
13+
* Distributed under the Boost Software License, Version 1.0.
14+
* (See accompanying file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
15+
*
16+
* Sample make file:
17+
* g++ -std=c++17 -I ~/Projects/utility-rack/include/ wait_queue_demo.cpp
18+
*
19+
*/
20+
21+
#include <iostream>
22+
#include <cstdlib> // EXIT_SUCCESS
23+
#include <string>
24+
#include <optional> // std::optional
25+
26+
#include "queue/wait_queue.hpp"
27+
28+
// print queue stats
29+
void queueState(const chops::wait_queue<int>& wq) {
30+
31+
std::cout << "wait queue contains " << wq.size() << " elements" << std::endl;
32+
std::cout << "wait queue is empty: " << (wq.empty() ? "true" : "false") << std::endl;
33+
std::cout << "wait queue is open: " << (wq.is_closed() ? "false" : "true") << std::endl;
34+
std::cout << std::endl;
35+
}
36+
37+
// tasty lambda utilities
38+
constexpr auto printStr = [] (const std::string& s) { std::cout << s << std::endl; };
39+
constexpr auto printLn = [] () { std::cout << std::endl; };
40+
41+
int main() {
42+
43+
// create a new (empty) queue
44+
printStr("create new wait queue");
45+
printLn();
46+
chops::wait_queue<int> wq;
47+
queueState(wq);
48+
49+
// put some values in the queue
50+
printStr("pushing elements onto the queue...");
51+
wq.push(42); wq.push(22); wq.push(102); wq.push(-12); wq.push(17);
52+
queueState(wq);
53+
54+
// print all the values
55+
printStr("print all the values in queue");
56+
wq.apply([] (int elem) { std::cout << elem << " "; });
57+
printLn();
58+
59+
// remove the elements
60+
printStr("pop (and remove) each element from the queue");
61+
auto num_elements = wq.size();
62+
while (num_elements-- > 0) {
63+
auto result = wq.try_pop(); // std::optional<int>
64+
if (result) {
65+
std::cout << result.value() << " ";
66+
}
67+
}
68+
printLn();
69+
70+
// all done, close shop
71+
wq.close();
72+
queueState(wq);
73+
74+
return EXIT_SUCCESS;
75+
}

0 commit comments

Comments
 (0)