Mutex and lots of threads

BlitzMax Forums/BlitzMax Programming/Mutex and lots of threads

appa(Posted 2011) [#1]
This may be something stupid on my behalf but i cant mutex's to work properly, using the threaded example all i did was up the number of loops to 100 and the number of worker threads to 1000 and now it crashes trying to write to the list. However if you comment the list.addlast line, it works fine..

'Make sure to have 'Threaded build' enabled!
'
Strict

'a global list that multiple threads want to modify
Global list:TList=New TList

'a mutex controlling access to the global list
Global listMutex:TMutex=CreateMutex()

Function MyThread:Object( data:Object )

	For Local item=1 To 100
		'simulate 'other' processing...
		Delay Rand( 10,50 )

		'lock mutex so we can safely modify global list
		LockMutex listMutex

		'modify list
		list.AddLast "Thread "+data.ToString()+" added item "+item

		'unlock mutex
		UnlockMutex listMutex
	Next
	
End Function

Local threads:TThread[1000]

'Create worker threads
For Local i=0 Until 1000
	threads[i]=CreateThread( MyThread,String( i+1 ) )
Next

Print "Waiting for worker threads..."

'Wait for worker threads to finish
For Local i=0 Until 10
	WaitThread threads[i]
Next

'Show the resulting list
'
'Note: We don't really have to lock the mutex here, as there are no other threads running.
'Still, it's a good habit to get into.
LockMutex listMutex
For Local t$=EachIn list
	Print t
Next
UnlockMutex listMutex



BlitzSupport(Posted 2011) [#2]
Strange, your example works absolutely fine here, after several runs, though I noticed you only wait for threads 1-10 (fixing this made no difference, though).

The mutex usage looks fine too, although I wonder if that last mutex lock/program exit is occurring while a thread is still running on your system (where it's finished on mine before then), combined with you only waiting for the first 10 threads. See if the fix from 10 to 1000 WaitThreads makes any difference.


BlitzSupport(Posted 2011) [#3]
Hmm, interesting: if I bump the number of threads up to 10,000, I get this error message many times over (even with debug off, which is even more interesting), though it still doesn't crash:

ERROR! bbThreadWait: WaitForSingleObject failed!
LastError=6


I wonder if this was left in deliberately, as I seem to recall a similar problem with the BlitzMax beta, which got sorted out and apparently hasn't turned up since!

Oddly, if I only wait for 10 of those 10,000 threads, I don't get the error message at all. Don't know what it all means, but I might move this to Bug Reports soon, unless anyone else has any explanations... ?

This situation seems vaguely similar:

http://stackoverflow.com/questions/1702172/terminating-a-thread-gracefully-not-using-terminatethread

That's an "invalid handle" error, too, ie. error 6, with a thread being terminated rather than returning.

Here's something else that might be relevant, though the program doesn't appear to be using much memory in Task Manager:

http://blogs.msdn.com/b/oldnewthing/archive/2005/07/29/444912.aspx

Last edited 2011


appa(Posted 2011) [#4]
interesting, i just ran the program with debug mode off and it seems to be working fine now..?

also the error 6 doesn't occur when you only wait for 10 of the threads as opposed to all 1000 of them.