Jul 2, 2014 at 1:17am UTC
Hello,
Here is what I've got so far:
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
// build:
// clang++ -g -std=c++11 -Wall main.cpp -o main
// main.cpp
#include <iostream>
#include <thread>
#include <vector>
void pt_ssleep(const int delay);
class ioes
{
public :
virtual void on_data(int i) = 0;
virtual void from_oes(int i) = 0;
};
class oes : public ioes
{
public :
oes() {}
void on_data(int i) override;
void from_oes(int i) override { }
};
void oes::on_data(int i)
{
std::cout << i << std::endl;
}
class oms
{
public :
oms() = delete ;
oms(ioes& param) : ioesobj(param) {}
void send_data(int i);
private :
ioes& ioesobj;
std::vector<int > numbers;
};
void oms::send_data(int i)
{
numbers.push_back(i);
auto j = rand();
ioesobj.on_data(j);
}
int main()
{
std::cout << " --- start prototype --- " << std::endl;
oes oesobj;
oms omsobj(oesobj);
for (int i=0; ; ++i)
{
omsobj.send_data(i);
pt_ssleep(1);
}
std::cout << " --- end prototype --- " << std::endl;
return 0;
}
void pt_ssleep(const int delay)
{
std::this_thread::sleep_for(std::chrono::seconds(delay));
}
I need oes object to send some data back to oms object asynchronously and to insert those data into numbers container.
Can someone help me with this. Thank you.
Last edited on Jul 2, 2014 at 10:28pm UTC
Jul 2, 2014 at 10:30pm UTC
Thanks JLBorges, it looks like I should start reading on C++11 threading.
I should have been more clear on what I mean by "asynchronous" but when I call oms::send_data() I do not need to get the return value from that function (those data are actually being sent into a socket).
I updated my original post and example. oms object updates it's numbers container and sends data to oes. There should be a mechanism for oes to send data back to oms and insert those data into numbers container.
Can you help with this one -- thank you.
Jul 3, 2014 at 4:06am UTC
Thanks a lot! I sure need the reading to understand the code and adapt it.