我ctypes在python中使用模块来加载一个包含线程本地存储的共享c库.它是一个历史悠久的大型c库,我们正在努力使线程安全.该库包含许多全局变量和静态,因此我们对线程安全的初始策略是使用线程本地存储.我们希望我们的libarary与平台无关,并且在win32,win64和64位Ubuntu上编译和测试线程安全性.从纯粹的c-process开始,似乎没有任何问题.
但是在win32和Ubuntu的python(2.6和2.7)中,我们看到了内存泄漏.当python线程终止时,似乎没有正确释放线程本地存储.或者至少在某种程度上,python进程没有"意识到"内存被释放.实际上在win32上的ac#-program中也可以看到同样的问题,但是我们的win64服务器测试机器上也没有这个问题(也运行python 2.7).
这个问题可以用这样一个简单的玩具例子重现:
创建一个包含(linux/unix删除__declspec(dllexport))的c文件:
#include <stdio.h>
#include <stdlib.h>
void __declspec(dllexport) Leaker(int tid){
static __thread double leaky[1024];
static __thread int init=0;
if (!init){
printf("Thread %d initializing.", tid);
int i;
for (i=0;i<1024;i++) leaky[i]=i;
init=1;}
else
printf("This is thread: %d\n",tid);
return;}
Run Code Online (Sandbox Code Playgroud)
MINGW在Linux上的windows/gcc上编译机智如:
gcc -o leaky.dll(或leaky.so)-shared the_file.c
在Windows上,我们可以用Visual Studio编译,替换__thread为__declspec(thread).但是在win32上(我相信winXP),如果要在运行时加载库,这将不起作用LoadLibrary.
现在创建一个python程序,如:
import threading, ctypes, sys, time
NRUNS=1000
KEEP_ALIVE=5
REPEAT=2
lib=ctypes.cdll.LoadLibrary("leaky.dll")
lib.Leaker.argtypes=[ctypes.c_int]
lib.Leaker.restype=None
def UseLibrary(tid,repetitions):
for i …Run Code Online (Sandbox Code Playgroud)