w1146869587
2021-11-05 cbf39419a8299e7d119618e5e8e1b1eb35f72f45
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <efsw/platform/win/ThreadImpl.hpp>
#include <efsw/Thread.hpp>
#include <assert.h>
 
#if EFSW_PLATFORM == EFSW_PLATFORM_WIN32
 
#include <efsw/Debug.hpp>
 
namespace efsw { namespace Platform {
 
ThreadImpl::ThreadImpl( Thread *owner )
{
    mThread = reinterpret_cast<HANDLE>( _beginthreadex( NULL, 0, &ThreadImpl::entryPoint, owner, 0, &mThreadId ) );
 
    if ( !mThread )
    {
        efDEBUG( "Failed to create thread\n" );
    }
}
 
ThreadImpl::~ThreadImpl()
{
    if ( mThread )
    {
        CloseHandle( mThread );
    }
}
 
void ThreadImpl::wait()
{
    // Wait for the thread to finish, no timeout
    if ( mThread )
    {
        assert( mThreadId != GetCurrentThreadId() ); // A thread cannot wait for itself!
 
        WaitForSingleObject( mThread, INFINITE );
    }
}
 
void ThreadImpl::terminate()
{
    if ( mThread )
    {
        TerminateThread( mThread, 0 );
    }
}
 
unsigned int __stdcall ThreadImpl::entryPoint( void * userData )
{
    // The Thread instance is stored in the user data
    Thread * owner = static_cast<Thread*>( userData );
 
    // Forward to the owner
    owner->run();
 
    // Optional, but it is cleaner
    _endthreadex(0);
 
    return 0;
}
 
}}
 
#endif