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 ).
-