Microsoft MVP성태의 닷넷 이야기
.NET Framework: 91. Foreground Thread / Background Thread [링크 복사], [링크+제목 복사],
조회: 25733
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 
(연관된 글이 1개 있습니다.)

Foreground Thread / Background Thread


마침 재미있는 글이 또 하나 떠서 ^^ 써봅니다.

메인 스레드가 종료되면 나머지 스레드도 종료될까?
; http://www.jiniya.net/tt/526

실제로 그렇지요. main thread라고 해서 특별한 것은 없습니다. 단지 해당 프로세스 공간의 Code Section을 긁고(!) 다니는 첫 번째 스레드일 뿐. (스레드라... 갑자기 안 좋은 기억이 떠오르는군요. 그 2명도... 어쩌면 "네이버 프로그래머"였을 지도.)

그런데, 닷넷에서 제공되는 Thread 클래스에는 재미있는 속성이 하나 있습니다. 바로 "IsBackgroundThread (read/write)"라는 속성입니다.

Foreground Thread와 Background Thread의 차이점은 단 하나입니다. Foreground Thread의 경우에는 아직 Running 상태에 있다면, 프로세스가 종료되는 것을 막는 반면, Background Thread의 경우에는 설령 동작 중이라 해도 프로세스의 종료를 막지 못합니다.

오호... 스레드에는 주종 관계가 없다고 위의 글에도 나와 있는데... 그렇다면 이를 어찌 설명해야 할까요?

그렇습니다. 닷넷의 "Managed" 환경은, 스레드까지도 잘 "관리"를 해주기 때문에 이런 것이 가능합니다. 이에 대한 대강의 해답을 찾기 위해서는... 역시나 "Shared Source CLR"을 참고하는 것이 좋겠습니다.

Shared Source Common Language Infrastructure 2.0 Release
; http://www.microsoft.com/downloads/details.aspx?FamilyId=8C09FD61-3F26-4555-AE17-3121B4F51D4D&displaylang=en

그럼, 단서가 되는 부분을 하나씩 검색해 볼까요?

"찾기"를 이용해서 우선 검색해 보아야 할 것이 "IsBackground"의 정의일 테죠! 오호... 검색해 보니 아래와 같이 나옵니다.

\sscli20\clr\src\bcl\system\threading\thread.cs

public bool IsBackground {
  get { return IsBackgroundNative(); }
  [HostProtection(SelfAffectingThreading=true)]
  set { SetBackgroundNative(value); }
}
[MethodImplAttribute(MethodImplOptions.InternalCall)]
private extern bool IsBackgroundNative();
[MethodImplAttribute(MethodImplOptions.InternalCall)]
private extern void SetBackgroundNative(bool isBackground);

차례로, "SetBackgroundNative"를 검색해 보면.

\sscli20\clr\src\vm\ecall.cpp

FCFuncStart(gThreadFuncs)
  FCFuncElement("SetBackgroundNative", ThreadNative::SetBackground)

결국 실제 정의는 "ThreadNative::SetBackground"에 있다는 것이고. 이를 또 검색해 보면.

\sscli20\clr\src\vm\comsynchronizable.cpp

FCIMPL2(void, ThreadNative::SetBackground, ThreadBaseObject* pThisUNSAFE, CLR_BOOL isBackground)
{
; 생략

  // validate the thread
  Thread  *thread = pThisUNSAFE->GetInternal();

; 생략

  thread->SetBackground(isBackground);

  HELPER_METHOD_FRAME_END();
}
FCIMPLEND

이렇게 Thread 클래스의 SetBackground 메서드에 isBackground 메서드를 전달하는 것으로 끝을 맺고 있습니다. 그렇다면 과연 SetBackground에서는 어떤 역할을 할까요?

\sscli20\clr\src\vm\threads.cpp

// Background threads must be counted, because the EE should shut down when the
// last non-background thread terminates.  But we only count running ones.
void Thread::SetBackground(BOOL isBack)
{
; 생략

  if (IsDead())
  {
  }
  else
  if (isBack)
  {
    if (!IsBackground())
    {
      FastInterlockOr((ULONG *) &m_State, TS_Background);

      // unstarted threads don't contribute to the background count
      if (!IsUnstarted())
        ThreadStore::s_pThreadStore->m_BackgroundThreadCount++;
    }
  }
  else
  {
; 생략
  }
}

자... "Thread::SetBackground" 함수까지 오니 이제서야 쓸만한 코드들이 나오기 시작합니다. 닷넷에서는, 명시적으로 IsBackground 값을 True로 지정하지 않는 한은 Foreground 스레드입니다. 따라서 isBack == TRUE이고 현재 Background 스레드가 아니면 s_pThreadStore->m_BackgroundThreadCount 값을 1 증가시켜 놓고 있습니다.

그렇다면, 간단한 상황이라고 가정하고 - 현재 어떤 스레드가 종료 중일 때 총 구동되고 있는 Managed Thread 수와 m_BackgroundThreadCount - 1의 수가 같은 상황이라면 주저없이 프로세스 종료를 해도 상관없다는 결론이 나옵니다. 즉, 닷넷의 CLR 호스팅 코드에서는 이러한 처리가 되어 있다는 것이겠지요.

예상 결과를 검증해 보기 위해 조금 더 검색을 해보면, CLR 종료를 할 수 있는 이벤트를 발생하는 다음과 같은 코드를 발견할 수 있습니다.

\sscli20\clr\src\vm\threads.cpp

void ThreadStore::CheckForEEShutdown()
{
; 생략

  if (g_fWeControlLifetime &&
      s_pThreadStore->OtherThreadsComplete())
  {
    BOOL bRet;
    bRet = s_pThreadStore->m_TerminationEvent.Set();
    _ASSERTE(bRet);
  }
}

아하... 예상이 맞아 들어갑니다. OtherThreadsComplete 조건이 TRUE가 되면 m_TerminationEvent 이벤트가 signal 되는군요. 잠시 OtherThreadsComplete 함수를 찾아보면,

\sscli20\clr\src\vm\threads.h

// Have all the foreground threads completed?  In other words, can we release
// the main thread?
BOOL        OtherThreadsComplete()
{
; 생략

  return (m_ThreadCount - m_UnstartedThreadCount - m_DeadThreadCount
          - Thread::m_ActiveDetachCount + m_PendingThreadCount
          == m_BackgroundThreadCount);
}

return 코드는 볼 것도 없이, ^^; 주석이 잘 말해주고 있습니다. 그렇다면, 이렇게 조건을 만족해서 m_TerminationEvent가 signal 된다면 어떤 코드가 실행이 되는 것일까요?

아래의 코드에서 그 해답을 찾을 수 있습니다.

\sscli20\clr\src\vm\threads.cpp

void ThreadStore::WaitForOtherThreads()
{
; 생략

  Thread      *pCurThread = GetThread();

; 생략

  if (!OtherThreadsComplete())
  {
    TSLockHolder.Release();

    FastInterlockOr((ULONG *) &pCurThread->m_State, Thread::TS_ReportDead);

    DWORD ret = WAIT_OBJECT_0;
    while (CLREventWaitWithTry(&m_TerminationEvent, INFINITE, TRUE, &ret))
    {
    }
    _ASSERTE(ret == WAIT_OBJECT_0);
  }
}

무한(INFINITE) 대기를 하고 있습니다. 만약 이 함수의 while 루프가 종료하게 되면... 아마도 당연히 CLR 호스팅이 종료하게 될 것입니다. 그래서 찾아보면 다음의 코드를 발견할 수 있습니다.

\sscli20\clr\src\vm\assembly.cpp

static void RunMainPost()
{

; 중간 생략

  GCX_PREEMP();
  ThreadStore::s_pThreadStore->WaitForOtherThreads();

#ifdef _DEBUG
  // Turn on memory dump checking in debug mode.
  _DbgRecord();
#endif
}

RunMainPost가 리턴하면, Assembly::ExecuteMainMethod가 종료하고, 이어서 SystemDomain::ExecuteMainMethod, ExecuteEXE, _CorExeMain2까지 이어지고, 결국 EEPolicy::HandleExitProcess를 호출하는 것으로 CLR 호스트 프로세스는 종료하게 됩니다.

오호... 그런데... 이 순서를 어디선가 본 기억이 나는 것 같습니다.
기억하시는 분이 계실래나? ^^

0:000> k
ChildEBP RetAddr  
0026ed70 79f9691a KERNEL32!RaiseException+0x58
0026edd0 7a098e88 mscorwks!RaiseTheExceptionInternalOnly+0x226
0026ee94 003e00a7 mscorwks!JIT_Throw+0xfc
WARNING: Frame IP not in any known module. Following frames may be wrong.
0026eeb0 79e826bd 0x3e00a7
0026ef30 79e8451b mscorwks!CallDescrWorkerWithHandler+0xa3
0026f06c 79e84403 mscorwks!MethodDesc::CallDescr+0x19c
0026f084 79e843e0 mscorwks!MethodDesc::CallTargetWorker+0x20
0026f098 79ef3e20 mscorwks!MethodDescCallSite::CallWithValueTypes+0x18
0026f1fc 79ef3c19 mscorwks!ClassLoader::RunMain+0x220
0026f464 79ef3aee mscorwks!Assembly::ExecuteMainMethod+0xa6
0026f934 79ef3737 mscorwks!SystemDomain::ExecuteMainMethod+0x398
0026f984 79ef18d1 mscorwks!ExecuteEXE+0x59
0026f9cc 79003aa0 mscorwks!_CorExeMain+0x11b
0026f9dc 76763833 mscoree!_CorExeMain+0x2c
0026f9e8 77c7a9bd KERNEL32!BaseThreadInitThunk+0xe
0026fa28 00000000 ntdll!_RtlUserThreadStart+0x23

기억이 안 나신다고요? ^^
다음의 토픽에서 설명했었습니다. (그나저나,,, windbg는 자주 사용을 안하다 보니 잊어버리게 되는군요. ^^;)

(Managed) Main Method에 Break Point 걸기
; https://www.sysnet.pe.kr/2/0/469

자꾸만 연상되는군요. 그러고 보니... SQLClr에서는 위의 호스팅 코드에서 사용되던 TLS도 모두 FLS로 바꾸는 작업을 했겠지요?

끝으로. 역시나,,, 이번에도 신영진 님 덕분에. ^^



[이 토픽에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 7/5/2021]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 



2007-07-09 05시30분
[codewiz] 좋은 글 잘 읽고 갑니다.
안 좋은 기억 부분에서 피식하고 웃었습니다.
[guest]

1  2  [3]  4  5  6  7  8  9  10  11  12  13  14  15  ...
NoWriterDateCnt.TitleFile(s)
13564정성태2/22/20241934Windows: 259. Hyper-V Generation 1 유형의 VM을 Generation 2 유형으로 바꾸는 방법
13563정성태2/21/20241959디버깅 기술: 196. windbg - async/await 비동기인 경우 메모리 덤프 분석의 어려움
13562정성태2/21/20241986오류 유형: 896. ASP.NET - .NET Framework 기본 예제에서 System.Web에 대한 System.IO.FileNotFoundException 예외 발생
13561정성태2/20/20242067닷넷: 2218. C# - (예를 들어, Socket) 비동기 I/O에 대한 await 호출 시 CancellationToken을 이용한 취소파일 다운로드1
13560정성태2/19/20242104디버깅 기술: 195. windbg 분석 사례 - Semaphore 잠금으로 인한 Hang 현상 (닷넷)
13559정성태2/19/20242946오류 유형: 895. ASP.NET - System.Security.SecurityException: 'Requested registry access is not allowed.'
13558정성태2/18/20242188닷넷: 2217. C# - 최댓값이 1인 SemaphoreSlim 보다 Mutex 또는 lock(obj)를 선택하는 것이 나은 이유
13557정성태2/18/20241923Windows: 258. Task Scheduler의 Author 속성 값을 변경하는 방법
13556정성태2/17/20241980Windows: 257. Windows - Symbolic (hard/soft) Link 및 Junction 차이점
13555정성태2/15/20242129닷넷: 2216. C# - SemaphoreSlim 사용 시 주의점
13554정성태2/15/20241863VS.NET IDE: 189. Visual Studio - 닷넷 소스코드 디컴파일 찾기가 안 될 때
13553정성태2/14/20241948닷넷: 2215. windbg - thin/fat lock 없이 동작하는 Monitor.Wait + Pulse
13552정성태2/13/20241899닷넷: 2214. windbg - Monitor.Enter의 thin lock과 fat lock
13551정성태2/12/20242094닷넷: 2213. ASP.NET/Core 웹 응용 프로그램 - 2차 스레드의 예외로 인한 비정상 종료
13550정성태2/11/20242210Windows: 256. C# - Server socket이 닫히면 Accept 시켰던 자식 소켓이 닫힐까요?
13549정성태2/3/20242513개발 환경 구성: 706. C# - 컨테이너에서 실행하기 위한 (소켓) 콘솔 프로젝트 구성
13548정성태2/1/20242339개발 환경 구성: 705. "Docker Desktop for Windows" - ASP.NET Core 응용 프로그램의 소켓 주소 바인딩(IPv4/IPv6 loopback, Any)
13547정성태1/31/20242107개발 환경 구성: 704. Visual Studio - .NET 8 프로젝트부터 dockerfile에 추가된 "USER app" 설정
13546정성태1/30/20241971Windows: 255. (디버거의 영향 등으로) 대상 프로세스가 멈추면 Socket KeepAlive로 연결이 끊길까요?
13545정성태1/30/20241874닷넷: 2212. ASP.NET Core - 우선순위에 따른 HTTP/HTTPS 호스트:포트 바인딩 방법
13544정성태1/30/20241894오류 유형: 894. Microsoft.Data.SqlClient - Could not load file or assembly 'System.Security.Permissions, ...'
13543정성태1/30/20241919Windows: 254. Windows - 기본 사용 중인 5357 포트 비활성화는 방법
13542정성태1/30/20241920오류 유형: 893. Visual Studio - Web Application을 실행하지 못하는 IISExpress - 두 번째 이야기
13541정성태1/29/20241978VS.NET IDE: 188. launchSettings.json의 useSSL 옵션
13540정성태1/29/20242105Linux: 69. 리눅스 - "Docker Desktop for Windows" Container 환경에서 IPv6 Loopback Address 바인딩 오류
13539정성태1/26/20242372개발 환경 구성: 703. Visual Studio - launchSettings.json을 이용한 HTTP/HTTPS 포트 바인딩
1  2  [3]  4  5  6  7  8  9  10  11  12  13  14  15  ...