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
| #pragma once
| #include"cstring"
| #include<fstream>
| #include<iostream>
| #include<string>
| #include<stdio.h>
| #include <Windows.h>
| #include "Python.h"
|
| #include <thread>
|
|
| /*全局解释和线程锁*/
| class PyGILThreadLock
| {
| public:
| PyGILThreadLock()
| {
| m_acquired = false;
| //检测当前线程是否拥有GIL
| if (!PyGILState_Check())
| {
| cout << "load GIL" << endl;
| m_gil_state = PyGILState_Ensure(); //如果没有GIL,则申请获取GIL
| m_gil_state = true;
| }
| }
| ~PyGILThreadLock()
| {
| if (m_acquired)
| {
| cout << "reload GIL" << endl;
| PyGILState_Release(gstate); //释放当前线程的GIL
| }
| }
|
| private:
| PyGILState_STATE m_gil_state;
| bool m_acquired;
| };
|
|