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

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로 바꾸는 작업을 했겠지요?

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




전경/배경 스레드 테스트 코드

#include <thread>
#include <iostream>
#include <unistd.h>
#include <pthread.h>

#define USE_STL

void* threadFunc(void* arg)
{
    sleep(5);
    std::cout << "Hello from thread!" << std::endl;
    return NULL;
}

int main()
{
#if defined(USE_STL)
    std::thread t([&]() {

        sleep(5);
        std::cout << "Hello from thread!" << std::endl;
    });

    t.detach();
#else
    pthread_t tid;
    pthread_create(&tid, NULL, threadFunc, NULL);
#endif

    pthread_exit(nullptr);

    return 0;
}

#include <thread>
#include <iostream>
#include <Windows.h>

DWORD threadFunc(void* arg)
{
    Sleep(5000);
    std::cout << "Hello from thread!" << std::endl;
    return 0;
}

int main()
{
#if defined(USE_STL)
    std::thread t([&]() {

        Sleep(5 * 1000);
        std::cout << "Hello from thread!" << std::endl;
    });

    t.detach();
#else
    CreateThread(nullptr, 0, threadFunc, nullptr, 0, nullptr);
#endif

    ExitThread(0);
    return 0;
}



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

[연관 글]






[최초 등록일: ]
[최종 수정일: 8/26/2024]

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

비밀번호

댓글 작성자
 



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

... 181  [182]  183  184  185  186  187  188  189  190  191  192  193  194  195  ...
NoWriterDateCnt.TitleFile(s)
432정성태1/10/200723959Windows: 13. InitOnceExecuteOnce API 소개 [5]
431정성태1/8/200721551Windows: 12. 비스타는 안전한 윈도우인가? [2]
430정성태1/7/200727482웹: 6. IIS 7 마이그레이션 정리 - Sysnet
427정성태12/30/200618208Team Foundation Server: 14. VS.NET IDE에 통합된 TFS Annotate [1]
425정성태12/29/200622129Windows: 11. Vista IIS 7(Integrated mode)에서의 ASP.NET F5 디버깅 방법
424정성태12/29/200620230기타: 16. 첫걸음 [2]
426정성태12/30/200618629    답변글 기타: 16.1. 예외 발생
420정성태12/27/200623337Windows: 10. Internet Explorer 7.0 호환성 백서 (Word 문서, 1.92MB) [7]파일 다운로드1
419정성태12/25/200625032Windows: 9. VS.NET 2005 원격 디버깅 구성
418정성태12/25/200633090웹: 5. IIS 7에서 클라이언트 측 인증서 사용 시 주의점 [2]
417정성태12/24/200625607Windows: 8. VS.NET 2005에서의 웹 애플리케이션 디버깅 설정
416정성태1/31/200725603오류 유형: 23. MSI 설치 시 로그 파일 생성하는 레지스트리 설정 [3]
415정성태12/31/200629390개발 환경 구성: 20. 인증서 오류 체크 목록
413정성태12/20/200626318VC++: 26. volatile 키워드 [1]파일 다운로드1
412정성태12/17/200630116오류 유형: 22. VS.NET 2005 SP1 설치 도중 오류 [9]
411정성태12/16/200619145오류 유형: 21. TFS SP1 설치 관련 오류 (2) - KB919156 패치 이후 TFS 접근 문제
410정성태12/16/200620495오류 유형: 20. TFS SP1 설치 관련 오류 (1) - KB919156 패치
408정성태12/10/200622656Windows: 7. USB 드라이브 내용 암호화
407정성태12/10/200620271오류 유형: 19. Vista 에서의 VS.NET 2005 로 개발한 어셈블리에 대한 서명 확인 오류
406정성태12/10/200625032Windows: 6. IE 7 검색 공급자 - 영한 사전
403정성태12/6/200632152Windows: 5. Vista 와 웹 인증 등록 서비스의 문제 [5]
402정성태12/11/200623137Windows: 4. Vista 설치 후기 [1]
409정성태12/11/200627159    답변글 Windows: 4.1. Vista 설치 후기 - 두 번째 이야기 [3]
396정성태2/13/200729861오류 유형: 18. "Automatic Updates" 서비스 CPU 100% 점유 현상
393정성태11/8/200619386오류 유형: 17. Unable to start debugging - The binding handle is invalid.
371정성태10/23/200618541오류 유형: 16. STS Communication failed.
... 181  [182]  183  184  185  186  187  188  189  190  191  192  193  194  195  ...