Hi and thanks for reading,
I am practicing C++ to further get better at writing multi-threaded capable programs.. specifically I am now attempting to solve the problem of using a 3rd party API's set of classes and functions...
Here is the code
helloworld.cpp
1 2 3 4 5 6 7 8 9
|
void requestSomeVectorOfData(std::vector<DataPayload> & _return, const std::vector<DataType> & requested_datas, const std::string& request_type) {
APICOMMAND command_type = ST_DATAREQUEST;
if(single_test_client.isConnected()) {
std::cout << "Requesting... multiple datas\n";
single_test_client.processMessages(_return, command_type, requested_datas);
}
}
|
this Vector of requests gets pushed down into an area where it's understood.. we want to retrieve 100's or even 100's of thousands of these `DataPayloads`.. and the API seems to have a async function call with callback approach.
My Question is... what is perhaps a good simple way to approach using multi-threading to tackle these calls in the quickest thread-safe manner?
So far my psuedo code approach sounds like this
TestCppClient.cpp implements 3rd party functionality but you write it yourself so to speak...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
/*
call this function once connection is established
*/
void TestCppClient::processMessages(std::vector<DataPayload> & _return, APICommand request_type, const std::vector<DataType> & payload)
{
time_t now = time(NULL);
printf("\tTestCppClient: processMessages(): M_STATE: %d , request_type: %d !\n" , m_state, request_type);
switch (m_state) {
case ST_CONNECT:
printf("\tTestCppClient: processMessages(): ST_CONNECT!\n");
break;
case ST_DATAREQUEST:
printf("\tTestCppClient: processMessages(): ST_DATAREQUEST!\n");
getalldatas(_return, payload);
break;
case ST_DATAREQUEST_ACK:
printf("\tTestCppClient: processMessages(): ST_DATAREQUEST_ACK\n");
break;
}
m_osSignal.waitForSignal();
errno = 0;
m_pReader->processMsgs();
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
void TestCppClient::getalldatas(std::vector<DataPayload> & _return, const std::vector<DataType> & req_load)
{
// use multi-threading to instantiate a threadpool which has local queues.. or work stealing queues
// feed each thread I have ( I have 12 right now on my machine in total to work with) an equal amount of "work"
//. in each thread.. execute this request via the 3rd party client
//. m_pClient->reqFundamentalData(request_id, some_single_request_load, report_type_str);
// finish only when all commands are through?
//m_state = ST_DATAREQUEST;
}
|
the callback where my requested data will "comeback"
1 2 3 4 5
|
void TestCppClient::yourReturnedData(RequestId reqId, const std::string& data) {
printf("\tTestCppClient: yourReturnedData!!!! ReqId: %ld, strlen() = %zu\n", reqId, strlen(data.c_str()));
}
// this data somehow needs to be appended into _return back at the top function call
|
my problem is.. how would I detect at a high level if the entire API is blocking my requests and I need to throttle myself?
Perhaps A better approach is write futures?? and bind the returning future.. to when I see the callback with the proper
request_id
.. and if so append that to my
_return
at the
requestSomeVectorOfData()
level?
there must be a simpler way but.. I am quite newb here and still grappling with basics.. but I can write some basic threadpools that can split up work and steal work from each other.. what I am seeing is, if I maybe write my job in the form of a function that returns a future... and wrap the async callback.. I can do some elegant/clean/understandable solutions here... but would love to hear others thoughts how they approach async code when wanting multi-threaded performance
Is it a bother to use async package but get multithreaded performance that's reliable/tunable?
edit: seems this article gives me a good starting point.. implement a use of futures.. but still have a threadpool with stealable work queues.. and perhaps in the future returning function I have a retry mechanism?
source:
https://www.spiria.com/en/blog/desktop-software/solving-the-problems-with-the-futures/