GurdeepS
GurdeepS

Reputation: 67243

CurrentThread/ProcessThread objects

In the .NET BCL, there is a CurrentThread and a ProcessThread object. What is the difference between these?

Upvotes: 2

Views: 1898

Answers (2)

Hans Passant
Hans Passant

Reputation: 942020

It is a hangover from a SQL Server project when .NET 2.0 was being designed. They pressed the CLR team really hard to break the link between the .NET Thread class and an operating system thread. They had reason to at the time, SQL Server supports "light-weight" threads that are implemented as fibers. A fiber is the Windows implementation of a "co-routine", aka "green thread", something that was popular about 15 years ago.

The project was a bust, they couldn't get it reliable enough. And the multicore revolution took place at the same time, obsoleting the practicality of co-routines. Sadly, we are stuck with no easy way to map a Thread to a ProcessThread. Quite a loss. Maybe somebody, some day, will take advantage of the uncoupling, I haven't seen it done yet.

The only possible mapping you've got available now is to P/Invoke GetCurrentThreadId() inside the thread itself. That returns a TID that you can match to a ProcessThread.Id.

Upvotes: 17

Scott Weinstein
Scott Weinstein

Reputation: 19117

The CurrentThread static property on the System.Threading.Thread class is the current CLR System.Threading.Thread instance. The CLR Thread is an abstraction over the underlying win32 thread. The System.Diagnostics.ProcessThread class gives one access to the win32 threads, largely for perfomance tracking.

A key distinction of CLR threads is that they are not fixed to win32 threads.

Upvotes: 4

Related Questions