본문 바로가기

프로그래밍/공부관련

스레드 클래스 함수

1. 클래스 헤더안에 static으로 선언

 

class TestDlg {

  public :

       static DWORD WINAPI ThreadHandler(LPVOID lpParam);           // CreateThread
       static unsigned int __stdcall ThreadHandler(void *pParam);       // _beginthreadex

}

 

 

2. 클래서 내용안에 정의

 

// CreateThread

DWORD WINAPI TestDlg::ThreadHandler(LPVOID lpParam)
{

    TestDlg *Dlg = (TestDlg *)lpParam;

 

     while(TRUE) {
        ...
     }


   return 0;

}

 

//  _beginthreadex()

unsigned int __stdcall TestDlg::ThreadHandler(void *pParam) 
{

    TestDlg *Dlg = (TestDlg *)pParam;

    while(TRUE) {
       ...
    }
  return 0;
}

 

 

3. 프로그램에서 실행


m_hThread = CreateThread(NULL, 0, ThreadHandler, this, 0, NULL);

m_hThread = (HANDLE) _beginthreadex(NULL,0,ThreadHandler,(void*)this , 0, NULL);

 

덧붙여쓰자면 이게 진국

C++ 클래스 안에서 쓰레드를 생성해 쓰기

자바는 쓰레드를 쓸려면,
thread클래스를 상속받거나..
runnable인터페이스를 첨부해서 run쓰레드만 구현하면 땡입니다만..

c++은 그렇게 쉽게 안됩니다.. 
플랫폼 종속적이라니 머니 해도.. java가 역시 편하긴 편하죵.

가령 윈도우 플랫폼에서는
_beginthreadex()로 쓰레드를 생성하는데..
문제는 여기서 요구하는 3번째 인자(start_address)가 WINAPI 콜백함수로 클래스 멤버 메소드로
포함시킬수 없다는 문제가 있습니다.

그냥.. 전역함수로 만들어 쓰면 되긴합니다만,  기껏 클래스 만들어 쓰는데, 딱히 그렇게 하기도 뭐하죠? --;;

정확히는 클래스에 포함된 메소드래서 안되는게 아니라, 
단일하지 못해서 그런것 같습니다. 클래스 정의는 하나지만 구현은 여러개 할수 있잖아요.

고로  클래스 안에 콜백 메소드를 넣을려면 걍 static로 선언해 버리면 됩니다.

static UINT WINAPI AAA::hello(LPVOID p)

근데, 문제는 요렇게 해서 넣어 놓으면
static메소드 특성상 멤버 변수를 조작할수 없는게  또 문제가 됩니다.

그럼 우짤까요?

우짜긴요. static 멤버메소드에서 걍 다른 멤버 메소드를 또 호출해서 쓰면 됩니다.

UINT WINAPI AAA::hello(LPVOID p)
 {
  AAA *aaa = (AAA*)p;
  aaa->print();   //print는 일반 멤버 클래스
  
  return 0;
 }

물론 LPVOID p는 _beginthreadex()에서 보내준 클래스 주소(this 값)을 보내서 받습니다.
아래는 main까지 포함한 소스입니다.

#include <process.h>
#include <windows.h>

class AAA
{
public:

 AAA(const char *str)
 {
  int a = strlen(str);
  memcpy(&m_str,str,strlen(str)+1);
 }

 ~AAA()
 {}

 static UINT WINAPI hello(LPVOID p)
 {
  AAA *aaa = (AAA*)p;
  aaa->print();
  
  return 0;
 }

 void start(void)
 {
  _beginthreadex(NULL,0, hello, (LPVOID)this,0,NULL);
 }


private:
 char m_str[10];
 
 void print(void)
 {
  printf("%s\n",m_str);
 }

};


void main()
{
 AAA aaa("hello");
 aaa.start();
 Sleep(5000);
}

Sleep은  메인스레드가 먼저 종료될까봐 넣은 겁니다.
빙빙~ 돌려서 결국 클래스 안에 콜백함수를 넣었습니다.

전역함수를 원하지 않는 객체지향으론
이렇게 어떻게든 클래스 안에 포함시켜서 전역함수 사용을 최소화 해야겠고.. --;;
요걸로 확장해나가면
서버쪽 쓰레드생성을 포함한 singleton 패턴을 만들수 있습니다.

하지만 c/c++ 특성상 전역함수 사용은 어쩔수 없는것 같습니다.
뭐, main자체가 전역함수니깐요.

by 중원 | 2007/07/17 03:25 | PC programming | 트랙백 | 덧글(4)

트랙백 주소 : http://titan515.egloos.com/tb/429059
☞ 내 이글루에 이 글과 관련된 글 쓰기 (트랙백 보내기) [도움말]