multithreading - c# Controlled Thread Pooling. More threads are running simultaneously than expected -
first timer threadpooling , critical sections. i'm trying manage number of threads active @ given time. mythreadpool class manages thread counts , active threads. myusefulwork has square method accessed threads. main method queues work using threadpool.queueuserworkitem.
i'm using manualresetevent class methods set() , waitone() try , limit threads max count(mythreadpool.maxthreads) 3 in example. apparently i'm doing wrong, given activethreads count going way beyond maxthreads upto 18(which displayed in output 'increment: number' or 'decreement: number'). activethreads incremented within lock if thread waiting, active thread not incremented.
so if can point out doing wrong, of great help. thank you.
using system; using system.collections.generic; using system.threading; namespace threadingdemo { class myusefulwork { public void square(object number) { try { console.writeline("thread: {0} square of {1} {2}", thread.currentthread.gethashcode(), number, math.pow(convert.todouble(number), 2)); thread.sleep(2000); console.writeline("thread {0} woke up", number); } catch { } { mythreadpool.threadfinishedwork(); } } } public static class mythreadpool { private static int activethreads { get; set; } private static int maxthreads { get; set; } public static manualresetevent mre { get; set; } public static object incrementlock { get; set; } public static object decreementlock { get; set; } public static void setmaxthreads(int maxthreads) { maxthreads = maxthreads; } public static int getmaxthreads() { return maxthreads; } public static void threadstartedwork() { setwait(); } public static void threadfinishedwork() { activethreads--; releasewait(); } private static void setwait() { lock (incrementlock) { activethreads++; console.writeline("increment: {0}", activethreads); if (activethreads >= maxthreads) mre.waitone(timeout.infinite, true); } } private static void releasewait() { lock (decreementlock) { console.writeline("decreement: {0}", activethreads); if (activethreads < maxthreads) mre.set(); } } } class program { public static int main(string[] args) { var myusefulwork = new myusefulwork(); var inputs = new list<string>(); mythreadpool.mre = new manualresetevent(false); mythreadpool.incrementlock = new object(); mythreadpool.decreementlock = new object(); mythreadpool.setmaxthreads(3); (var = 1; <= 20; i++) inputs.add(i.tostring()); (int iitem = 1; iitem <= 20; iitem++) { console.writeline("queue thread pool {0}", iitem); mythreadpool.threadstartedwork(); threadpool.queueuserworkitem(new waitcallback(myusefulwork.square), iitem.tostring()); } console.readkey(); return 0; } } }
the output follows.
queue thread pool 1 increment: 1 queue thread pool 2 increment: 2 queue thread pool 3 increment: 3 thread: 6 square of 1 1 thread: 11 square of 2 4 thread 1 woke decreement: 2 queue thread pool 4 increment: 3 thread: 12 square of 3 9 queue thread pool 5 increment: 4 queue thread pool 6 increment: 5 queue thread pool 7 increment: 6 queue thread pool 8 increment: 7 thread: 6 square of 4 16 queue thread pool 9 increment: 8 queue thread pool 10 increment: 9 queue thread pool 11 increment: 10 queue thread pool 12 increment: 11 queue thread pool 13 increment: 12 queue thread pool 14 increment: 13 thread 2 woke decreement: 12 thread: 11 square of 5 25 queue thread pool 15 increment: 13 queue thread pool 16 increment: 14 queue thread pool 17 increment: 15 queue thread pool 18 increment: 16 queue thread pool 19 increment: 17 queue thread pool 20 increment: 18 thread: 13 square of 6 36 thread: 14 square of 7 49 thread 3 woke decreement: 17 thread 4 woke decreement: 16 thread: 6 square of 9 81 thread: 12 square of 8 64 thread 5 woke decreement: 15 thread: 11 square of 10 100 thread: 15 square of 11 121 thread 6 woke decreement: 14 thread: 13 square of 12 144 thread 7 woke decreement: 13 thread: 14 square of 13 169 thread 9 woke decreement: 12 thread: 6 square of 14 196 thread 8 woke decreement: 11 thread: 12 square of 15 225 thread 10 woke decreement: 10 thread: 11 square of 16 256 thread 11 woke decreement: 9 thread: 15 square of 17 289 thread 12 woke decreement: 8 thread: 13 square of 18 324 thread 13 woke decreement: 7 thread: 14 square of 19 361 thread 14 woke decreement: 6 thread: 6 square of 20 400 thread 15 woke decreement: 5 thread 16 woke decreement: 4 thread 17 woke decreement: 3 thread 18 woke decreement: 2 thread 19 woke decreement: 1 thread 20 woke decreement: 0
you forgot reset()
manualresetevent
, stays set.
you want use autoresetevent
instead of manualresetevent
, see here
Comments
Post a Comment