multithreading - .NET - Block main thread until there are any available threads -
i have process main thread reading file , splitting parts. parts require further processing. utilize available threads downstream processing utilizing cpu (or many cores) possible. don't want create excessive backlog main thread, need main thread wait add queue until there available thread.
i see many articles vb.net 4.0: looking execute multiple threads, wait until threads completed before resuming, waiting all threads complete, whereas need any threads available
is can tackle task parallel library, or should manually creating threads , monitoring threadpool?
using reader new streamreader(filename) currentblocksize = reader.readblock(currentbuffer, 0, buffersize) runningbuffer &= new string(currentbuffer) if runningbuffer.contains(rowdelimiter) lineparts = runningbuffer.split(rowdelimiter) integer = 0 lineparts.count - 1 if < lineparts.count - 1 'make synchronous call blocks until' 'another thread available process line' addlinetotheprocessingqueue(currentline) else runningbuffer = lineparts(i) end if next end if loop while currentblocksize = buffersize end using
paste code new console application.
imports system.threading module module1 ' picked 6 randomly, not sure strategy picking number ' also, not sure difference between worker thread , completion thread const maxworkerthreads integer = 6 const maxcompletionportthreads integer = 6 sub main() threadpool.setmaxthreads(maxworkerthreads, maxcompletionportthreads) dim availableworkerthreads integer dim availablecompletionportthreads integer integer = 0 100 ' getavailablethreads returns results via output parameters threadpool.getavailablethreads(availableworkerthreads, availablecompletionportthreads) dim tries integer = 0 while (availableworkerthreads = 0) ' loop not execute if there available threads ' may want add fail-safe check "tries" in case child threads stuck tries += 1 console.writeline(string.format("waiting start item {0}, attempt {1}, available threads: {2}, {3}", i, tries, availableworkerthreads, availablecompletionportthreads)) ' failure call sleep make program unresponsive thread.sleep(1000) ' call getavailablethreads again next test @ top of loop threadpool.getavailablethreads(availableworkerthreads, availablecompletionportthreads) loop ' how pass parameters thread created through queueuserworkitem dim parameters object() = {i} threadpool.queueuserworkitem(addressof dowork, parameters) ' according msdn, must sleep after calling queueuserworkitem, or else current thread exit thread.sleep(500) next end sub sub dowork(parameters object()) dim itemnumber = parameters(0) dim sleeplength = itemnumber * 1000 console.writeline(string.format("item: {0} - sleeping {1} miliseconds.", itemnumber, sleeplength)) thread.sleep(sleeplength) console.writeline(string.format("item: {0} - done sleeping.", itemnumber)) end sub end module