简体   繁体   中英

Ordering of thread using ThreadPool.QueueUserWorkItem

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

Either create a new thread for each file, or just use one ThreadPool.QueueUserWorkItem call for each file - either way, you want the operations for the file to be executed sequentially in order, so there's no point in using multiple threads for that part. In your case the only parallelism available is across different files, not operations.

You should avoid using thread blocking techniques like Monitor locks and WaitHandle structures in ThreadPool threads, 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 the ThreadPool will work.

ThreadPool Implementation

You could just use EnqueueUserWorkItem on a file-centric method...something like this:

private void ProcessFile(Object data)
{ 
    File f = (File)data;

    foreach(Operation oprn in getOperationsForFile(f))
    {
        performOperation(f, oprn);
    }
}

Then in your code that processes the files, do this:

while(queue.Count > 0)
{
    ThreadPool.QueueUserWorkItem(new WaitCallback(ProcessFile), queue.Dequeue());
}

If you need your calling thread to block until they are all complete, then a WaitHandle is OK (since you're blocking your own thread, not the ThreadPool thread). You will, however, have to create a small payload class to pass it to the thread:

private class Payload
{
    public File File;
    public AutoResetEvent Handle;
}

private void ProcessFile(Object data)
{ 
    Payload p = (Payload)data;

    foreach(Operation oprn in getOperationsForFile(p.File))
    {
        performOperation(f, oprn);
    }

    p.Handle.Set();
}

...

WaitHandle[] handles = new WaitHandle[queue.Count];
int index = 0;

while(queue.Count > 0)
{        
    handles[index] = new AutoResetEvent();

    Payload p = new Payload();

    p.File = queue.Dequeue();
    p.Handle = handles[index];

    ThreadPool.QueueUserWorkItem(new WaitCallback(ProcessFile), p);

    index++;
}

WaitHandle.WaitAll(handles);

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 WaitHandle s.

private void ProcessFile(File f)
{     
    foreach(Operation oprn in getOperationsForFile(f))
    {
        performOperation(f, oprn);
    }

    p.Handle.Set();
}

private object queueLock = new object();

private void ThreadProc()
{
    bool okToContinue = true;

    while(okToContinue)
    {
        File f = null;

        lock(queueLock)
        {
            if(queue.Count > 0) 
            {
                f = queue.Dequeue();
            }
            else
            {
                f = null;
            }
        }

        if(f != null)
        {
            ProcessFile(f);
        }
        else
        {
            okToContinue = false;
        }
    }
}

...

Thread[] threads = new Thread[20]; // arbitrary number, choose the size that works

for(int i = 0; i < threads.Length; i++)
{
    threads[i] = new Thread(new ThreadStart(ThreadProc));

    thread[i].Start();
}

//if you need to wait for them to complete, then use the following loop:
for(int i = 0; i < threads.Length; i++)
{
    threads[i].Join();
}

The preceding example is a very rudimentary thread pool, but it should illustrate what needs to be done.

Threads are for operations that can be done asynchronously and you want your operations done synchronously. The processing of the files seems like they can be done asynchronously (multithreaded) though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM