Thread security bug when openrtmfp/Cumulus primer (19) uses cumuluslib independently

Source: Internet
Author: User

Thread security bug when openrtmfp/Cumulus primer (19) uses cumuluslib independently
  • Author: Liu Da-poechant (Zhong Chao)
  • Email: zhongchao. USTC # gmail.com (#-> @)
  • Blog: blog.csdn.net/poechant
  • Date: June 7Nd, 2012

Openrtmfp/Cumulus provides cumuluslib for other rtmfp applications, not limited to cumulusserver.

Generally, thread a prepares the message to be pushed, and then thread a pushes the message to the message queue.

However, in cumuluslib, thread a pushes a message to the message queue, and then enters a field in the message according to the pointer of the message in the queue. And expect the following:

Because in cumulusserver, a client is operated only in one thread, the corresponding flowwriter will not have cross-thread problems. However, if cumuluslib is used separately, if thread communication occurs and flowwriter is shared, the message queue will be shared, which may happen at this time.

This leads to a very serious error and causes the process to crash. The correction method can be to put the message into the queue after it is fully prepared, as follows:

/* * author:  michael * date:    June 6th, 2012 * type:    add */MessageBuffered* FlowWriter::createAMFMessage(const std::string& name)    // signature.empty() means that we are on the flowWriter of FlowNull    if (!(_closed || signature.empty() || _band.failed())) {        MessageBuffered* pMessage = new MessageBuffered();        MessageBuffered& message(*pMessage);        writeResponseHeader(message.rawWriter,name,0);        return pMessage;    }    MessageBuffered& message(_MessageNull);    writeResponseHeader(message.rawWriter,name,0);    return NULL;}

Then, the push operation is added at the end of the call:

/* * author:  michael * date:    June 6th, 2012 * type:    add */void FlowWriter::pushAMFMessage(MessageBuffered* pMessage) {    if (pMessage != NULL) {        _messages.push_back(pMessage);    }}

In this way, the message data is written to the queue after being written, as shown below:

However, if thread security is considered, multiple threads need to lock the operation on the same message queue:

/* * author:  michael * date:    June 6th, 2012 * type:    add */void FlowWriter::pushAMFMessage(MessageBuffered* pMessage) {    if (pMessage != NULL) {        Poco::Mutex::ScopedLock lock(msgQueueMutex);        _messages.push_back(pMessage);    }}

In this way, the thread security problem is basically solved.

In addition, to use cumuluslib, follow the GPL protocol. Do not forget it.

-

For more information, see the csdn blog blog.csdn.net/poechant from LIU Da poechant (zhongchao ).

-

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.