1

I have a secondary thread running some code in a loop and i want it to send a callback to the main thread every N loop cycles.

I pass the callback function as a pointer void (*loop_callback)(uint); that i pass to the thread on creation.

How would i call the callback function from the secondary thread on both linux and windows (i use pthread to spawn the thread on linux and CreateThread on windows)? I want it to run on the main thread so that the secondary thread would not wait for the execution to end. I cannot have the main thread wait in a loop either.

JustClaire
  • 451
  • 3
  • 11
  • Instead of using functions and callbacks, you could use some kind of signalling between the threads. The "secondary" thread sets a signal, and then the "main" thread checks for this signal at regular intervals, and if set call the function (in the context of the "main" thread). – Some programmer dude Sep 14 '20 at 08:50
  • You'll want to use some of these families of API functions: https://stackoverflow.com/questions/33098419/linux-posix-equivalent-for-win32s-createevent-setevent-waitforsingleobject. – Lundin Sep 14 '20 at 08:59

0 Answers0