multithreading - Thread Pool Class developement -
i run multithread application , want limit number of threads on machine code concept goes (it drft show major ideas behind)
// list threads have class mythreadlist = list<tmycalcthread>; // pool class, check how many threads active // try start additions threads once nr. of running threads // below max_thread_value_running class mytheardpool = class threadlist : mythreadlist; stillrunningthreads : integer; startfromthreadid : integer; end; var athreadlist : mythreadlist; procedure mainform.callcreatethreadfunction( < thread params > ); begin // create not start here mythread := tmycalcthread.create ( < thread params > ); mythread.onterminate := what_to_do; // add threads list athreadlist.add(mythread); end; /// (a) procedure mainform.what_to_do () var start : integer; begin max_thread_value_running := max_thread_value_running -1; if max_thread_value_running < max_thread_count begin start := max_thread_count - max_thread_value_running; startthereads(start,startfromthreadid) end; end; procedure mainform.startthereads (howmany , fromindex : integer); var : integer; begin := fromindex howmany + fromindexdo begin // thread[i].start end; end; mainform.button (); /// main vcl form , create threats := 0 allthreads begin callcreatethreadfunction( < thread params > ); end; ...... /// (b) while stillrunningthreads > 0 begin application.processmessages; end;
the complete idea small list threads, on every individual thread terminate step update number of running threads , start possible max. number of new threads.(a) instead of function waitforsingleobject () .... loop @ end wait threads finish execution. (b)
from code design did not find full example on net, may approach vaild design or run trouble did not consider right now. comment on diesign or better class design welcome.
don't try micro-manage threads this. don't. either use win api threadpool calls, or make own threadpool producer-consumer queue , tthread instances.
when task completed, suggest work thread call 'oncompletion' tnotifyevent of task class task parameter. ths can set issuer of task might wish, eg. postmessaging task instance gui thread display.
micro-managing threads, continually creating/waiting/terminating/destroying, application.processmessages loops etc. horrible , sure go wrong @ point.
waiting in gui event-handler bad. gui system state-machine , should not wait inside it. if want issue task gui thread, don't wait - fire off , forget until completed , gets posted message-handler procedure.
Comments
Post a Comment