|
楼主 |
发表于 2011-9-18 22:34:35
|
显示全部楼层
我是这样进行线程保护的:
-
- WaitForSingleObject(g_pSocketManager->g_hInputQueueMutex,INFINITE);
- x = msgEntityMotion->fLocationX;
- y = msgEntityMotion->fLocationY;
- z = msgEntityMotion->fLocationZ;
- Azimuth = osg::DegreesToRadians(msgEntityMotion->fAzimuthAng);
- Pitch = osg::DegreesToRadians(msgEntityMotion->fPitchAng);
- Roll = osg::DegreesToRadians(msgEntityMotion->fRollAng);
- ReleaseMutex(g_pSocketManager->g_hInputQueueMutex);
复制代码
以上代码是在节点回调中取数据,其中g_pSocketManager、msgEntityMotion都是在IntegratedDisplaySystem.cpp(MFC工程名为:IntegratedDisplaySystem)中定义的全局变量。
- CSocketManager *g_pSocketManager=NULL;
复制代码
CSocketManager用于管理网络通信,将网络接收的数据写到msgEntityMotion全局变量中:
-
- class CSocketManager
- {
- public:
- CSocketManager();
- virtual ~CSocketManager();
- private:
- BOOL m_bComPro;
- CTCPClient m_TCPClient;
- HANDLE m_hDataProExitFlag;
- HANDLE m_hDataSendExitFlag;
- public:
- HANDLE g_hInputQueueMutex;
- private:
- static UINT WINAPI ThreadProcessComData(void *ptr);
- void StopThread();
- void StartThread();
-
- private:
- int ProcessMsgEntityMotion(SMsgEntityMotion MsgEntityMotion);
-
- public:
- bool InitTCPSocket(char *pszClientIPAddr,char *pszServerIPAddr,u_short nServerPort,u_short nLocalPort);
- };
复制代码
在CSocketManager:rocessMsgEntityMotion(SMsgEntityMotion MsgEntityMotion)中:
- ::WaitForSingleObject(g_hInputQueueMutex,INFINITE);
- ……(向msgEntityMotion中写数据)……
- ReleaseMutex(g_hInputQueueMutex);
复制代码 |
|