Now in c++ there's a new way of doing Callback Mechanism using the async() method.In c++ which has opened up a new realm of methodities and functions in c++ which makes it so much easier for us to do stuff ,like concurrent programming and etc.
Now with this update comes the new async method which allows us to do asynchronous tasks in c++ with no issues.The async() method allows you to execute a function asynchronously and obtain a std:: future object representing the result of the function.In order for this to work we need the <future> template which u can implement using #include <future>.
We are creating an asynchronous task called future task.You will also notice that in the parameters of the function we are passing launch::async and Callee::task.We explicitly specify launch::async to ensure that the task runs asynchronously. However, the default behaviour may vary depending on the standard library implementation.
We are calling the task which it will do asynchronously that the Callee’s task also in the triangular brackets we have defined this futuretask variable to be bool because this is so in order to be up to parr with the task method.
After that we use an if method to check whether the method returns true or not using the get()this method allows us to see the result and if it does return true the callback will finish the task and if not it will continue doing its own labour.
Now i have only covered about the important new async method we have used in here and not the whole callback mechanism in concurrent programming check out these blogs for your necessities:
https://ridittechworld.blogspot.com/2023/12/callback-mechanism.html
/*
* Callback.h
*
* Created on: 18-Mar-2023
* Author: ridit
*/
#ifndef CALLBACK_H_
#define CALLBACK_H_
class Callback {
public:
virtual void onStartTask() = 0;
virtual void onFinishTask() = 0;
};
#endif /* CALLBACK_H_ */
/*
* Callee.h
*
* Created on: 18-Mar-2023
* Author: ridit
*/
#ifndef CALLEE_H_
#define CALLEE_H_
#include <iostream>
#include <future>
#include <thread>
#include "Callback.h"
using namespace std;
class Callee {
private:
Callback* cb;
public:
Callee(Callback* cb){
this->cb = cb;
}
virtual ~Callee(){
}
static bool task(){
cout<<"Background thread id = " <<this_thread::get_id()<<endl;
for(int i = 0;i<10;i++){
cout<<"Callee is doing the background task"<<endl;
}
}
void doBackgroundTask(){
this->cb->onStartTask();
future<bool> futureTask = async(launch::async, Callee::task);
if(futureTask.get() == true){
this->cb->onFinishTask();
}
}
};
#endif /* CALLEE_H_ */
/*
* Caller.h
*
* Created on: 18-Mar-2023
* Author: ridit
*/
#ifndef CALLER_H_
#define CALLER_H_
#include <iostream>
#include "Callee.h"
using namespace std;
class Callee;
class Caller : public Callback{
private:
Callee* callee;
public:
Caller(){
callee = new Callee(this);
}
void doOwnTask(){
for(int i = 0;i<10;i++){
cout<<"Caller is doing its own task"<<endl;
}
}
void delegateTaskToCallee(){
callee->doBackgroundTask();
}
virtual ~Caller(){
}
void onStartTask(){
cout<<"The background task is starting"<<endl;
}
void onFinishTask(){
cout<<"The background task is finished thank you for taking my burden,Callee"<<endl;
}
};
#endif /* CALLER_H_ */
//============================================================================
// Name : Callback1.cpp
// Author : Ridit
// Version :
// Copyright : @Ridit...
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include "Callback.h"
#include "Caller.h"
#include "Callee.h"
using namespace std;
int main() {
Caller* caller = new Caller();
caller->doOwnTask();
caller->delegateTaskToCallee();
return 0;
}