提交 | 用户 | 时间
|
f7932f
|
1 |
#pragma once |
D |
2 |
#include"cstring" |
|
3 |
#include<fstream> |
|
4 |
#include<iostream> |
|
5 |
#include<string> |
|
6 |
#include<stdio.h> |
|
7 |
#include <Windows.h> |
|
8 |
#include "Python.h" |
|
9 |
|
|
10 |
#include <thread> |
|
11 |
|
|
12 |
|
|
13 |
/*ȫ�ֽ��ͺ��߳���*/ |
|
14 |
class PyGILThreadLock |
|
15 |
{ |
|
16 |
public: |
|
17 |
PyGILThreadLock() |
|
18 |
{ |
|
19 |
_save = NULL; |
|
20 |
nStatus = 0; |
|
21 |
nStatus = PyGILState_Check(); //��ǰ�߳��Ƿ�ӵ��GIL |
|
22 |
PyGILState_STATE gstate; |
|
23 |
if (!nStatus) |
|
24 |
{ |
|
25 |
gstate = PyGILState_Ensure(); //���û��GIL���������ȡGIL |
|
26 |
nStatus = 1; |
|
27 |
} |
|
28 |
_save = PyEval_SaveThread(); |
|
29 |
PyEval_RestoreThread(_save); |
|
30 |
} |
|
31 |
~PyGILThreadLock() |
|
32 |
{ |
|
33 |
_save = PyEval_SaveThread(); |
|
34 |
PyEval_RestoreThread(_save); |
|
35 |
if (nStatus) |
|
36 |
{ |
|
37 |
PyGILState_Release(gstate); //�ͷŵ�ǰ�̵߳�GIL |
|
38 |
} |
|
39 |
} |
|
40 |
|
|
41 |
private: |
|
42 |
PyGILState_STATE gstate; |
|
43 |
PyThreadState *_save; |
|
44 |
int nStatus; |
|
45 |
}; |