Signals & multithreading
[libplayerc++]
Along with providing access to the basic C functions of
libplayerc in a C++ fashion, libplayerc++ also provides additional functionality along the lines of signaling and multithreading. The multithreaded ability of libplayerc++ allieves the developer from having to worry about allotting time to handle messaging. It also allows for the PlayerClient to act as a messaging loop for event driven programs. The signaling and multithreading ability of libplayerc++ is built from the
Boost c++ libraries. This is relevant because we will be using boost semantincs for connecting the signals to the client. Much of this functionality can best be illustrated through the use of an example:
#include <iostream>
#include <boost/signal.hpp>
#include <boost/bind.hpp>
#include <libplayerc++/playerc++.h>
#include "args.h"
void cb1()
{ std::cout << "cb1" << std::endl; }
void cb2(uint &aI)
{ std::cout << "cb2 " << ++aI << std::endl; }
class TestCb
{
int mId;
public:
TestCb(int aId) : mId(aId) {};
void Cb()
{ std::cout << "TestCb " << mId << std::endl; }
void Cb(uint aOpt)
{ std::cout << "TestCb " << mId << " " << aOpt << std::endl; }
};
void stop_cb(PlayerCc::PlayerClient* c, uint &i)
{
if (++i>10)
{
std::cout << "stop: " << i << std::endl;
c->Stop();
}
}
int main(int argc, char** argv)
{
parse_args(argc, argv);
try
{
PlayerCc::PlayerClient client(gHostname, gPort);
PlayerCc::CameraProxy cp(&client, 0);
PlayerCc::ClientProxy::connection_t conn;
conn = cp.ConnectReadSignal(&cb1);
uint count = 0;
cp.ConnectReadSignal(boost::bind(&cb2, count));
TestCb test1(1), test2(2);
cp.ConnectReadSignal(boost::bind(&TestCb::Cb, boost::ref(test1)));
cp.ConnectReadSignal(boost::bind(&TestCb::Cb, boost::ref(test2), 1));
std::cout << "Read()" << std::endl;
for (uint i=0; i<10; ++i)
{
client.Read();
if (4==i)
cp.DisconnectReadSignal(conn);
}
std::cout << "Run()" << std::endl;
uint i = 0;
conn = cp.ConnectReadSignal(boost::bind(&stop_cb, &client, i));
client.Run();
std::cout << "StartThread()" << std::endl;
cp.DisconnectReadSignal(conn);
client.StartThread();
timespec sleep = {5, 0};
nanosleep(&sleep, NULL);
for (uint j=0; j<10; ++j)
{
cp.SaveFrame("test");
std::cout << cp << std::endl;
}
client.StopThread();
std::cout << "finished!" << std::endl;
}
catch (PlayerCc::PlayerError e)
{
std::cerr << e << std::endl;
return -1;
}
return 1;
}
|
Last updated 12 September 2005 21:38:45
|