How do you properly Await/Await Asunc in C++?

  async-await, asynchronous, c++, c++11, std-future

Sorry if the question is not clear. i’ll try to explain it here.

I am working on a test project where two nodes will be communicating specialty packets to each other. As in: Node A will be sending a packet to Node B, and while Node B is generating it’s packet, Node B Should also be processing the information from Node A. Node A should be preparing another packet for when Node B.

I’ve been reading into std::future but I’m not sure i understand how it works. The question i have is about the large section of commented out code. This is just psuedo code so i can try and get a better understanding about asynchronous functions in C++. I normally program in C#/Java where Asynchronous functions are fairly simple(ish). I hope the above kind of explains what i’m trying to do.

The code below might have some other errors. this is just a quick-and-dirty attempt to try and convey what i am asking.

My questions are:

  1. What is the best way to check if Node B is ready, assuming that Node B is a separate instance of this theoretical app, running on a different device? if this makes any difference, this is being written with Linux C++.

  2. Is this even the correct way of doing Tasks/Await/Await Async in C++? If this is incorrect, what is the proper way?

Thanks in advance.

//Excerpt from pseudo code. file would be kw_worker_delegate.h

#include <future>
#include <iostream>
#include <string>
#include <optional>
#include <system_error>

#include "kw_network.h"

class kw_worker
{
private:
   /* ... */
   std::future<kw_packet> *kw_packet_receive_delegate;
   std::future<bool> *kw_packet_send_delegate;
   
   bool worker_is_ready;
   
   std::string kw_worker_adress;
    
   network_interface *kw_network_interface;

   /* Base CTOR that provides Delegates */
public:
   kw_worker(std::future<kw_packet> *kwpktr, std::future<kw_packet> *kwpkts, network_interface *kwinetf, std::string kwaddr);

   bool kw_worker_execute(bool isSendOrRecieve, kw_worker *active_worker, kw_worker *target_worker, std::optional<kw_packet> packet_data = std::nullopt);
};

kw_worker::kw_worker(std::future<kw_packet> *kwpktr, std::future<bool> *kwpkts, network_interface *kwinetf, std::string kwaddr)
{
   &kw_packet_recieve_delegate = &kwpktr;
   &kw_packet_send_delegate = &kwpkts;
   &kw_network_interface = &kwintef;
   kw_worker_address = kwaddr;
   kw_worker_is_ready = true;
}

bool kw_Worker::kw_worker_execute(bool isSendOrRecieve, kw_worker *active_worker, kw_worker *target_worker, std::optional<kw_packet> packet_data = std::nullopt);
{
   try
   {
    //if(isSendOrRecieve)
    //{
    //    IS THIS CORRECT?
    //
    //   if(!(&target_worker->worker_is_ready))
    //   {
    //      cout << "Worker B is not ready for a data submission...n";
    //      do_something_or_await();
    //   }else{ 
    //      if(packet_data.has_value())
    //         kw_packet_send_delegate = std::async (&kw_network_interface->send_to, &target_worker->kw_worker_adress, packet_data);
    //      else throw -1;
    //      cout << "The data was sent to Worker B. Waiting for response on receipt of data...n";
    //      &active_worker->worker_is_ready = true;
    //
    //      //Do somewithng else...
    //   }
    //}else{
    //   if(!(&active_worker->worker_is_ready))
    //   {
    //      cout << "Worker A is not ready to receive data...n";
    //      do_something_or_await();
    //   }else{ 
    //      kw_packet_receive_delegate = std::async (&kw_network_interface->receive_from, &target_worker->kw_worker_adress);
    //      cout << "The data was received by Worker A. A Will now process the data...n";
    //  &active_worker->kw_worker_is_ready = false;
    //   
    //      //Worker B will set it's worker_is_ready boolean value using it's send function
    //   
    //      //process the data now
    //   }
    //}
    }catch(const system_error &e)
    {
        cout << "There are no threads available to complete delegation. Please Try Again later";
        return false;
    }
    catch(int)
    {
        cout << "Some other error occurred when getting data from worker" << &target_worker << ". Please Try Again later.";
        return false;
    }
    return true;
}

Source: Windows Questions C++

LEAVE A COMMENT