question

HarikrishnanVR-4367 avatar image
0 Votes"
HarikrishnanVR-4367 asked JeanineZhang-MSFT commented

How to block an async method till completion

I have a fire_and_forget method,

 fire_and_forget DeviceInfo::Connect()
 {
    
 // Pair
 DevicePairingResult result = co_await GetDevice().Pairing().PairAsync();
    
 // Connect
 m_BluetoothLEDevice = co_await BluetoothLEDevice::FromIdAsync(GetId());
    
 }

I need to come out from this fire_and_forget function after completing all the async methods inside

Please suggest some ideas



c++
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JeanineZhang-MSFT avatar image
0 Votes"
JeanineZhang-MSFT answered JeanineZhang-MSFT commented

Hi,

I suggest you could try to construct a mutex object, by std::mutex, and then call the member function lock() to lock the mutex, call the member function unlock() to unlock it. You could also try to use lock_guard Class.

And then you could use the condition_variable class to wait for an event when you have a mutex of type unique_lock<mutex>. You only need to call wait and notify_one.

Best Regards,

Jeanine



If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@HarikrishnanVR-4367

May I know if you have got any chance to check my answer? I am glad to help if you have any other questions.

0 Votes 0 ·