I have this test program:
-------------------------------------------
#include <iostream>
#include <vector>
#include
class TestThread
{
size_t m_loopCount;
int m_serverPort;
public:
TestThread (size_t loopCount)
: m_loopCount (loopCount > 0 ? loopCount : 1)
, m_serverPort (9090)
{
return;
}
void operator() (void)
{
for (size_t loopCount = 0; loopCount < m_loopCount; ++loopCount)
{
// Do the same thing the socket open code does
char port[6];
sprintf(port, "%d", m_serverPort);
char buf0[512];
sprintf (buf0, "%s %zd", "Our loop cont is: " , loopCount);
char buf1[793];
sprintf (buf1, "Now the loop count is: %zd", loopCount);
// boost::this_thread::yield();
}
}
};
int main (int argc, char * argv[])
{
unsigned int threadLoopCount = 20000;
unsigned int threadCount = 2500;
// Thread storage
std::vector threadVec;
for (size_t i = 0; i < 1; ++i)
{
std::cout << i << ": Spawn " << threadCount << " threads with
loop count = " << threadLoopCount << std::endl;
for (size_t i = 0; i < threadCount; ++i)
{
TestThread theThread(threadLoopCount);
threadVec.push_back (new boost::thread (theThread));
}
while (false) <<<-----------------------------------------This
block of code disabled
{
threadVec.at(0)->join();
delete threadVec.at(0);
threadVec.erase(threadVec.begin());
TestThread theThread(threadLoopCount);
threadVec.push_back (new boost::thread (theThread));
}
for (size_t i = 0; i < threadVec.size(); ++i)
{
threadVec.at(i)->join(); <<<<-----------------------------Error
location 1
delete threadVec.at(i); <<<<-----------------------------Error
location 2
}
threadVec.clear();
std::cout << "Done" << std::endl;
}
}
-------------------------------------------
I haven't edited it to remove disabled code. Note the while(false)
which disables the block of code that was starting a new thread as
soon as one completed. What the thread code actually does is designed
to reveal a different problem. The for loop that only executes once
was a failed attempt to make the desired error happen more often.
If I run this program enough times in the Xcode debugger, I eventually
see the error:
./boost/thread/pthread/mutex.hpp:50: failed assertion `!
pthread_mutex_lock(&m)'
In the latest case, this happened at the line I have labeled as "Error
location 1", but I have also seen this at the line labeled as "Error
location 2".
I have seen other threads regarding similar errors with boost threads,
but nothing says that this was a bug that has been fixed, or that
there is something I should do to avoid the error. I'm also wondering
if I have misunderstood something about how to use the threads code.
I am running Boost 1.36.0 on OSX 10.5.6 with Xcode 3.1.2.
Does this look like pilot error, or is it a bug?
- Rush