Changeset 35:f8a5b321d0c3 in xplcommon for src


Ignore:
Timestamp:
01/03/13 06:57:15 (12 years ago)
Author:
István Váradi <ivaradi@…>
Branch:
default
Phase:
public
Message:

Added support for detached threads

Location:
src/xplcommon
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • src/xplcommon/Thread.h

    r20 r35  
    5353
    5454public:
     55    /**
     56     * Construct the thread.
     57     *
     58     * @param detached if true, the thread will be created as a
     59     * detached one, i.e. it can't be joined later on. The thread
     60     * object will also be deleted when its run() function has
     61     * finished.
     62     */
     63    Thread(bool detached = false);
     64
    5565    /**
    5666     * Virtual destructor.
  • src/xplcommon/posix/Thread.cc

    r3 r35  
    4040void* Thread::threadFn(void* arg)
    4141{
    42     reinterpret_cast<Thread*>(arg)->run();
     42    Thread* thread = reinterpret_cast<Thread*>(arg);
     43    thread->run();
     44    if (thread->detached) delete thread;
    4345    return 0;
     46}
     47
     48//------------------------------------------------------------------------------
     49
     50bool Thread::start()
     51{
     52    pthread_attr_t attr;
     53    pthread_attr_init(&attr);
     54    pthread_attr_setdetachstate(&attr, detached ? PTHREAD_CREATE_DETACHED :
     55                                PTHREAD_CREATE_JOINABLE);
     56    bool isOK = pthread_create(&thread, &attr, &threadFn, this)>=0;
     57    pthread_attr_destroy(&attr);
     58    return isOK;
    4459}
    4560
  • src/xplcommon/posix/Thread.h

    r4 r35  
    5959
    6060    /**
     61     * Indicate if the thread is detached or not.
     62     */
     63    bool detached;
     64
     65    /**
    6166     * The posix thread handle.
    6267     */
     
    6469
    6570public:
     71    /**
     72     * Construct the thread
     73     */
     74    Thread(bool detached = false);
     75
    6676    /**
    6777     * Virtual destructor.
     
    98108//------------------------------------------------------------------------------
    99109
    100 inline Thread::~Thread()
     110inline Thread::Thread(bool detached) :
     111    detached(detached)
    101112{
    102113}
     
    104115//------------------------------------------------------------------------------
    105116
    106 inline bool Thread::start()
     117inline Thread::~Thread()
    107118{
    108     return pthread_create(&thread, 0, &threadFn, this)>=0;
    109119}
    110120
  • src/xplcommon/win32/Thread.cc

    r3 r35  
    4040DWORD Thread::threadFn(LPVOID arg)
    4141{
    42     reinterpret_cast<Thread*>(arg)->run();
     42    Thread* thread = reinterpret_cast<Thread*>(arg);
     43
     44    thread->run();
     45
     46    if (thread->detached) delete thread;
     47
    4348    return 0;
    4449}
  • src/xplcommon/win32/Thread.h

    r20 r35  
    5858
    5959    /**
     60     * Indicate if the thread is detached.
     61     */
     62    bool detached;
     63
     64    /**
    6065     * The thread handle.
    6166     */
     
    6368
    6469public:
     70    /**
     71     * Construct the thread object.
     72     */
     73    Thread(bool detached = false);
     74
    6575    /**
    6676     * Virtual destructor.
     
    8494     */
    8595    void join();
     96
     97    void release();
    8698};
    8799
     
    97109//------------------------------------------------------------------------------
    98110
     111inline Thread::Thread(bool detached) :
     112    detached(detached),
     113    handle(0)
     114{
     115}
     116
     117//------------------------------------------------------------------------------
     118
    99119inline Thread::~Thread()
    100120{
     
    106126{
    107127    handle = CreateThread(0, 0, &threadFn, this, 0, 0);
    108     return handle!=0;
     128    bool isOK = handle!=0;
     129    if (detached && handle!=0) {
     130        CloseHandle(handle);
     131        handle = 0;
     132    }
     133    return isOK;
    109134}
    110135
     
    113138inline void Thread::join()
    114139{
    115     WaitForSingleObject(handle, INFINITE);
     140    if (handle!=0) {
     141        WaitForSingleObject(handle, INFINITE);
     142        CloseHandle(handle);
     143    }
    116144}
    117145
Note: See TracChangeset for help on using the changeset viewer.