Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

new Thread() and Threadpool?

Why does a Thread (which I set IsBackgroundthread to True) is not running with the threadpool Threads ?

/*1*/   volatile bool r = false;
/*2*/   var g= new Thread(() => r=Thread.CurrentThread.IsThreadPoolThread );
/*3*/   g.IsBackground = true;
/*4*/   g.Start();
/*5*/   g.Join();
/*6*/   Console.WriteLine(r); //false

While this code (obviously) does run at a threadpool thread ?

 Task.Factory.StartNew(()=>Console.Write(Thread.CurrentThread.IsThreadPoolThread)); //true
 Console.ReadLine();

p.s. (I know that Task are (by default)run at a background threads and they run in a threadpool , but my question is about a similar situation where I set a thread to run at background).)

like image 827
Royi Namir Avatar asked Dec 12 '25 22:12

Royi Namir


1 Answers

The ThreadPool is a pool of dedicated threads managed by the runtime.

User-created background threads are not part of the threadpool.

In other words, all thread-pool threads are background threads, but not all background threads are thread-pool threads.

like image 132
SLaks Avatar answered Dec 15 '25 11:12

SLaks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!