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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:18:02+00:00 2026-05-27T10:18:02+00:00

Example threadpool: public class Example { public static void Main() { ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc)); //task

  • 0

Example threadpool:

public class Example {
public static void Main() {

    ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc)); //task 1
    ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc)); //task 2
    ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc)); //task 3

    Console.WriteLine("Main thread does some work, then sleeps.");

    Thread.Sleep(1000);

    Console.WriteLine("Main thread exits.");
}
static void ThreadProc(Object stateInfo) {
    Console.WriteLine("Hello from the thread pool.");
}
}

Example Semaphore:

public class Example
{
private static Semaphore _pool;

private static int _padding;

public static void Main()
{
      _pool = new Semaphore(0, 3);

    // Create and start five numbered threads. 
    //
    for(int i = 1; i <= 5; i++)
    {
        Thread t = new Thread(new ParameterizedThreadStart(Worker));

        t.Start(i);
    }

    Thread.Sleep(500);

     Console.WriteLine("Main thread calls Release(3).");
    _pool.Release(3);

    Console.WriteLine("Main thread exits.");
}

private static void Worker(object num)
{
    Console.WriteLine("Thread {0} begins " +
        "and waits for the semaphore.", num);
    _pool.WaitOne();

    int padding = Interlocked.Add(ref _padding, 100);

    Console.WriteLine("Thread {0} enters the semaphore.", num);

    Thread.Sleep(1000 + padding);

    Console.WriteLine("Thread {0} releases the semaphore.", num);
    Console.WriteLine("Thread {0} previous semaphore count: {1}",
        num, _pool.Release());
}
}

I guess its some overhead with the Semaphore, to create 5 threads in this example,
but the threadpool, uses the built in “threadpool” (it will use existing threads like the backgroundworker) . Is this correct or is it more to this, is there any real advantage to use a semaphore, if you just want a simple threadpool but speed and performance is an issue?

  • 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-27T10:18:03+00:00Added an answer on May 27, 2026 at 10:18 am

    Create and manage explicit threads only if you expect those threads to be long running, OR you need to have that thread executed as soon as possible. Even then, you’re probably better off using the Task Parallel Library and marking the task as long-running.

    The thread pool maintains a pool of “live” threads that are idle. So when you call QueueUserWorkItem, it’s highly likely that there will be an idle thread that can grab your work and go. That’s not always true, but it often is. When you create a new thread (i.e. var t = new Thread(...)), there is always some startup overhead.

    In addition, the thread pool lets you set the maximum number of pool threads, and manages the workload. So if you allow four pool threads and queue ten work items, the thread pool will ensure that only four pool threads are running at a time. In some ways, you can think of it as an implicit semaphore in that it won’t let more than those four threads run at a time. But it lets you queue as many as you want (within some large system-defined limit).

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

Sidebar

Related Questions

I'm using C# with .net 4.0 Is ThreadPool.QueueUserWorkItem thread safe? Example: Parallel.ForEach(commands, commandModel =>
In this following excerpt from an example on msdn: public static void StartListening() {
Can any one guide me with example about Thread and ThreadPool what is difference
I am trying to understand what ThreadPool does, I have this .NET example: class
Passing two parameters to a new thread on the threadpool can sometimes be complicated,
using System; using System.Threading; public class Example { // mre is used to block
I'm new to threading and I came accross a custom thread pool implementation example
Example C API signature: void Func(unsigned char* bytes); In C, when I want to
I have a java application where the main-thread starts 2 other threads. If one
I am running into a bit of difficulty using the threadpool class and an

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.