Skip to content

Commit

Permalink
Build examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiavrammsd committed Oct 8, 2023
1 parent 33e6f09 commit fa0d7ca
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ jobs:
working-directory: ${{github.workspace}}/build
shell: bash
# Execute the build. You can specify a specific target with "--target <NAME>"
run: cmake --build . --config ${{ matrix.config.build_type }} --target channel_test11 channel_test14 channel_test17
run: cmake --build . --config ${{ matrix.config.build_type }} --target tests examples

- name: Test
working-directory: ${{github.workspace}}/build
Expand Down
12 changes: 10 additions & 2 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@ function(add_example NAME)
add_executable(${NAME} ${ARGN})

set_target_warnings(${NAME} PRIVATE)
target_link_libraries(${NAME} -ltsan)
target_compile_options(${NAME} PRIVATE -fsanitize=thread)

if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
target_link_libraries(${NAME} -ltsan)
target_compile_options(${NAME} PRIVATE -fsanitize=thread)
endif()

add_dependencies(examples ${NAME})
endfunction()

add_custom_target(examples)

# Examples
add_example(example_basic basic.cpp)
add_example(example_close close.cpp)
add_example(example_move move.cpp)
Expand Down
9 changes: 5 additions & 4 deletions examples/streaming.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <chrono>
#include <future>
#include <iostream>
#include <string>
#include <thread>
#include <utility>

Expand All @@ -14,8 +15,8 @@ int main()
messages channel{threads};

// Continuously get some data on multiple threads and send it all to a channel
auto in = [](messages& ch, int thread, std::chrono::milliseconds pause) {
thread_local static int i = 0;
auto in = [](messages& ch, std::size_t thread, std::chrono::milliseconds pause) {
thread_local static std::size_t i = 0U;

while (true) {
if (ch.closed()) {
Expand All @@ -31,7 +32,7 @@ int main()

std::vector<std::future<void>> in_futures;
for (std::size_t i = 0U; i < threads; ++i) {
in_futures.push_back(std::async(in, std::ref(channel), i, std::chrono::milliseconds{500}));
in_futures.push_back(std::async(in, std::ref(channel), i, std::chrono::milliseconds{500U}));
}

// Stream incoming data to a destination
Expand All @@ -45,7 +46,7 @@ int main()
std::this_thread::sleep_for(after);
ch.close();
};
auto timeout_future = std::async(timeout, std::ref(channel), std::chrono::milliseconds{3000});
auto timeout_future = std::async(timeout, std::ref(channel), std::chrono::milliseconds{3000U});

out_future.wait();
for (auto& future : in_futures) {
Expand Down
2 changes: 2 additions & 0 deletions include/msd/blocking_iterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ template <typename Channel>
class blocking_iterator {
public:
using value_type = typename Channel::value_type;
using reference = typename Channel::value_type&;

explicit blocking_iterator(Channel& ch) : ch_{ch} {}

Expand Down Expand Up @@ -62,6 +63,7 @@ class blocking_iterator {
template <typename T>
struct std::iterator_traits<msd::blocking_iterator<T>> {
using value_type = typename msd::blocking_iterator<T>::value_type;
using reference = typename msd::blocking_iterator<T>::reference;
using iterator_category = std::output_iterator_tag;
};

Expand Down
4 changes: 4 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ function(package_add_test TESTNAME)
endif ()

add_test(NAME ${TESTNAME} COMMAND ${TESTNAME})

add_dependencies(tests ${TESTNAME})
endfunction()

add_custom_target(tests)

# Tests
package_add_test(channel_test11 channel_test.cpp blocking_iterator_test.cpp)
package_add_test(channel_test14 channel_test.cpp blocking_iterator_test.cpp)
Expand Down

0 comments on commit fa0d7ca

Please sign in to comment.