Determining when an event queued in an EventQueue is complete

I will start out noting that I’m using MBedOS 5.12. I would like to upgrade but at the moment I cannot.

I am making calls to a library of functions from two different threads. They must be executed serially as the library is not reentrant. I am already making one of those calls periodically using EventQueue::call_every which works fine since the function doesn’t return a value nor does it set information I need continue my processing. However, some of the functions use pointers passed in and return a status.

I need to

  • Wait until the call completes
  • Respond to the returned status
  • Depending on the status, use the values stored by the function call in provided pointers

The EventQueue works great for queuing up the function calls but it doesn’t provide a way to verify if the function is complete. Depending on how I queue the function call, I can determine if the function has been dispatched but not when it is complete.

Am I mistaken in assuming EventQueue executes queued functions serially and not in parallel or in some form of task sharing? I.e., I’m just checking that an EventQueue executes in a single thread.

I cannot modify the library so I’m stuck managing this external to the function calls. Using an Event and associating it with a queue seems promising but I cannot get my head around how to set the value – I think it is replacing a function call and instead pointing a value that is dequeued and sent to the provided callback function. I really just want to be able to supply a callback to the queue that receives the value returned by the queued function call. I really don’t want to create a templatized class since EventQueue already has done that for me.

Is there a way to get the return value of the function call using EventQueue or some other similar class?

The library is a C library so I didn’t think of this initially. EventQueue supports passing in a function pointer and arguments, or a class instance, a pointer to its method, and the args. I think this latter method will do what I need. I will have to wrap the C library in a C++ class so that the class method is called by the EventQueue and sets a flag or something once the C function has returned.

Hello Mason,

Another way could be to use a global shared variable or variables. They can be shared by different threads but in that case it’s necessary to protect them from simultaneous access. One technique is to setup a Mutex and use ConditionVariable to control the access to the shared variable.

Best regards, Zoltan

1 Like