Skip to main content
gamedev.net
🔒 Locked

mutex/criticalsections

Started by Pactuul 1 reply 950+ views
Original Post
Pactuul
Pactuul
ok i've been redesigning my app from the ground using memory leak detection, exceptions, and threads. Everything works properly except threads. I've been testing the threads. I've got a thread class that begins() and ends() threads. i've tested it by making 3 threads and having it print to a file the mem address (to see which thread is printing what) and a static count variable that's in the function. Like so:
  
DWORD Thread::ThreadProc()
{
	static fstream file;
	static count = 0;
	count++;

	if (count == 1)
		file.open("test.txt",ios::out);

	while (d_bIsRunning)
	{
		this->Lock();
		count++;
		file << &(*this) << "   " << count << endl;
		this->Unlock();
	}

	return 0;
}
  
the text file shows that the 3 threads are being called almost at the same time, but I notice it misprints the count number or the memory address in correctly. By that it looks like it was in the middle of executing my file << statement and it switched threads. Well I decide to add mutexs (then I also tried criticalsections) so my thread class is derived from mutex (hence the "this->Lock/Unlock()" calls) And this doesn't seem to fix it either. I'd really like it to execute one pass through the while loop completely without messing up. Anybody have thoughts or suggestions? -Pac [edited by - Pactuul on June 15, 2002 1:23:15 AM]
-Pac "The thing I like about friends in my classes is that they can't access my private members directly." "When listening to some one tell about their problem (whether it's code or not), don't listen to what went right or wrong, but what they assumed....."
felonius
felonius
I am not sure if I udnerstand you application correctly but it seem odd to me that you let your thread class derive from mutex (CCriticalSection I suppose). That way you will have a seperate mutex for each of your threads and you will not lock anything (since each thread has its own lock).

Instead you should create a single global mutex and then lock that. Instead of deriving from CCriticalSection you could let Thread contain a static CCriticalSection and then call Lock()/Unlock() on that one instead.


  

// Thread definition


static CCriticalSection mutex;

...

// In Threadproc()


while (d_bIsRunning)
{
mutex.Lock();
count++;
file << &(*this) << " " << count << endl;
mutex.Unlock();
}


Also, you should encapsulate any place in the app that changes count with a mutex lock/unlock or it wont help much.
Jacob Marner, M.Sc.Console Programmer, Deadline Games
Pactuul
Pactuul
thanks! That actually solved my problem.
I decided to not derive thread from mutex and I just created a single global instance.

Because when I removed the lock/unlock, it still gave the same problem in the text file.

So I guess each thread having a mutex and locking/unlocking it''s own was very counterproductive.

Thanks for the ideas!

-Pac
-Pac "The thing I like about friends in my classes is that they can't access my private members directly." "When listening to some one tell about their problem (whether it's code or not), don't listen to what went right or wrong, but what they assumed....."

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.