I’m new to threading basics.
I have a queue of operations to be performed on a XML files(node add,node delete etc)
1]There are ‘n’ xml files and for each file a thread from thread pool is allocated using
ThreadPool.QueueUserWorkItem to do those file operations.
I want to achieve both concurrency and ordering of operation(important) using threads.
eg: Suppose if operations [a1,a2,a3,a4,a5] are to be performed on file “A.xml”
and operations [b1,b2,b3,b4,b5,b6,b7] are to be performed on file “B.xml” …..
I want to allocated threads such that i can perform these operations in the
same order and also concurrently(since files are different).
2]Also is it possible to assign each operation a thread and achieve concurency and preserve order.
In STA model i did something similar..
while(queue.count>0){
File f = queue.Dequeue(); //get File from queue
OperationList oprlst = getOperationsForFile(f);
// will get list-> [a1,a2,a3,a4,a5]
for each Operation oprn in oprlst
{
performOperation(f,oprn)
//in MTA i want to wait till operation "a1" completes and then operation "a2" will
//start.making threads wait till file is in use or operation a(i) is in use.
}
}
i want to do this concurrently with operation order preservation.
Threads(of operation) can wait on one file…but
Different operations take different execution times.
i tried AutoResetEvent and WaitHandle.WaitAll(..) but it made the while loop
stop untill all ‘a(i)’ operations finish..i want both a(i) and b(j) perform concurrently.
(but ordering in a(i) and b(j))
Currently using .net 2.0 .
This is quite similar and is part of this question asked Question
You should avoid using thread blocking techniques like
Monitorlocks andWaitHandlestructures inThreadPoolthreads, since those threads are used by other processes. You need to have your threading be based around individual files. If an individual file doesn’t take that long to process (and you don’t have too many files), then theThreadPoolwill work.ThreadPool Implementation
You could just use
EnqueueUserWorkItemon a file-centric method…something like this:Then in your code that processes the files, do this:
If you need your calling thread to block until they are all complete, then a
WaitHandleis OK (since you’re blocking your own thread, not theThreadPoolthread). You will, however, have to create a small payload class to pass it to the thread:Thread Implementation
If, however, you have a large number of files (or it may take a significant amount of time for your files to process), then creating your own threads is a better idea. This also allows you to get away with omitting the
WaitHandles.The preceding example is a very rudimentary thread pool, but it should illustrate what needs to be done.