The main problem is, like the comment says, that there is a statically allocated buffer. If two threads would work synchronously on that buffer, you would most likely get race conditions, so you have to avoid that.
That means either fixing FCGX_GetParam
(which I doubt will be a good idea since it is a third party library) or synchronizing the access to it:
//some common mutex
std::mutex mtx;
std::string getRequestContent(const FCGX_Request &request)
{
std::string content_length_str;
{
lock(mtx); //guard every action on the static buffer with this lock
char *content_length_cptr = FCGX_GetParam("CONTENT_LENGTH", request.envp);
content_length_str = content_length_cptr; //copy the content of the buffer
} //unlock the mutex, you dont work on the buffer hence forth
unsigned long content_length = 0;
if (!content_length_str.empty()) try {
content_length = boost::lexical_cast<unsigned long>(content_length_str);
if (content_length > STDIN_MAX)
{
content_length = STDIN_MAX;
}
}
catch(boost::bad_lexical_cast const&)
{
std::cerr << "Can't Parse 'CONTENT_LENGTH='"
<< content_length_str
<< "'. Consuming stdin up to " << STDIN_MAX << "\n";
content_length = STDIN_MAX;
}
// the rest as it was...
}