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 681919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:32:12+00:00 2026-05-14T01:32:12+00:00

Example for threading queue book Accelerated C# 2008 (CrudeThreadPool class) not work correctly. If

  • 0

Example for threading queue book “Accelerated C# 2008” (CrudeThreadPool class) not work correctly. If I insert long job in WorkFunction() on 2-processor machine executing for next task don’t run before first is over. How to solve this problem? I want to load the processor to 100 percent

public class CrudeThreadPool
{
    static readonly int MAX_WORK_THREADS = 4;
    static readonly int WAIT_TIMEOUT = 2000;
    public delegate void WorkDelegate();

    public CrudeThreadPool()
    {
        stop = 0;
        workLock = new Object();
        workQueue = new Queue();
        threads = new Thread[MAX_WORK_THREADS];
        for (int i = 0; i < MAX_WORK_THREADS; ++i)
        {
            threads[i] = new Thread(new ThreadStart(this.ThreadFunc));
            threads[i].Start();
        }

    }


    private void ThreadFunc()
    {
        lock (workLock)
        {
            int shouldStop = 0;
            do
            {
                shouldStop = Interlocked.Exchange(ref stop, stop);
                if (shouldStop == 0)
                {
                    WorkDelegate workItem = null;

                    if (Monitor.Wait(workLock, WAIT_TIMEOUT))
                    {
                        // Process the item on the front of the queue
                        lock (workQueue)
                        {
                            workItem = (WorkDelegate)workQueue.Dequeue();
                        }
                        workItem();
                    }
                }
            } while (shouldStop == 0);
        }
    }

    public void SubmitWorkItem(WorkDelegate item)
    {
        lock (workLock)
        {

            lock (workQueue)
            {
                workQueue.Enqueue(item);
            }
            Monitor.Pulse(workLock);
        }
    }

    public void Shutdown()
    {
        Interlocked.Exchange(ref stop, 1);
    }
    private Queue workQueue;
    private Object workLock;
    private Thread[] threads;
    private int stop;
}

public class EntryPoint
{
    static void WorkFunction()
    {
        Console.WriteLine("WorkFunction() called on Thread 0}", Thread.CurrentThread.GetHashCode());
        //some long job
        double s = 0;
        for (int i = 0; i < 100000000; i++)
            s += Math.Sin(i);
    }

    static void Main()
    {
        CrudeThreadPool pool = new CrudeThreadPool();
        for (int i = 0; i < 10; ++i)
        {
            pool.SubmitWorkItem(
            new CrudeThreadPool.WorkDelegate(EntryPoint.WorkFunction));
        }
        pool.Shutdown();
    }
}
  • 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-14T01:32:13+00:00Added an answer on May 14, 2026 at 1:32 am

    I can see 2 problems:

    • Inside ThreadFunc() you take a lock(workLock) for the duration of the method, meaning your threadpool is no longer async.

    • in the Main() method, you close down the threadpool w/o waiting for it to finish. Oddly enough that is why it is working now, stopping each ThreadFunc after 1 loop.

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

Sidebar

Related Questions

In this threading online book: http://www.albahari.com/threading/part4.aspx theres an example of Thread.MemoryBarrier() class Foo {
My question is: "How does the stacking work on Asynchronous threading?" For example if
I've reused the example producer consumer queue from the C# in a Nutshell book
guys please post me an example for threading in MFC....it should show the progress
Example Div: <div class=container> <div class=select1></div> <div class=select2></div> <div class=select3></div> </div> Now I want
If I have a program that uses threading and Queue, how do I get
I have this code: package org.example.Threading; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message;
Example System.Threading.AutoResetEvent e = new System.Threading.AutoResetEvent(false); bool b = e.WaitOne(1000, false); I've done a
System.Threading.Interlocked.CompareExchange operator provides atomic (thus thread-safe) C# implementation of the Compare-And-Swap operation. For example
When it comes to threading, I know you have to make sure you're not

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.