anyKode Marilou
|
[DEBUG] Verrouille la connexion.
void Lock(void);
Lock permet de verrouiller (EnterCriticalSection) la connexion et la protège contre des accès concurrents (multi-threading). Cette fonction doit être utilisée conjointement à la fonction Unlock. Les méthodes Connect et Disconnect verrouillent la connexion.
Cette fonction est utilisée pour mettre en évidence par exemple des problèmes de connections/déconnexion liés à une application qui fonctionne avec plusieurs threads. Cette situation indique que la synchronisation des threads est à améliorer : Le thread d'acquisition des données doit être stoppé avant que la déconnexion démarre.
//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!
|