Skip to content

Usage of Arduino_Threads requires extension of ArduinoCore-mbed (Danger of CPU hogging) #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
aentinger opened this issue Oct 27, 2021 · 3 comments
Labels
type: imperfection Perceived defect in any part of project

Comments

@aentinger
Copy link
Contributor

In order to avoid CPU hogging by the main thread (loop() of *.ino file) it is necessary to add rtos::ThisThread::yield(); to ArduinoCore-mbed as shown below:

for (;;) {
	loop();
	if (arduino::serialEventRun) arduino::serialEventRun();
+	rtos::ThisThread::yield();
}

For all *.inot defined threads this is automatically done by this code executed after calling the thread's loop() function.

Since this is currently not the case rtos::ThisThread::yield(); is currently called within the main thread of each example (i.e. here) and I think we should rather avoid exposing the raw mbed API to the wider public.

Any side effects you can think of @facchinm?

CC @pnndra @giulcioffi @pennam @MaxPayne86

@aentinger aentinger added the type: imperfection Perceived defect in any part of project label Oct 27, 2021
@facchinm
Copy link
Collaborator

I think we should benchmark it on common examples but it shouldn't hurt in a normal execution (since all other threads automatically spawned are either yielding or waiting on semaphores already)

@aentinger
Copy link
Contributor Author

In the example Threadsafe_Serial_Writer CPU hogging does occur without rtos::ThisThread::yield(), i.e. the output without this statement within ino::loop() is:

[3097] Thread #0: Lorem ipsum ...
[3099] Thread #0: Lorem ipsum ...
[3101] Thread #0: Lorem ipsum ...
[3103] Thread #0: Lorem ipsum ...
[3104] Thread #0: Lorem ipsum ...

but it really should be

[4532] Thread #0: Lorem ipsum ...
[4534] Thread #1: Lorem ipsum ...
[4536] Thread #2: Lorem ipsum ...
[4537] Thread #3: Lorem ipsum ...
[4539] Thread #0: Lorem ipsum ...
[4541] Thread #1: Lorem ipsum ...
[4543] Thread #2: Lorem ipsum ...

True story 😉

@aentinger
Copy link
Contributor Author

Turned out to be a implementation bug within SerialDispatcher, see #45.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: imperfection Perceived defect in any part of project
Projects
None yet
Development

No branches or pull requests

2 participants