summaryrefslogtreecommitdiffstats
path: root/test/thread_test.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'test/thread_test.cpp')
-rw-r--r--test/thread_test.cpp68
1 files changed, 68 insertions, 0 deletions
diff --git a/test/thread_test.cpp b/test/thread_test.cpp
new file mode 100644
index 0000000..72ba93f
--- /dev/null
+++ b/test/thread_test.cpp
@@ -0,0 +1,68 @@
+/*
+ This file is part of Raul.
+ Copyright 2007-2017 David Robillard <http://drobilla.net>
+
+ Raul is free software: you can redistribute it and/or modify it under the
+ terms of the GNU General Public License as published by the Free Software
+ Foundation, either version 3 of the License, or any later version.
+
+ Raul is distributed in the hope that it will be useful, but WITHOUT ANY
+ WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+ A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Raul. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "raul/Semaphore.hpp"
+
+#include <atomic>
+#include <iostream>
+#include <thread>
+
+namespace {
+
+using Semaphore = Raul::Semaphore;
+
+thread_local int var(0);
+std::atomic<int> n_errors(0);
+
+void
+wait_for_sem(Semaphore* sem)
+{
+ var = 41;
+ std::cout << "[Waiter] Waiting for signal..." << std::endl;
+ sem->wait();
+ std::cout << "[Waiter] Received signal, exiting" << std::endl;
+ var = 42;
+ if (var != 42) {
+ std::cerr << "[Waiter] var != 42" << std::endl;
+ ++n_errors;
+ }
+}
+
+} // namespace
+
+int
+main()
+{
+ Semaphore sem(0);
+ std::thread waiter(wait_for_sem, &sem);
+
+ var = 24;
+
+ std::cout << "[Main] Signalling..." << std::endl;
+ sem.post();
+
+ std::cout << "[Main] Waiting for waiter..." << std::endl;
+ waiter.join();
+
+ std::cout << "[Main] Exiting" << std::endl;
+
+ if (var != 24) {
+ std::cerr << "[Main] var != 24" << std::endl;
+ ++n_errors;
+ }
+
+ return n_errors.load();
+}