-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathr_thread.cpp
More file actions
69 lines (55 loc) · 1.08 KB
/
Copy pathr_thread.cpp
File metadata and controls
69 lines (55 loc) · 1.08 KB
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
65
66
67
68
#include "r_thread.h"
#include <stdio.h>
#include <handleapi.h>
#include <windef.h>
#include <WinBase.h>
#include "r_util.h"
unsigned WINAPI ThreadFunction( LPVOID lpParam )
{
r_thread* ctx = static_cast<r_thread*>(lpParam);
int ret = ctx->Run();
_endthreadex( 0 );
return ret;
}
r_thread::r_thread()
: m_handle(NULL)
, m_mutex(NULL)
, m_exit_flag(0)
{
}
r_thread::~r_thread()
{
if (m_handle != NULL) {
CloseHandle(m_handle);
}
if (m_mutex != NULL) {
CloseHandle(m_mutex);
}
}
int r_thread::Start()
{
m_handle =(HANDLE) _beginthreadex(NULL, 0, ThreadFunction, this, 0, NULL);
if (m_handle == NULL) {
r_log("fail to create thread\n");
return -1;
}
m_mutex = CreateMutex(NULL, FALSE, NULL);
return 0;
}
void r_thread::RLock()
{
WaitForSingleObject(m_mutex, INFINITE);
}
void r_thread::RUnlock()
{
ReleaseMutex(m_mutex);
}
int r_thread::WaitStop()
{
WaitForSingleObject(m_handle, 5000);
CloseHandle( m_handle );
m_handle = NULL;
CloseHandle( m_mutex );
m_mutex = NULL;
return 0;
}