DCL 4.0
Loading...
Searching...
No Matches
Thread Class Referenceabstract

#include <Thread.h>

Inheritance diagram for Thread:
Object PollThread

Classes

class  CondVar
class  Event
class  Mutex
class  SingleLock
class  SingleTryLock

Public Types

typedef void(* initRoutine) ()
typedef SingleLock< MutexSingleLockMutex
typedef SingleLock< SpinLock > SingleLockSpin
typedef SingleTryLock< MutexSingleTryLockMutex
typedef SingleTryLock< SpinLock > SingleTryLockSpin

Static Public Member Functions

static void sleep (unsigned int _mills)
static void yield ()
static unsigned long getCurrentThreadId ()
static ThreadgetCurrentThread ()
static void crtLock (const void *_p)
static void crtUnlock (const void *_p)
static long incrementAndGet (volatile long &_n)
static long long incrementAndGet (volatile long long &_n)
static long decrementAndGet (volatile long &_n)
static long long decrementAndGet (volatile long long &_n)

Protected Member Functions

virtual bool init ()
virtual int run ()=0
Protected Member Functions inherited from Object
virtual ~Object ()
 Object ()

Additional Inherited Members

Public Member Functions inherited from Object
virtual String toString () const
virtual void destroy ()
String className () const
bool isInstanceOf (const std::type_info &typeinfo) const
virtual const std::type_info & typeInfo () const

Detailed Description

Definition at line 33 of file Thread.h.

Member Typedef Documentation

◆ initRoutine

typedef void(* Thread::initRoutine) ()

다중 스레드 환경에서 단일 호출을 위한 제어변스의 타입을 정의한다.

Definition at line 142 of file Thread.h.

◆ SingleLockMutex

Definition at line 403 of file Thread.h.

◆ SingleLockSpin

Definition at line 404 of file Thread.h.

◆ SingleTryLockMutex

Definition at line 424 of file Thread.h.

◆ SingleTryLockSpin

Definition at line 425 of file Thread.h.

Member Function Documentation

◆ crtLock()

void Thread::crtLock ( const void * _p)
static

다중 스레드 환경에서 _initRoutine의 단일 호출을 보장한다.

사용의 예는 다음과 같다.

      static Thread::OnceType once = Thread::ONCE_INIT;
      static void initRoutine()
      {
        ...
      }

      Thread::once(once, initRoutine);
 */
static void once(OnceType& _onceControl, initRoutine _initRoutine);







/**
   스레드 전용 지억장소를 위한 키를 생성한다.
   
UNIX는 pthread_key_create, Windows는 TlsAlloc을 사용한다.
   
Exceptions
SysError*키 생성이 실패하면 예외가 던져지며, 사유는 다음과 같다.
  • UNIX - 키생성에 PTHREAD_KEYS_MAX에 도달했거나 메모리가 부족한 경우이다.
  • Windows - TlsAlloc이 TLS_OUT_OF_INDEXES을 반환했다.
*/ static KeyType keyCreate() __DCL_THROWS1(SysError*); /** 스레드 전용 기억장소를 위한 키를 삭제한다
Parameters
_keykeyCreate로 반환된 값.
Exceptions
SysError*_key가 유효하지 않으면 예외가 던져진다. */ static void keyDelete(KeyType _key) __DCL_THROWS1(SysError*); /** 호출 스레드 전용 기억장소에 값을 설정한다.
Parameters
_keykeyCreate로 반환된 값.
Exceptions
SysError*_key가 유효하지 않으면 예외가 던져진다. */ static void keySetValue(KeyType _key, void* _value) __DCL_THROWS1(SysError*); /** 호출 스레드 전용 기억장소의 값을 리턴한다.
Parameters
_keykeyCreate로 반환된 값.
Returns
값이 설정되어 있지 않으면 NULL을 반환한다.
Exceptions
SysError*_key가 유효하지 않으면 예외가 던져진다. */ static void* keyGetValue(KeyType _key) __DCL_THROWS1(SysError*); /** C 런타임이 다중 스레드에 안전하지 않으면 이것을 사용하여 동기화 한다.
crtLock을 호출하면 반드시 이 후에 crtUnlock을 호출 해야만 한다.
   
example
   
   char a[100];
   time_t now = time(NULL);
   Thread::crtLock(&now);
   char* cp = ctime(&now);
   strcpy(a, cp);
   Thread::crtUnlock(&now);
   

Definition at line 262 of file Thread.cpp.

263{
264 __lock_t& lock = __lock[(size_t) _p % __LOCK_COUNT];
265 __lock(lock);
266}
#define __LOCK_COUNT
Definition Thread.cpp:259

◆ crtUnlock()

void Thread::crtUnlock ( const void * _p)
static

Definition at line 268 of file Thread.cpp.

269{
270 __lock_t& lock = __lock[(size_t) _p % __LOCK_COUNT];
271 __unlock(lock);
272}

◆ decrementAndGet() [1/2]

long Thread::decrementAndGet ( volatile long & _n)
static

◆ decrementAndGet() [2/2]

long long Thread::decrementAndGet ( volatile long long & _n)
static

◆ getCurrentThread()

Thread * Thread::getCurrentThread ( )
static

호출한 스레드의 객체를 반환한다.

이 메소드는 스레드 생성 후 init()에서 스레드 전용 기억장소에 설정한 것의 반환값이다. 따라서, init()이 호출되지 않으면 NULL을 리턴한다.

Definition at line 102 of file Thread.cpp.

103{
104 __DCL_ASSERT(__keyCurrentThread != (__key_t)-1);
105 return (Thread*) __key_get(__keyCurrentThread);
106}
#define __DCL_ASSERT(expr)
Definition Object.h:371

◆ getCurrentThreadId()

unsigned long Thread::getCurrentThreadId ( )
static

호출한 스레드의 스레드의 ID을 얻는다.

Definition at line 173 of file Thread.cpp.

174{
175#if __DCL_PTHREAD
176 return pthread_self();
177#elif __DCL_WINDOWS
178 return GetCurrentThreadId();
179#endif
180}

◆ incrementAndGet() [1/2]

long Thread::incrementAndGet ( volatile long & _n)
static

◆ incrementAndGet() [2/2]

long long Thread::incrementAndGet ( volatile long long & _n)
static

◆ init()

bool Thread::init ( )
protectedvirtual

스레드 객체를 초기화한다.

이 메소드는 start()에 의해 생성된 스레드 내에서 호출된다.

기본 구현은 getCurrentThread()을 위해 객체의 포인터를 스레드 전용 기억장소에 설정한다.

See also
#keySetValue
Returns
false를 반환하면 스레드는 즉시 종료되고, run()은 호출되지않고 join()은 -1을 반환한다.

Definition at line 97 of file Thread.cpp.

98{
99 return __key_set_success(__keyCurrentThread, this);
100}

◆ run()

virtual int Thread::run ( )
protectedpure virtual

스레드의 주 실행 루프를 구현한다.

Implemented in PollThread.

◆ sleep()

void Thread::sleep ( unsigned int _mills)
static

미리초만큼 스레드의 실행을 중지한다. UNIX 에서 시그널에 인터럽트될 수 있다.

Definition at line 150 of file Thread.cpp.

151{
152#if __DCL_PTHREAD
153 /*
154 ::sleep(_milliseconds / 1000);
155 ::usleep((_milliseconds % 1000) * 1000);
156 */
157 poll(NULL, 0, _mills);
158#elif __DCL_WINDOWS
159 Sleep(_mills);
160#endif
161}
#define NULL
Definition Config.h:340

◆ yield()

void Thread::yield ( )
static

CPU를 다른 스레드에 양보한다.

Definition at line 163 of file Thread.cpp.

164{
165#if __DCL_PTHREAD
166 // pthread_yield(); deprecated
167 sched_yield();
168#elif __DCL_WINDOWS
169 SwitchToThread();
170#endif
171}

The documentation for this class was generated from the following files: