Cocos2dx C ++ custom thread Security Message Management, cocos2dx thread

Source: Internet
Author: User

Cocos2dx C ++ custom thread Security Message Management, cocos2dx thread
I. background

Since the icationicationcenter of cocos2dx does not implement thread-safe processing, when we use multithreading in cocos2dx for message synchronization,

Problems may occur. To solve this problem, we only need to lock the thread safely.

In order not to modify the source code, I imitated icationcenter center and customized A Message Management.

Ii. First of all, we define the data type Message of a Message body. It stores the message Name, message processing function, message target, and message content.
Then, we define a message management class. It contains a message container. To accommodate each message. The management class only adds and sends messages to external exposed interfaces. The specific content must be implemented by yourself. Finally, lock each operation of the message container. A reusable multi-line message processing mechanism is complete.
Iii. Code header file:
/************************************************************** * Copyright (c) 2014-11-12 by real.Xm   * Blog Address:http://blog.csdn.net/q229827701 * Email: 229827701@qq.com **************************************************************/#ifndef __XMESSAGE__H#define __XMESSAGE__H#include "cocos2d.h"class XMessage;class CC_DLL MessageMsg : public cocos2d::Ref{public:MessageMsg();virtual ~MessageMsg();static MessageMsg* getInstance();bool addObserver(const std::string &msgName,cocos2d::Ref* target,cocos2d::SEL_CallFuncO selector,cocos2d::Ref* msgContent = nullptr);bool postMessage(const std::string &msgName,cocos2d::Ref* msgContent);bool removeObserverByName(const std::string &msgName,cocos2d::Ref* target = nullptr);bool removeAllObservers(cocos2d::Ref* target);protected: XMessage* getMessageByName(const std::string &msgName) const;private://message containercocos2d::Vector<XMessage*> _msgContainer; };class CC_DLL XMessage: public cocos2d::Ref{public:XMessage(const std::string &msgName,cocos2d::Ref* target,cocos2d::SEL_CallFuncO selector,cocos2d::Ref* msgContent);~XMessage();void handerMessage(cocos2d::Ref* msgContent);CC_SYNTHESIZE_READONLY(std::string,_msgName,MessageName);//message nameCC_SYNTHESIZE_READONLY(cocos2d::Ref*,_target,Target);   //targetCC_SYNTHESIZE_READONLY(cocos2d::SEL_CallFuncO,_selector,Selector);//functionCC_SYNTHESIZE_READONLY(cocos2d::Ref*,_msgContent,MessageConent);//function args};#endif // !_XMESSAGE_H

Implementation File
#include "XMessageManger.h"std::mutex _ContainerMutex;static MessageMsg* _Manager = nullptr;MessageMsg::MessageMsg(){}MessageMsg::~MessageMsg(){_msgContainer.clear();}MessageMsg* MessageMsg::getInstance(){if (!_Manager){_Manager = new MessageMsg;}return _Manager;}bool MessageMsg::addObserver( const std::string &msgName,cocos2d::Ref* target,cocos2d::SEL_CallFuncO selector,cocos2d::Ref* msgContent /*= nullptr*/ ){if (!getMessageByName(msgName)){auto msg = new XMessage(msgName,target,selector,msgContent);IF_NULL_RETURN_FALSE(msg);msg->autorelease();std::lock_guard<std::mutex> ul(_ContainerMutex);_msgContainer.pushBack(msg);return true;}return false;}bool MessageMsg::postMessage( const std::string &msgName,cocos2d::Ref* msgContent ){auto msg = getMessageByName(msgName);if (msg){msg->handerMessage(msgContent);return true;}return false;}XMessage* MessageMsg::getMessageByName( const std::string &msgName ) const{std::lock_guard<std::mutex> ul(_ContainerMutex);for (auto &msg : _msgContainer){if (msgName == msg->getMessageName()){return msg;}}return nullptr;}bool MessageMsg::removeObserverByName( const std::string &msgName,cocos2d::Ref* target /*= nullptr*/ ){std::lock_guard<std::mutex> ul(_ContainerMutex);for (auto &msg : _msgContainer){if (msgName == msg->getMessageName()&&(target == msg->getTarget()||!target)){_msgContainer.eraseObject(msg,true);return true;}}return false;}bool MessageMsg::removeAllObservers( cocos2d::Ref* target ){std::lock_guard<std::mutex> ul(_ContainerMutex);for (auto &msg : _msgContainer){if (target == msg->getTarget()){_msgContainer.eraseObject(msg,true);return true;}}return false;}/////////////////////////////////////////////////////////////////////////////   XMessage Class/////////////////////////////////////////////////////////////////////////XMessage::XMessage( const std::string &msgName,cocos2d::Ref* target,   cocos2d::SEL_CallFuncO selector,cocos2d::Ref* msgContent )   :_msgName(msgName),_target(target),_selector(selector),_msgContent(msgContent){}XMessage::~XMessage(){}void XMessage::handerMessage( cocos2d::Ref* msgContent ){if (_target){if (msgContent) {(_target->*_selector)(msgContent);} else {(_target->*_selector)(msgContent);}}}




4. declare that this article is original. In order to respect originality, please indicate the source when reprinting. Http://blog.csdn.net/q229827701/article/details/41042147

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.