Explore the world from your desktop—one photo at a time. Get the Bing Wallpaper app today. Bring your desktop to life with daily backgrounds when you get Bing Wallpaper
No thanks Get it now

This site uses cookies for analytics, personalized content and ads. By continuing to browse this site, you agree to this use. Learn more

Skip to main content

Microsoft
.NET Blog

.NET Blog

.NET Blog

Home

DevBlogs

Product Blogs

App Center

Azure DevOps

Visual Studio

Visual Studio Code

Visual Studio for Mac

DevOps

Azure Artifacts

Azure Boards

Azure Pipelines

Azure Repos

Azure Test Plans

DevOps

Languages

C++

Java

Java Blog in Chinese

JavaScript

PowerShell

Python

Q#

Scripting

TypeScript

Visual Basic

Visual C#

Visual F#

.NET

.NET

ASP.NET

NuGet

Xamarin

More

Platform Development

Apps for Windows

Azure Government

Azure SDKs

Bing Dev Center

Command Line

DirectX Developer Blog

IoT Developer

Microsoft Edge Dev

Microsoft Azure

Office 365 Development

Old New Thing

PIX on Windows

Premier Developer

Surface Duo

Sustainable Software Engineering

Windows Search Platform

Data Development

Azure Cosmos DB

Azure Data Studio

Azure SQL

OData

Revolutions R

SQL Server Data Tools

Login

Suspending and resuming threads for GC

Maoni

June 7th, 2006

First of all, suspension and resumption of threads is not really part of the GC. GC calls functions to do the suspension and the resumption as a service provided in the CLR. Other components in the CLR also use this service such as the debugger implementation. But it’s true that suspending and resuming because of the GC is the most significant usage of this service.

Secondly, how threads are suspended and resumed works the same way in Workstation GC (with or without Concurrent GC) and Server GC. Workstation GC with Concurrent GC just suspends and resumes threads more times than the other two cases.

Now let’s talk about suspension. A managed thread could be running either managed code or native code at the time. If it’s running native code which can either be from

·         the EE (Execution Engine) of the CLR – for example, EE implements some helper function for your managed call which is on the stack at the time. In this case the EE code tells the suspension code how it should be suspended. A common scenario is the EE code will wait for GC to finish so the suspension code doesn’t need to suspend it (it already suspends itself). Or

·         the native code is not from the EE, which means it’s running some native code via Interop, we don’t need to suspend this thread because it shouldn’t be doing anything with managed objects that can are not pinned. When this thread needs to return to managed code, however, it will need to check if a GC is in progress and if so it needs to wait for GC to finish.

For the managed threads that are running managed code it can be in either

§         a mode where it can be stopped at any time (because the JIT recorded enough info for each instruction) in which case it can be suspended right there. We then redirect it to some function that will make this thread wait for GC to complete. Or

·         a mode where it can’t be stopped at any time, in which case the thread’s return address will be hijacked, meaning we replace the return address by a stub which will suspend this thread. What this stub does is it will suspend this thread to wait for GC to finish.

In both case the function that was used to suspend the thread knows how to resume them correctly when GC finishes.

Resumption works much simpler. When a GC is finished, all the threads that were suspended will wake up (they were waiting on an event for GC to finish) to resume execution.

Maoni Stephens

Follow

Posted in Dot.NetTagged General maoniposts

Read next

I Am a Happy Janitor – Part 1: Finding garbage
Indeed what I do is very much like the job of the janitors – like the ones who clean your building, or janitors you see at a food court, or yourself when you are ...

maoni August 18, 2006

0 comment

Understand the problem before you try to find a solution
So far I’ve never written a blog entry that gives out philosophical advices on doing performance work. But lately I thought perhaps it’s time to write such an entry ...

maoni September 1, 2006

0 comment

0 comments

Comments are closed.

Relevant Links

.NET Download

.NET Hello World

.NET Meetup Events

.NET Documentation

.NET API Browser

.NET SDKs

.NET Application Architecture Guides

Web apps with ASP.NET Core

Mobile apps with Xamarin.Forms

Microservices with Docker Containers

Modernizing existing .NET apps to the cloud

Archive

August 2020

July 2020

June 2020

May 2020

April 2020

March 2020

February 2020

January 2020

December 2019

November 2019

October 2019

September 2019

August 2019

July 2019

June 2019

May 2019

April 2019

March 2019

February 2019

January 2019

December 2018

November 2018

October 2018

September 2018

August 2018

July 2018

June 2018

May 2018

April 2018

March 2018

February 2018

January 2018

December 2017

November 2017

October 2017

September 2017

August 2017

July 2017

June 2017

May 2017

April 2017

March 2017

February 2017

January 2017

December 2016

November 2016

October 2016

September 2016

August 2016

July 2016

June 2016

May 2016

April 2016

March 2016

February 2016

January 2016

December 2015

November 2015

October 2015

September 2015

August 2015

July 2015

May 2015

April 2015

March 2015

February 2015

January 2015

December 2014

November 2014

October 2014

September 2014

August 2014

July 2014

June 2014

May 2014

April 2014

March 2014

February 2014

January 2014

December 2013

November 2013

October 2013

September 2013

August 2013

July 2013

June 2013

May 2013

April 2013

February 2013

January 2013

December 2012

November 2012

October 2012

September 2012

August 2012

July 2012

June 2012

May 2012

April 2012

March 2012

February 2012

October 2011

September 2011

June 2011

April 2011

November 2010

September 2010

June 2010

May 2010

April 2010

March 2010

February 2010

December 2009

November 2009

October 2009

September 2009

August 2009

July 2009

June 2009

May 2009

April 2009

March 2009

February 2009

January 2009

November 2008

May 2007

March 2007

January 2007

December 2006

November 2006

October 2006

September 2006

August 2006

June 2006

April 2006

March 2006

February 2006

January 2006

October 2005

July 2005

May 2005

December 2004

November 2004

September 2004

June 2004

Topics

Dot.Net

.NET

.NET Core

.NET Framework

Entity Framework

C#

ML.NET

F#

WPF

Visual Studio

Performance

AI Machine Learning

Machine Learning

Security

ASP.NET

Docker

Async

Concurrency

TPL

Maoni

.NET Internals

Azure

Apache

LOH

GC

XAML

Stay informed

Code Block
×

Paste your code snippet

Cancel Ok

What's new

Surface Go 2

Surface Book 3

Microsoft 365

Surface Pro X

Windows 10 apps

Microsoft Store

Account profile

Download Center

Microsoft Store support

Returns

Order tracking

Store locations

Virtual workshops and training

Microsoft Store Promise

Education

Microsoft in education

Office for students

Office 365 for schools

Deals for students & parents

Microsoft Azure in education

Enterprise

Azure

AppSource

Automotive

Government

Healthcare

Manufacturing

Financial services

Retail

Developer

Microsoft Visual Studio

Windows Dev Center

Developer Center

Microsoft developer program

Channel 9

Office Dev Center

Microsoft Garage

Company

Careers

About Microsoft

Company news

Privacy at Microsoft

Investors

Diversity and inclusion

Accessibility

Security

English (United States)

Sitemap

Contact Microsoft

Privacy & cookies

Terms of use

Trademarks

Safety & eco

About our ads

© Microsoft 2020