Stop a running thread

Hi everyone.
I’m having trouble wrapping my head around the Threading module.
I wonder what is the best way to stop a thread that is currently running, once some conditions are met.

I managed to achieve this by doing something like:

def threadFunction():
  global condition
  while not condition:
     do something ...
     ...

Updating the value of “condition” externally does stop the thread here, but this looks like a horrible solution.

I think I could probably use the Event class or the Condition class (?) but I’m not sure I understand how.
Any help is much appreciated!
Thanks in advance for your time. :smiley:

The simplest way is like what you’ve done, letting the main function terminate and the RTOS should do all of the cleanup like deleting the task, freeing the memory…
For events and conditions, they ensure synchronization between different tasks, for instance if many tasks access a shared resource.

1 Like

Thanks a lot for your reply, Karim.
I guess it wasn’t that horrible of a solution, after all.

I’ll keep experimenting :smiley: