anyKode Marilou
ContentsIndexHome
PreviousUpNext
Connection::Lock

[DEBUG] Lock the connection.

C++
void Lock(void);

Lock allows to lock (EnterCriticalSection) the connection and protect it from multiple access (multi-threading). This function must be used jointly with the Unlock function. Functions Connect and Disconnect lock and unlock the Connection object. 

 

This function is used for debugging purpose and to find synchronisation problems in your application. For exemple an application that try to read/write devices data from a thread whereas another thread is disconnecting ...

//This C++ sample shows how to use Lock/Unlock
//These functions are provided for DEBUG ONLY
//Your application must take care about multiple threads access itself
//Lock/Unlock connection consumes time that is not needed if application is well synchronized

#include "Modacpp.h"
#include "conio.h"

#define MODASERVER    "localhost"
#define ROBOTNAME    "/robot1"
#define SENSORNAME    "ray1/sensor"

using namespace ModaCPP;


//Global variables
Connection *pConnection=NULL;
RobotPHX *pRobot=NULL;
DeviceDistance *pDistanceDevice=NULL;
HANDLE hThread=NULL;

//-------------------------------------------------------------------
void ReadThread(void)
{
bool bError=false;
while(!bError)
    {
    //Lock connection to disalow multiple access
    pConnection->Lock();
    if(pConnection->IsConnected())
        {
        float measure=pDistanceDevice->GetMeasure();
        _cprintf("measure: %f\r\n",measure);
        //sleep 1000 simulated ms
        pConnection->Sleep(1000);
        //Then unlock
        pConnection->Unlock();
        }
    else
        {
        bError=true;
        pConnection->Unlock();
        }
    }
//End
}

//-------------------------------------------------------------------
void main(void)
{
bool bError=false;

//Connect to MODA server
pConnection=new ModaCPP::Connection(true);
if(pConnection->Connect(MODASERVER))
    {
    //Find robot and sensor
    pRobot=pConnection->QueryRobotPHX(ROBOTNAME);
    if(pRobot)
        {
        pDistanceDevice=pRobot->QueryDeviceDistance(SENSORNAME);
        if(pDistanceDevice)
            {
            //Run the Read thread
            DWORD dwID;
            hThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ReadThread,0,0,&dwID);

            //the loop ....
            _cprintf("Connected : press anykey to stop\r\n");
            while(!_kbhit())
                {
                //Real system time wait
                Sleep(1000);

                pConnection->Lock();
                if(pConnection->IsConnected())    _cprintf("Connected ...\r\n");
                else                            _cprintf("Not connected ...\r\n");
                pConnection->Unlock();
                }
            }
        }

    //Disconnecting : Disconnect Lock/Unlock the object to protect itself
    _cprintf("Disconnecting\r\n");
    pConnection->Disconnect();

    if(hThread)
        {
        //wait for the read thread end
        WaitForSingleObject(hThread,INFINITE);
        CloseHandle(hThread);
        }
    }

//delete objects
if(pConnection) delete pConnection;
if(pRobot) delete pRobot;
if(pDistanceDevice) delete pDistanceDevice;
}
Documentation v4.7 (18/01/2015), Copyright (c) 2015 anyKode. All rights reserved.
What do you think about this topic? Send feedback!