diff --git a/Tests/LibThreading/CMakeLists.txt b/Tests/LibThreading/CMakeLists.txt new file mode 100644 index 00000000000000..40021e9d4a5d85 --- /dev/null +++ b/Tests/LibThreading/CMakeLists.txt @@ -0,0 +1,4 @@ +file(GLOB TEST_SOURCES CONFIGURE_DEPENDS "*.cpp") +foreach(source ${TEST_SOURCES}) + serenity_test(${source} LibThreading LIBS LibThreading LibPthread) +endforeach() diff --git a/Tests/LibThreading/TestThread.cpp b/Tests/LibThreading/TestThread.cpp new file mode 100644 index 00000000000000..8d5c7eae48099a --- /dev/null +++ b/Tests/LibThreading/TestThread.cpp @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2021, Spencer Dixon + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include + +TEST_CASE(threads_can_detach) +{ + int should_be_42 = 0; + + auto thread = Threading::Thread::construct([&should_be_42]() { + usleep(10 * 1000); + should_be_42 = 42; + return 0; + }); + thread->start(); + thread->detach(); + usleep(20 * 1000); + + EXPECT(should_be_42 == 42); +} + +TEST_CASE(joining_detached_thread_errors) +{ + auto thread = Threading::Thread::construct([]() { return 0; }); + thread->start(); + thread->detach(); + + EXPECT(thread->join().is_error()); +}