MOOS学习笔记——多线程

Posted 静世孔明

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MOOS学习笔记——多线程相关的知识,希望对你有一定的参考价值。

/*
* A simple example showing how to use a comms client
*/

#include "MOOS/libMOOS/Comms/MOOSAsyncCommClient.h"
#include "MOOS/libMOOS/Utils/CommandLineParser.h"
#include "MOOS/libMOOS/Utils/ConsoleColours.h"
#include "MOOS/libMOOS/Utils/ThreadPrint.h"

MOOS::ThreadPrint gPrinter ( std : : cout ) ;
bool OnConnect(void ?pParam) {
    CMOOSCommClient ?pC = reinterpret_ cast<CMOOSCommClient?>(pParam) ;
    pC?>Register ( "X" , 0.0 ) ;
    pC?>Register ( "Y" , 0.0 ) ;
    pC?>Register ( "Z" , 0.0 ) ;
    retu rn true ;
}
bool OnMail (void ?pParam){
CMOOSCommClient *pC = reinterpret _ cast<CMOOSCommClient?>(pParam );
MOOSMSG_LIST M ; // get the mail
pC?>Fetch (M);
MOOSMSG_LIST : : iterator q ; // p r o c e s s i t
for ( q=M.begin () ; q!=M.end () ; q++){
gPrinter . SimplyPrintTimeAndMessage ( " mail : "+q?>GetSource ( ) , MOOS:: -
ThreadPrint : : GREEN ) ;
}
r etu rn t ru e ;
}
bool funcX ( CMOOSMsg & M , void ? TheParameterYouSaidtoPassOnToCallback )
{
gPrinter . SimplyPrintTimeAndMessage ( " c a l l back f o r X" , MOOS : : ThreadPrint : : -
CYAN ) ;
r etu rn t ru e ;
}
bool funcY ( CMOOSMsg & M , void ? TheParameterYouSaidtoPassOnToCallback )
{
gPrinter . SimplyPrintTimeAndMessage ( " c a l l back f o r Y" , MOOS : : ThreadPrint : : -
MAGENTA ) ;
r etu rn t ru e ;
}
i n t main ( int argc , char ? argv [] ) {
// unde r stand the command l i n e
MOOS : : CommandLineParser P ( argc , argv ) ;
std : : string db_host=" l o c a l h o s t " ;
P . GetVariable ( "??moos_host" , db_host ) ;
i n t db_port =9000;
P . GetVariable ( "??moos_port" , db_port ) ;
std : : string my_name =" ex40 " ;
P . GetVariable ( "??moos_name" , my_name ) ;
// c o n f i g u r e the comms
MOOS : : MOOSAsyncCommClient Comms ;
Comms . SetOnMailCallBack ( OnMail ,&Comms ) ;
Comms . SetOnConnectCallBack ( OnConnect ,&Comms ) ;
// here we add per message c a l l b a c k s
// fi r s t parameter i s the channel nick?name , then the f u n c t i o n
// to c a l l , then a parameter we want passed when c a l l b a c k i s
// invoked
Comms.AddMessageCallback ( " callback_X " , "X" , funcX , NULL ) ;
Comms.AddMessageCallback ( " callback_Y " , "Y" , funcY , NULL ) ;
// start the comms running
Comms . Run ( db_host , db_port , my_name ) ;
// f o r ever loop sending data
std : : vector<unsigned char> X (1000) ;
f o r ( ; ; ) {
MOOSPause (10) ;
Comms.Notify ( "X",X) ; // for callback_X
Comms.Notify ( "Y","This is Y"); // f o r callback_Y
Comms.Notify ( "Z",7.0); //no callb a c k
}
return 0 ;
}

以上是关于MOOS学习笔记——多线程的主要内容,如果未能解决你的问题,请参考以下文章

MOOS学习笔记1——HelloWorld

MOOS学习笔记3——命令行

MOOS学习笔记2——HelloWorld回调

线程学习知识点总结

多线程编程学习笔记——线程同步

Java多线程学习笔记