Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 666009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:46:18+00:00 2026-05-13T23:46:18+00:00

I would like to describe some specifics of my program and get feedback on

  • 0

I would like to describe some specifics of my program and get feedback on what the best multithreading model to use would be most applicable. I’ve spent a lot of time now reading on ThreadPool, Threads, Producer/Consumer, etc. and have yet to come to solid conclusions.

I have a list of files (all the same format) but with different contents. I have to perform work on each file. The work consists of reading the file, some processing that takes about 1-2 minutes of straight number crunching, and then writing large output files at the end.

I would like the UI interface to still be responsive after I initiate the work on the specified files.

Some questions:

  1. What model/mechanisms should I use? Producer/Consumer, WorkPool, etc.
  2. Should I use a BackgroundWorker in the UI for responsiveness or can I launch the threading from within the Form as long as I leave the UI thread alone to continue responding to user input?
  3. How could I take results or status of each individual work on each file and report it to the UI in a thread safe way to give user feedback as the work progresses (there can be close to 1000 files to process)

Update:

Great feedback so far, very helpful. I’m adding some more details that are asked below:

  • Output is to multiple independent files. One set of output files per “work item” that then themselves gets read and processed by another process before the “work item” is complete

  • The work items/threads do not share any resources.

  • The work items are processed in part using a unmanaged static library that makes use of boost libraries.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T23:46:19+00:00Added an answer on May 13, 2026 at 11:46 pm

    Update based on comments:
    I don’t agree with the statement that a ThreadPool will not be able to handle the workload you’re encountering… let’s look at your problem and get more specific:
    1. You have almost 1000 files.
    2. Each file might take up to 2 minutes of CPU-intensive work to process.
    3. You want to have parallel processing to increase throughput.
    4. You want to signal when each file is complete and update the UI.

    Realistically you don’t want to run 1000 threads, because you’re limited by the number of cores you have… and since it’s CPU intensive work you are likely to max out the CPU load with very few threads (in my programs it’s usually optimal to have 2-4 threads per core).

    So you shouldn’t load 1000 work items in the ThreadPool and expect to see an increase of throughput. You’ll have to create an environment where you’re always running with an optimal number of threads and this requires some engineering.

    I’ll have to contradict my original statement a little bit and actually recommend a Producer/Consumer design. Check out this question for more details on the pattern.

    Here is what the Producer might look like:

    class Producer
    {
        private final CountDownLatch _latch;
        private final BlockingQueue _workQueue;
        Producer( CountDownLatch latch, BlockingQueue workQueue)
        {
            _latch = latch;
            _workQueue = workQueue;
        }
    
        public void Run()
        {
            while(hasMoreFiles)
            {
                // load the file and enqueue it
                _workQueue.Enqueue(nextFileJob);
            }
    
            _latch.Signal();
        }
    }
    

    Here is your consumer:

    class Consumer
    {
        private final CountDownLatch _latch;
        private final BlockingQueue _workQueue;
    
        Consumer(CountDownLatch latch, BlockingQueue workQueue, ReportStatusToUI reportDelegate)
        {
            _latch = latch;
            _workQueue = workQueue;
        }
    
        public void Run()
        {
            while(!terminationCondition)
            {
                // blocks until there is something in the queue
                WorkItem workItem = _workQueue.Dequeue();
    
                // Work that takes 1-2 minutes
                DoWork(workItem);
    
                // a delegate that is executed on the UI (use BeginInvoke on the UI)
                reportDelegate(someStatusIndicator);
            }
    
            _latch.Signal();
        }
    }
    

    A CountDownLatch:

    public class CountDownLatch
    {
        private int m_remain;
        private EventWaitHandle m_event;
    
        public CountDownLatch(int count)
        {
            Reset(count);
        }
    
        public void Reset(int count)
        {
            if (count < 0)
                throw new ArgumentOutOfRangeException();
            m_remain = count;
            m_event = new ManualResetEvent(false);
            if (m_remain == 0)
            {
                m_event.Set();
            }
        }
    
        public void Signal()
        {
            // The last thread to signal also sets the event.
            if (Interlocked.Decrement(ref m_remain) == 0)
                m_event.Set();
        }
    
        public void Wait()
        {
            m_event.WaitOne();
        }
    }
    

    Jicksa’s BlockingQueue:

    class BlockingQueue<T> {
        private Queue<T> q = new Queue<T>();
    
        public void Enqueue(T element) {
            q.Enqueue(element);
            lock (q) {
                Monitor.Pulse(q);
            }
        }
    
        public T Dequeue() {
            lock(q) {
                while (q.Count == 0) {
                    Monitor.Wait(q);
                }
                return q.Dequeue();
            }
        }
    }
    

    So what does that leave? Well now all you have to do is start all your threads… you can start them in a ThreadPool, as BackgroundWorker, or each one as a new Thread and it doesn’t make any difference.

    You only need to create one Producer and the optimal number of Consumers that will be feasible given the number of cores you have (about 2-4 Consumers per core).

    The parent thread (NOT your UI thread) should block until all consumer threads are done:

    void StartThreads()
    {
        CountDownLatch latch = new CountDownLatch(numConsumer+numProducer);
        BlockingQueue<T> workQueue = new BlockingQueue<T>();
    
        Producer producer = new Producer(latch, workQueue);
        if(youLikeThreads)
        {
            Thread p = new Thread(producer.Run);
            p.IsBackground = true;
            p.Start();
        }
        else if(youLikeThreadPools)
        {
            ThreadPool.QueueUserWorkItem(producer.Run);
        }
    
        for (int i; i < numConsumers; ++i)
        {
            Consumer consumer = new Consumer(latch, workQueue, theDelegate);
    
            if(youLikeThreads)
            {
                Thread c = new Thread(consumer.Run);
    
                c.IsBackground = true;
    
                c.Start();
            }
            else if(youLikeThreadPools)
            {
                ThreadPool.QueueUserWorkItem(consumer.Run);
            }
        }
    
        // wait for all the threads to signal
        latch.Wait();
    
        SayHelloToTheUI();
    }
    

    Please not that the above code is illustrative only. You still need to send a termination signal to the Consumer and the Producer and you need to do it in a thread safe manner.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Would like to get a list of advantages and disadvantages of using Stored Procedures.
I would like to use C++11's variadic templates to achieve a generalized random picker
I would like to use the jQuery button elements as standalone - without the
I would like to implement an distributed Point-Of-Sale system, somewhat like the one described
I would like to know which dependency described in my pom.xml brings a transitive
I would like to connect to a clustered Oracle database described by this TNS:
As described in the title I would like to develop a Windows Forms Application
In a Pygame application, I would like to render resolution-free GUI widgets described in
Would like to create a strong password in C++. Any suggestions? I assume it
Would like to be able to set colors of headings and such, different font

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.