Overview
Features
Download
Documentation
Community
Add-Ons & Services

Poco Threads fail mutex locking test?

Please post support and help requests here.

Poco Threads fail mutex locking test?

Postby SyRenity » 12 Feb 2007, 23:51

UPDATE: I was pointed to a bug in my code, it seems the problem was at my end (:redface:).

Please close or delete this topic.

(Because the forum software breaks the code, I cross-posted this thread to the mailing list.)

Hi.

I'm evaluating Poco for use in our projects, especially for threads handling. It seems that Poco threads/mutex'es fail a very simple test of mutex locking. The test code is below, any ideas?

Thanks.

^
#include
using namespace std;

#include
#include
#include
using namespace Poco;


static int volatile threadCounter = 0;

static int volatile tangledThreads = 0;

static int volatile numOfThreads = 0;
static int volatile bStop = false;


class TestMutex : public Mutex
{
public:
void lockedFunc()
{
lock();
threadCounter++;

if(threadCounter != 1)
{
cout << "Mutex failed!!!" << endl;
tangledThreads++;

threadCounter = 1;
}

threadCounter--;
unlock();
}
};

class Runner : public Runnable
{
TestMutex *tMutex;
public:
Runner()
{
numOfThreads++;
tMutex = new TestMutex();
}

~Runner()
{
delete tMutex;
}

void run()
{
do
{
tMutex->lockedFunc();
Thread::sleep(33);
}
while(!bStop);

numOfThreads--;
}
};


//EDIT the number of concurrent threads
#define NUM_THREADS 300


int main (int argc, char *argv[])
{
Thread tThreads[NUM_THREADS];
Runner rThreads[NUM_THREADS];

cout << "Launching " << NUM_THREADS << " threads" << endl;

for(int i=0;i tThreads[i].start(rThreads[i]);

cout << NUM_THREADS << " threads are running" << endl;

cin.get();

bStop = true;
cout << "Stopping " << NUM_THREADS << " threads" << endl;

while(numOfThreads > 0)
usleep(33000);

cout << "Stopped" << endl;

if(tangledThreads > 0)
cout << tangledThreads << " tangled threads!" << endl;

return(0);
}
^
SyRenity
 
Posts: 1
Joined: 31 Aug 2006, 16:06

Re: Poco Threads fail mutex locking test?

Postby alex » 13 Feb 2007, 15:04

I've noticed that you consistently use postfix (i++, i--) in the code. Prefix (++i, --i) is faster and usually preferred increment/decrement method in cases when you do not need the previous value.

Alex
alex
 
Posts: 1145
Joined: 11 Jul 2006, 16:27
Location: United_States


Return to Support

Who is online

Users browsing this forum: No registered users and 1 guest

cron