Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / March 2007

Tip: Looking for answers? Try searching our database.

Hard vs Soft Threads

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Anthony Paul - 15 Mar 2007 00:47 GMT
Hello everyone,

I thought I has an excellent grasp on threading within the CLR (I've
read "C# via the CLR" several times over) but I just went through
"Essential .NET" and came across what are reffered to as "soft-
threads". Now based on the little information on the subject, I
presume that the "soft thread" is merely some sort of wrapper around
an actual OS thread? If not, please correct me. I still don't
understand why there was a need to create a "soft-thread" concept in
the first place; why not just use the OS thread as-is? If anyone out
there can offer some clarity on the subject I'd appreciate it.

Regards,

Anthony
Anthony Paul - 15 Mar 2007 01:23 GMT
Forgive the typos... I meant to say "I thought I had", not has... lol!

Cheers!

Anthony
Anthony Paul - 15 Mar 2007 19:33 GMT
Okay, here's an excerpt from "Essential .NET" regarding soft vs hard
threads (page 254, please excuse any typos, they are bound to be mine
and not the authors') :

<EXCERPT>
It is difficult to talk about processes without quickly steering the
conversation to the topic of threads. The CLR has its own abstraction
for modeling the execution of code that is conceptually similar to an
OS thread. The CLR defines a type, System.Threading.Thread, that
represents a schedulable entity in an AppDomain. A
System.Threading.Thread thread object is sometimes referred to as a
"soft thread" because it is a construct that is not recognized by the
underlying operating system. In contrast, OS threads are referred to
as "hard threads" because they are what the OS deals with.

There is NO ONE-TO-ONE RELATIONSHIP BETWEEN HARD THREADS AND CLR SOFT
THREAD objects. However, certain realities are known to be true based
both on the programming model and empirical analysis of the current
CLR implementation. For one thing, a CLR soft thread object resides in
exactly one AppDomain. ... Second, a given AppDomain may have multiple
soft thread objects. In the current implementation, this happens when
two or more hard threads execute code in a single AppDomain. All other
assumptions about the relationship between hard and soft threads are
implementation-specific.

In the current implemenation of the CLR, a given hard thread will have
at most one soft thread object affiliated with it for a given
AppDomain. Also, if a hard thread winds up executing code in multiple
AppDomains, each AppDomain will have a distinct soft thread object
affiliated with that thread. However, a hard thread never enters a
given AppDomain, then that AppDomain will not have a soft thread
object that represents the hard thread.

Finally, each time a given hard thread enters an AppDomainm it gets
the same soft thread object...

The CLR maintains a fair amout of information inside the hard thread's
thread local storage (TLS). In particular, one can find hard TLS
references to the current AppDomain and soft thread object. When a
hard thread crosses over from one AppDomain to another, the CLR
automatically adjusts these references to point to the new "current"
AppDomain and soft thread. The current implenetation of the CLR
maintains a per-AppDomain thread table to ensure that a given hard
thread is afiliated with only one soft thread object per AppDomain.

It is worth noting that a soft thread object has its own private
thread local storage that is accesible via the Thread.GetData and
Thread.SetData methods. Because this TLS is bound to the soft thread
object, when a hard thread crosses the AppDomain boundaires, it cannot
see the soft TLS that was stored while the hard thread was executing
in the previous AppDomain.

... As a point of interest, one can extract the current soft thread
object from the hard thread's TLS via the Thread.CurrentThread
property...
</EXCERPT>

This is basically all the information there is regarding soft-threads.
I haven't been able to find any more references to it anywhere. If
anybody has more details to share, please, do so!!

>From this I take it that the "soft thread" is simply a sort of wrapper
around a "hard thread" to prevent .NET developers from accessing the
hard thread directly. What confuses me, however, is that he states
that there is no one-to-one relationship between hard threads and soft
threads. Would anyone care to clarify this relationship or lack
thereof?

Regards,

Anthony
Chris Mullins [MVP] - 15 Mar 2007 22:29 GMT
The point he's making is true, but mostly irrelevant. As a day-to-day
programmer, even one who does multi-threaded stuff all day long, it's just
doesn't matter.

Yes, the reality is that CLR threads are mapped (by the CLR host) to O/S
threads. The O/S then schedules it's threads, and everything runs just fine.

The potential is there for the CLR host to break this 1:1 mapping, and do
something more creative. Originally the SQL 2005 CLR host was going to do
that, but they have abandoned that as it caused too many issues.

At this point in time, and for the forseeable future, it's a moot point, and
the distinction between "hard & soft threads" can be ignored.

Signature

Chris Mullins, MCSD.NET, MCPD:Enterprise, Microsoft C# MVP
http://www.coversant.com/blogs/cmullins

> Okay, here's an excerpt from "Essential .NET" regarding soft vs hard
> threads (page 254, please excuse any typos, they are bound to be mine
[quoted text clipped - 67 lines]
>
> Anthony
Anthony Paul - 16 Mar 2007 21:45 GMT
Then it's just what I thought... the "soft thread" is, for all intents
and purposes, just a wrapper around the OS thread. I presume this is
all so mysterious and cryptic because of their intent to use fibers in
other CLR implementations. If not for the fibers, I would presume that
there IS in fact a one-to-one relationship between the soft-thread and
hard-thread.

Regards,

Anthony

> The point he's making is true, but mostly irrelevant. As a day-to-day
> programmer, even one who does multi-threaded stuff all day long, it's just
[quoted text clipped - 12 lines]
> --
> Chris Mullins, MCSD.NET, MCPD:Enterprise, Microsoft C# MVPhttp://www.coversant.com/blogs/cmullins
Chris Mullins [MVP] - 15 Mar 2007 18:06 GMT
I've never heard of "Hard" or "Soft" threads before.

I'm guessing that Hard threads are threads that have their lifetime managed
by you, and that Soft Threads come from the various threadpools.

The only other option would be Fibers, but any book that calls itself
"Essential .Net" and talks about Fibers really needs to re-think it's title.

Signature

Chris Mullins, MCSD.NET, MCPD:Enterprise, Microsoft C# MVP
http://www.coversant.com/blogs/cmullins

> Hello everyone,
>
[quoted text clipped - 11 lines]
>
> Anthony
Laura T. - 15 Mar 2007 18:53 GMT
Or maybe CLR threads (soft?) vs. OS Threads (hard?). Strange terms for me
anyway.

Some good readings about CLR threading:

http://bdn1.borland.com/article/borcon/files/3176/paper/3176.html
http://blogs.msdn.com/cbrumme/archive/2003/04/15/51351.aspx
http://msdn.microsoft.com/msdnmag/issues/06/09/clrinsideout/default.aspx

> I've never heard of "Hard" or "Soft" threads before.
>
[quoted text clipped - 20 lines]
>>
>> Anthony

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.