반응형


쓰레드 중지/재시작 ( SuspendThread, ResumeThread ) 

복사http://blog.naver.com/kki2406/80041184166

SuspendThread

The SuspendThread function suspends the specified thread.

DWORD SuspendThread(  HANDLE hThread   // handle to the thread); 

Parameters

hThread
Handle to the thread.

Windows NT: The handle must have THREAD_SUSPEND_RESUME access.

Return Values

If the function succeeds, the return value is the thread's previous suspend count; otherwise, it is 0xFFFFFFFF. To get extended error information, use the GetLastError function.

 

 

ResumeThread

The ResumeThread function decrements a thread's suspend count. When the suspend count is decremented to zero, the execution of the thread is resumed.

DWORD ResumeThread(  HANDLE hThread   // identifies thread to restart); 

Parameters

hThread
Specifies a handle for the thread to be restarted.

Windows NT: The handle must have THREAD_SUSPEND_RESUME access to the thread.

Return Values

If the function succeeds, the return value is the thread's previous suspend count.

If the function fails, the return value is 0xFFFFFFFF. To get extended error information, call GetLastError.

 

DWORD WINAPI MyThread( LPVOID arg )
{
 int t = 20;
 

while ( t-- ) 
 {
  _tprintf( _T("MyThread()...%d\n"), t);
//  Sleep(500);
 }

 return 0;
}

 

int _tmain(int argc, TCHAR* argv[])
{
 int p = 20;
 DWORD idThread;

 HANDLE hThread = CreateThread(NULL, 0, MyThread, NULL, CREATE_SUSPENDED, &idThread );
 assert( hThread != NULL );
 
 while ( p-- ) 
 {
  _tprintf(_T("_tmain()...%d\n"), p);

 

  if ( p == 17 ) 
  {
   ResumeThread( hThread );
  }

 

  if( p == 10 ) 
  {
   SuspendThread( hThread );
  }

//  Sleep(500);
 }

 

 return 0;
}

 

 


 

 

http://bhnbhn.tistory.com/62

 

 

SuspendThread, ResumeThread


가끔 스레드를 쓰다보면, 스레드를 일시 정지 했다가 다시 시작하는 작업이 필요할 때가 있습니다.

그럴 때 사용하는 함수가 SuspendThread()와 ResumeThread()죠.

 

그런데 이게 특이한게,

내부적으로 suspend count 라는걸 가지고있습니다.

 

뭔얘기냐면, suspend를 할 때 마다 suspend count를 1씩 증가시키고, resume을 할 때 마다, suspend count를 1씩 감소시켜서

suspend count가 0일 때만 스레드를 돌린다는거죠.

 

즉, 10번 중지 했으면, 10번 재실행 해야 다시 돌아간다는 말씀.

 

근데, 사람 일이 어디 그런가요?

 

여기저기서 중지를 시켜놨는데, 한방에 짜잔 하고 재실행 하고 싶을 때가 있다는 말이죠.

 

으.. 그럼 어떡해야하죠? 일일이 어디서 SuspendThread가 호출되었는지 기억이라도 하고 있어야 하나요? 따로 suspend count를 기억하고있어야 하나요?

 

설마 그럴리가요.

 

간단한 해결책을 만들어봅시다.

 

void  Thread::Pause(BOOL p)
{
    if(p)
    {
        SuspendThread(m_hThread); 
    }
    else 
    {
        int supendCount;

        do{//재실행이 됨을 보장한다.
            supendCount = ResumeThread(m_hThread); 
        }while(supendCount > 0);
        
    }
}

----------------------------------------------------------------------------------------------------------

SuspendThread()스레드 사용시 유의점

보통 일시정지/시작하는 프로세스를 만드시거나
백그라운드로 돌아가는 구조를 만드는 경우 스레드를 많이 사용합니다.
물론 소켓 프로그래밍에서도 다중 처리시 사용합니다.

그런데 SuspendThread 사용시 내키는 대로 사용하면 시스템 다운되는 경우가 많습니다....
특히 외부 스레드에서 대상스레드를 정지시키는 경우 문제가 많이 일어나더군요
제가 겪은 것중에 메모리 할당하고 해제하는 루틴이 도는 중에 외부에서
스레드를 정지시켜버리는 경우... 잦은 다운이 일어나더군요~~~~

이를 피하기 위해서 외부 스레드에서 서스펜드하지 마시고...
불리언 타입 전역변수 하나 만들어서 수행중인 스레드에서 변수 체크해서
SuspendThread를 수행할것인지 말것인지 결정하면 됩니다.
ResumeThread는 물론 외부에서만 가능하지만....
고맙게도 SuspendThread는 자신의 스레드 정지가 가능합니다.~~~~

다운이 없어지더군요...


반응형

+ Recent posts