Skip to content

Commit

Permalink
fix timer causing not able to ctrl-c exit without all timer ending
Browse files Browse the repository at this point in the history
  • Loading branch information
marty1885 committed Dec 10, 2022
1 parent 6c2ecfd commit 36e9f9f
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 4 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
build/
.vscode
.cache
CMakeCache.txt
CMakeFiles
cmake_install.cmake
Makefile
install_manifest.txt
CMakeLists.txt.user
ninjia.build
5 changes: 4 additions & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ add_executable(gnunetpp-fs fs/main.cpp)
target_link_libraries(gnunetpp-fs gnunetfs gnunetcore gnunetidentity gnunetutil gnunetpp)

add_executable(gnunetpp-identity identity/main.cpp)
target_link_libraries(gnunetpp-identity gnunetidentity gnunetcore gnunetutil gnunetpp)
target_link_libraries(gnunetpp-identity gnunetidentity gnunetcore gnunetutil gnunetpp)

add_executable(gnunetpp-scheduler scheduler/main.cpp)
target_link_libraries(gnunetpp-scheduler gnunetcore gnunetutil gnunetpp)
15 changes: 15 additions & 0 deletions examples/scheduler/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <gnunetpp.hpp>
#include <iostream>

void service(const GNUNET_CONFIGURATION_Handle* cfg)
{
std::cout << "Hello world will be printed in 5 seconds" << std::endl;
gnunetpp::scheduler::runLater(std::chrono::seconds(5), []() {
std::cout << "Hello, world!" << std::endl;
});
}

int main()
{
gnunetpp::run(service);
}
11 changes: 10 additions & 1 deletion gnunetpp/gnunetpp-scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,20 @@ void cancel(TaskID id)
g_tasks.remove(id);
}

void cancelAll()
{
for(auto& [id, data] : g_tasks)
GNUNET_SCHEDULER_cancel(data.handle);
g_tasks.clear();
}

static bool running = true;
void shutdown()
{
if(running)
if(running) {
cancelAll();
GNUNET_SCHEDULER_shutdown();
}
running = false;
}

Expand Down
1 change: 1 addition & 0 deletions gnunetpp/gnunetpp-scheduler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ TaskID runEvery(std::chrono::duration<double> delay, std::function<void()> fn);
void runOnShutdown(std::function<void()> fn);
void run(std::function<void()> fn);
void cancel(TaskID id);
void cancelAll();
void shutdown();
}
30 changes: 28 additions & 2 deletions gnunetpp/inner/UniqueData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,34 @@ struct UniqueData

bool contains(size_t id)
{
std::lock_guard l(mtx);
return data.find(id) != data.end();
std::lock_guard l(mtx);
return data.find(id) != data.end();
}

auto begin()
{
return data.begin();
}

auto end()
{
return data.end();
}

auto begin() const
{
return data.begin();
}

auto end() const
{
return data.end();
}

void clear()
{
std::lock_guard l(mtx);
data.clear();
}
protected:
std::mutex mtx;
Expand Down

0 comments on commit 36e9f9f

Please sign in to comment.