hmbdc
simplify-high-performance-messaging-programming
hello-world.cpp

for usage

Template Parameters
CcClientthe concrete Client type
typename... Messages message types that the Client interested if the type of JustBytes is declared, a special callback void handleJustBytesCb(uint16_t tag, uint8_t* bytes) is expected see ConsoleClient.hpp
///
/// A first example to show inter-thread messaging using hmbdc-free lib
///
///to build: do change paths
///g++ hello_world.cpp -g -std=c++1y -Wall -Werror -pthread -Ipath-to-hmbdc-lib-include path-to-hmbdc-lib/libhmbdc.a -lrt -o hw
///
///output:
///-------------------------------------------
///[host]$ ./hw
///anyone saying hello? - waiting...
///no one said hello - this world is going to work for a second...
///no one said hello - this world is going to work for a second...
///no one said hello - this world is going to work for a second...
///someone just said hello - exiting
#include "hmbdc/app/Context.hpp"
#include "hmbdc/app/Client.hpp"
using namespace hmbdc::app;
///
/// user defined messages
/// the tag number starting from 0 - 1000 is reserved for internal usage
///
struct Hello
: hasTag<1001> {
};
///
/// declare Context type - the maximum message size this context can handle is sizeof(Hello)
/// would not compile if size is too small
///
using MyContext = Context<sizeof(Hello)>;
///
/// a Client is like a task or thread of execution
///
struct World
: Client<World, Hello> { ///the Client(World) is interested in Hello message
///
///a callback called once by hmbdc framework before dispatching any messages
///
void messageDispatchingStartedCb(uint16_t) override {
cout << "anyone saying hello? - waiting..." << endl;
}
void invokedCb(uint16_t threadSerialNumber) override {
cout << "no one said hello - this world is going to work for a second..." << endl;
sleep(1); //actually sleeping, but could be doing some work here
}
void handleMessageCb(Hello const&) {
cout << "someone just said hello - exiting" << endl;
throw(0); //any exception in callback will exit the Client
}
};
int main() {
World kingslanding;
MyContext ctx; /// this the communication media object
//starting kingslanding as a client (a thread), pin it on 1st core
ctx.start(kingslanding, 0x01);
sleep(3); /// let kingslanding work for 3 seconds
ctx.send(Hello()); ///say hello now, anyone in ctx (only kingslanding for now) gets it
//wait for kingslanding to exit
ctx.join();
}