]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Add a test for Threads::Task. 16934/head
authorWolfgang Bangerth <bangerth@colostate.edu>
Sun, 28 Apr 2024 13:18:58 +0000 (18:48 +0530)
committerWolfgang Bangerth <bangerth@colostate.edu>
Sun, 28 Apr 2024 13:18:58 +0000 (18:48 +0530)
tests/multithreading/task_17.cc [new file with mode: 0644]
tests/multithreading/task_17.output [new file with mode: 0644]

diff --git a/tests/multithreading/task_17.cc b/tests/multithreading/task_17.cc
new file mode 100644 (file)
index 0000000..c23dd0b
--- /dev/null
@@ -0,0 +1,72 @@
+// ------------------------------------------------------------------------
+//
+// SPDX-License-Identifier: LGPL-2.1-or-later
+// Copyright (C) 2016 - 2023 by the deal.II authors
+//
+// This file is part of the deal.II library.
+//
+// Part of the source code is dual licensed under Apache-2.0 WITH
+// LLVM-exception OR LGPL-2.1-or-later. Detailed license information
+// governing the source code and code contributions can be found in
+// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
+//
+// ------------------------------------------------------------------------
+
+
+// Allow only 2 threads (= concurrently running tasks) but create 3
+// and make sure each can wait for the next to finish. This requires
+// that in the underlying implementation, when we wait for a task we
+// go back into the scheduler and execute more tasks. In other words,
+// waiting for another task doesn't just block the current task (which
+// would lead to deadlocks because we're already running the maximum
+// number of current tasks) but gives the scheduler the opportunity to
+// work on other tasks.
+
+
+#include <deal.II/base/thread_management.h>
+
+#include "../tests.h"
+
+void
+bottom()
+{
+  deallog << "      Starting task at the bottom" << std::endl;
+  deallog << "        ... ... ..." << std::endl;
+  std::this_thread::sleep_for(std::chrono::seconds(1));
+  deallog << "      Ending task at the bottom" << std::endl;
+}
+
+void
+middle()
+{
+  deallog << "    Starting task in the middle" << std::endl;
+  auto t = Threads::new_task([]() { bottom(); });
+  deallog << "    Waiting for sub-task in the middle" << std::endl;
+  t.join();
+  deallog << "    Ending task in the middle" << std::endl;
+}
+
+void
+top()
+{
+  deallog << "  Starting task at the top" << std::endl;
+  auto t = Threads::new_task([]() { middle(); });
+  deallog << "  Waiting for sub-task at the top" << std::endl;
+  t.join();
+  deallog << "  Ending task at the top" << std::endl;
+}
+
+
+int
+main()
+{
+  initlog();
+
+  MultithreadInfo::set_thread_limit(2);
+
+  deallog << "Starting task in main()" << std::endl;
+  auto t = Threads::new_task([]() { top(); });
+  deallog << "Waiting for task in main" << std::endl;
+  t.join();
+  deallog << "Done in main" << std::endl;
+}
diff --git a/tests/multithreading/task_17.output b/tests/multithreading/task_17.output
new file mode 100644 (file)
index 0000000..2155c5b
--- /dev/null
@@ -0,0 +1,13 @@
+
+DEAL::Starting task in main()
+DEAL::Waiting for task in main
+DEAL::  Starting task at the top
+DEAL::  Waiting for sub-task at the top
+DEAL::    Starting task in the middle
+DEAL::    Waiting for sub-task in the middle
+DEAL::      Starting task at the bottom
+DEAL::        ... ... ...
+DEAL::      Ending task at the bottom
+DEAL::    Ending task in the middle
+DEAL::  Ending task at the top
+DEAL::Done in main

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.