µOS++ IIIe Reference 7.0.0
The third edition of µOS++, a POSIX inspired open source framework, written in C++
Loading...
Searching...
No Matches
thread-cpp.h
Go to the documentation of this file.
1/*
2 * This file is part of the µOS++ project (https://micro-os-plus.github.io/).
3 * Copyright (c) 2016-2025 Liviu Ionescu. All rights reserved.
4 *
5 * Permission to use, copy, modify, and/or distribute this software
6 * for any purpose is hereby granted, under the terms of the MIT license.
7 *
8 * If a copy of the license was not distributed with this file, it can
9 * be obtained from https://opensource.org/licenses/mit.
10 */
11
12// ============================================================================
13// This file is for internal use in µOS++ and should not be included
14// in applications.
15
16thread::thread (thread&& t) noexcept
17{
18 swap (t);
19}
20
21thread&
23{
24 if (joinable ())
25 {
26 os::trace::printf ("%s() @%p attempt to assign a running thread\n",
27 __func__, this);
28 std::abort (); // in ISO it is std::terminate()
29 }
30 swap (t);
31 return *this;
32}
33
34void
36{
37 if (id_ != id ())
38 {
39 void* args = id_.native_thread_->function_args ();
40 if (args != nullptr && function_object_deleter_ != nullptr)
41 {
42 // Manually delete the function object used to store arguments.
44 }
45
46 // Manually delete the system thread.
47 delete id_.native_thread_;
48 }
49}
50
52{
53 os::trace::printf ("%s() @%p\n", __func__, this);
54 if (joinable ())
55 {
56 os::trace::printf ("%s() @%p attempt to destruct a running thread\n",
57 __func__, this);
58 std::abort (); // in ISO it is std::terminate()
59 }
60
62}
63
64// ------------------------------------------------------------------------
65
66void
67thread::swap (thread& t) noexcept
68{
69 std::swap (id_, t.id_);
70 std::swap (function_object_deleter_, t.function_object_deleter_);
71}
72
73bool
74thread::joinable () const noexcept
75{
76 return !(id_ == thread::id ());
77}
78
79void
81{
82 os::trace::printf ("%s() @%p\n", __func__, this);
83
85
86 id_ = id ();
87 os::trace::printf ("%s() @%p joined\n", __func__, this);
88}
89
90void
92{
93 os::trace::printf ("%s() @%p\n", __func__, this);
94 if (id_ != id ())
95 {
97 }
98
99 // The detached thread will continue to run, but we'll not have
100 // access to it from here, not even to delete it.
101 // TODO: arrange to delete it at exit()?
102
103 id_ = id ();
104 os::trace::printf ("%s() @%p detached\n", __func__, this);
105}
106
107// ==========================================================================
void * function_args(void) const
Get the thread function arguments.
Definition os-thread.h:2364
result_t detach(void)
Detach a thread.
Thread unique id.
native_handle_type native_thread_
Standard thread.
void delete_system_thread(void)
Definition thread-cpp.h:35
bool joinable(void) const noexcept
Definition thread-cpp.h:74
~thread()
Definition thread-cpp.h:51
thread() noexcept=default
void detach(void)
Definition thread-cpp.h:91
thread & operator=(const thread &)=delete
void swap(thread &t) noexcept
Definition thread-cpp.h:67
void join(void)
Definition thread-cpp.h:80
function_object_deleter_t function_object_deleter_
int printf(const char *format,...)
Write a formatted string to the trace device.
Definition trace.cpp:59
void swap(thread &x, thread &y) noexcept