Skip to content

Commit

Permalink
RAII thread notifier (#24)
Browse files Browse the repository at this point in the history
added thread notifier
  • Loading branch information
KjellKod authored Jan 22, 2024
1 parent b565a50 commit 4d7b822
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 379 deletions.
4 changes: 2 additions & 2 deletions benchmark/test_performance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
// #include <chrono>
// #include <future>
// #include <iostream>
// #include <q/mpsc_receiver_round_robin.hpp>
// #include <q/mpsc_fixed_receiver_round_robin.hpp>
// #include <q/q_api.hpp>
// #include <q/spmc_sender_round_robin.hpp>
// #include <q/spmc_fixed_sender_round_robin.hpp>
// #include <string>
// #include <vector>
// //#include "test_helper.hpp"
Expand Down
8 changes: 5 additions & 3 deletions examples/examples.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# examples/examples.cmake
set(MAIN_SRC ${Q_SOURCE_DIR}/examples/spsc_main.cpp)
add_executable(example_spsc ${MAIN_SRC})
set(SPSC_EXAMPLE_SRC ${Q_SOURCE_DIR}/examples/spsc_main.cpp)
add_executable(example_spsc ${SPSC_EXAMPLE_SRC})
target_link_libraries(example_spsc ${Q_LIBRARY})

set(NOTIFY_EXAMPLE_SRC ${Q_SOURCE_DIR}/examples/thread_notify_main.cpp)
add_executable(example_thread_notify ${NOTIFY_EXAMPLE_SRC})
target_link_libraries(example_thread_notify ${Q_LIBRARY})
27 changes: 27 additions & 0 deletions examples/thread_notify_main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <chrono>
#include <iostream>
#include <thread>
#include "q/thread_exit_notifier.hpp"

void coutThreadExit(std::thread::id id) {
std::cout << "Thread " << id << " has exited." << std::endl;
}

void threadRun() {
thread_local ThreadExitNotifier notifier(coutThreadExit);
for (int i = 0; i < 5; ++i) {
std::cout << "Thread " << std::this_thread::get_id() << " is running iteration " << i << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
}

int main() {
{
std::thread thread1(threadRun);
std::thread thread2(threadRun);

thread1.join();
thread2.join();
}
return 0;
}
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 4d7b822

Please sign in to comment.