Introduction
The Future Library is designed to handle asynchronous tasks in C. It provides a framework for creating, managing, and executing tasks asynchronously. this library is example of raylist library because future library depends on raylist
Usage
// include libs
#include < stdio.h >
#include < stdarg.h >
#define LIST_C
#include "../../raylist.h"
#define FUTURE_C
#include "future.h"
// define your data
typedef struct{
int a;
}Example;
// and implement poll method
// method should return HandlFuture structure
// HandlFuture{
// bool isfinished;
// bool iserror;
// void* return_data;
//}
// and should accept void* in parameter return Future from other task will pushed to parameter
// because this is channel
Handle my_poll(void* task){
// do some work
// if not finish
if(true)
return (HandlFuture) {
.isfinished = false,
.iserror = false,
.return_data = data // your data
};
return (HandlFuture) {
.isfinished = true, // task done
.iserror = false,
.return_data = NULL
};
}
int main(void)
{
// allocate your data
// init your Future with FutureNewTask
Future* tasks[2] = {
FutureNewTask(my_poll , my_data1);
FutureNewTask(my_poll , my_data2);
};
// add your tasks to event loop
FutureAddTasks(task,5);
// run your tasks
FutureLoop(callback);
// NOTE: callback function return void* and accept void* , int
// you can apply changes to data and any task you want ... for logging or changing something
}
Example
task 1
task 2
task 3
task 4
#include < stdio.h >
#include < stdarg.h >
#define LIST_C
#include "../../raylist.h"
#define FUTURE_C
#include "future.h"
typedef struct {
int count;
int max_count;
} CounterData;
// implementation of abstract method poll
// NOTE : poll function should return HandlFuture structure
HandlFuture task_poll(void* task) {
// getting the data from task
CounterData* data = (CounterData*)((Future*)task)->data;
// check if count is less than count_max
if (data->count < data->max_count) {
// do work (count++)
data->count++;
// return NULL (function is not finished yet)
return (HandlFuture){
.isfinished = false,
.iserror = false,
.return_data = (void*)&data->count
};
}
// free allocated data memory
free(data);
// return task (task is not NULL so function is done)
return (HandlFuture){
.isfinished = true,
.iserror = false,
.return_data = NULL
};
}
int main(void)
{
// allocate the data
CounterData* data1 = malloc(sizeof(CounterData));
data1->count = 0;
data1->max_count = 100;
CounterData* data2 = malloc(sizeof(CounterData));
data2->count = 200;
data2->max_count = 400;
CounterData* data3 = malloc(sizeof(CounterData));
data3->count = 1000;
data3->max_count = 1200;
CounterData* data4 = malloc(sizeof(CounterData));
data4->count = 10000;
data4->max_count = 12000;
// allocate 5 tasks
Future* task[5];
task[0] = FutureNewTask(task_poll,data1);
task[1] = FutureNewTask(task_poll,data2);
task[2] = FutureNewTask(task_poll,data3);
task[3] = FutureNewTask(task_poll,data4);
FutureAddTasks(task,4);
// logging callback
FutureLoop(lambda(void* , (void* data , int task) , {
// if the function executed in task 3
if(task == 2){
if(*(int*)data >= 1000)
*(int*)data += 5;
}
printf("Debug :: %d\n" , *(int*)data);
return data;
}));
}