Skip to content

Commit 022f821

Browse files
committed
Rewrote thread deprecation notices to help migration
User feedback indicated that the previous deprecation notices were confusing and mislead migration from the old style of thread spawning. The deprecation notices were updated to emphasize the replacement functions, and examples of correct usage were added in the doxygen.
1 parent 756a090 commit 022f821

File tree

1 file changed

+79
-33
lines changed

1 file changed

+79
-33
lines changed

rtos/rtos/Thread.h

Lines changed: 79 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,34 @@
3131

3232
namespace rtos {
3333

34-
/** The Thread class allow defining, creating, and controlling thread functions in the system. */
34+
/** The Thread class allow defining, creating, and controlling thread functions in the system.
35+
*
36+
* Example:
37+
* @code
38+
* #include "mbed.h"
39+
* #include "rtos.h"
40+
*
41+
* Thread thread;
42+
* DigitalOut led1(LED1);
43+
* volatile bool running = true;
44+
*
45+
* // Blink function toggles the led in a long running loop
46+
* void blink(DigitalOut *led) {
47+
* while (running) {
48+
* *led = !*led;
49+
* Thread::wait(1000);
50+
* }
51+
* }
52+
*
53+
* // Spawns a thread to run blink for 5 seconds
54+
* int main() {
55+
* thread.start(led1, blink);
56+
* Thread::wait(5000);
57+
* running = false;
58+
* thread.join();
59+
* }
60+
* @endcode
61+
*/
3562
class Thread {
3663
public:
3764
/** Allocate a new thread without starting execution
@@ -52,15 +79,20 @@ class Thread {
5279
@param stack_size stack size (in bytes) requirements for the thread function. (default: DEFAULT_STACK_SIZE).
5380
@param stack_pointer pointer to the stack area to be used by this thread (default: NULL).
5481
@deprecated
55-
Thread-spawning constructors hide errors and may lead to complex
56-
program state when a thread is declared.
82+
Thread-spawning constructors hide errors. Replaced by thread.start(task).
5783
58-
The explicit Thread::start member function should be used to spawn
59-
a thread.
84+
@code
85+
Thread thread(priority, stack_size, stack_pointer);
86+
87+
osStatus status = thread.start(task);
88+
if (status != osOK) {
89+
error("oh no!");
90+
}
91+
@endcode
6092
*/
6193
MBED_DEPRECATED_SINCE("mbed-os-5.1",
62-
"Thread-spawning constructors hide errors and may lead to complex "
63-
"program state when a thread is declared")
94+
"Thread-spawning constructors hide errors. "
95+
"Replaced by thread.start(task).")
6496
Thread(mbed::Callback<void()> task,
6597
osPriority priority=osPriorityNormal,
6698
uint32_t stack_size=DEFAULT_STACK_SIZE,
@@ -76,21 +108,26 @@ class Thread {
76108
@param stack_size stack size (in bytes) requirements for the thread function. (default: DEFAULT_STACK_SIZE).
77109
@param stack_pointer pointer to the stack area to be used by this thread (default: NULL).
78110
@deprecated
79-
Thread-spawning constructors hide errors and may lead to complex
80-
program state when a thread is declared.
111+
Thread-spawning constructors hide errors. Replaced by thread.start(callback(argument, task)).
112+
113+
@code
114+
Thread thread(priority, stack_size, stack_pointer);
81115
82-
The explicit Thread::start member function should be used to spawn
83-
a thread.
116+
osStatus status = thread.start(callback(argument, task));
117+
if (status != osOK) {
118+
error("oh no!");
119+
}
120+
@endcode
84121
*/
85122
template <typename T>
86123
MBED_DEPRECATED_SINCE("mbed-os-5.1",
87-
"Thread-spawning constructors hide errors and may lead to complex "
88-
"program state when a thread is declared")
89-
Thread(T *obj, void (T::*method)(),
124+
"Thread-spawning constructors hide errors. "
125+
"Replaced by thread.start(callback(argument, task)).")
126+
Thread(T *argument, void (T::*task)(),
90127
osPriority priority=osPriorityNormal,
91128
uint32_t stack_size=DEFAULT_STACK_SIZE,
92129
unsigned char *stack_pointer=NULL) {
93-
constructor(mbed::callback(obj, method),
130+
constructor(mbed::callback(argument, task),
94131
priority, stack_size, stack_pointer);
95132
}
96133

@@ -102,21 +139,26 @@ class Thread {
102139
@param stack_size stack size (in bytes) requirements for the thread function. (default: DEFAULT_STACK_SIZE).
103140
@param stack_pointer pointer to the stack area to be used by this thread (default: NULL).
104141
@deprecated
105-
Thread-spawning constructors hide errors and may lead to complex
106-
program state when a thread is declared.
142+
Thread-spawning constructors hide errors. Replaced by thread.start(callback(argument, task)).
107143
108-
The explicit Thread::start member function should be used to spawn
109-
a thread.
144+
@code
145+
Thread thread(priority, stack_size, stack_pointer);
146+
147+
osStatus status = thread.start(callback(argument, task));
148+
if (status != osOK) {
149+
error("oh no!");
150+
}
151+
@endcode
110152
*/
111153
template <typename T>
112154
MBED_DEPRECATED_SINCE("mbed-os-5.1",
113-
"Thread-spawning constructors hide errors and may lead to complex "
114-
"program state when a thread is declared")
115-
Thread(T *obj, void (*method)(T *),
155+
"Thread-spawning constructors hide errors. "
156+
"Replaced by thread.start(callback(argument, task)).")
157+
Thread(T *argument, void (*task)(T *),
116158
osPriority priority=osPriorityNormal,
117159
uint32_t stack_size=DEFAULT_STACK_SIZE,
118160
unsigned char *stack_pointer=NULL) {
119-
constructor(mbed::callback(obj, method),
161+
constructor(mbed::callback(argument, task),
120162
priority, stack_size, stack_pointer);
121163
}
122164

@@ -128,15 +170,20 @@ class Thread {
128170
@param stack_size stack size (in bytes) requirements for the thread function. (default: DEFAULT_STACK_SIZE).
129171
@param stack_pointer pointer to the stack area to be used by this thread (default: NULL).
130172
@deprecated
131-
Thread-spawning constructors hide errors and may lead to complex
132-
program state when a thread is declared.
173+
Thread-spawning constructors hide errors. Replaced by thread.start(callback(argument, task)).
174+
175+
@code
176+
Thread thread(priority, stack_size, stack_pointer);
133177
134-
The explicit Thread::start member function should be used to spawn
135-
a thread.
178+
osStatus status = thread.start(callback(argument, task));
179+
if (status != osOK) {
180+
error("oh no!");
181+
}
182+
@endcode
136183
*/
137184
MBED_DEPRECATED_SINCE("mbed-os-5.1",
138-
"Thread-spawning constructors hide errors and may lead to complex "
139-
"program state when a thread is declared")
185+
"Thread-spawning constructors hide errors. "
186+
"Replaced by thread.start(callback(argument, task)).")
140187
Thread(void (*task)(void const *argument), void *argument=NULL,
141188
osPriority priority=osPriorityNormal,
142189
uint32_t stack_size=DEFAULT_STACK_SIZE,
@@ -156,13 +203,12 @@ class Thread {
156203
@param method function to be executed by this thread.
157204
@return status code that indicates the execution status of the function.
158205
@deprecated
159-
The start function does not support cv-qualifiers. Replaced by
160-
start(callback(obj, method)).
206+
The start function does not support cv-qualifiers. Replaced by start(callback(obj, method)).
161207
*/
162208
template <typename T, typename M>
163209
MBED_DEPRECATED_SINCE("mbed-os-5.1",
164-
"The start function does not support cv-qualifiers. Replaced by "
165-
"start(callback(obj, method)).")
210+
"The start function does not support cv-qualifiers. "
211+
"Replaced by thread.start(callback(obj, method)).")
166212
osStatus start(T *obj, M method) {
167213
return start(mbed::callback(obj, method));
168214
}

0 commit comments

Comments
 (0)