如何保护一个基于C的库的初始化函数? [英] How to protect init function of a C based library?

查看:92
本文介绍了如何保护一个基于C的库的初始化函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个基于C库,并为它在多线程平行工作,我创建的初始化函数一些全局互斥。

I have written a C based library and for it to work in multi-thread parallely, I create some global mutexes in the init function.

我预计初始化函数在主线程中调用多线程使用的库API之前。

I expect the init function to be called in the main thread before the library APIs are used in multi-thread.

但是,如果init函数本身是在多线程直接调用,则这是一个问题。是否有保护初始化函数本身从我的媒体库的方法吗?我能想到的一个方法是要求应用程序创建一个互斥体,保护我的初始化函数并行调用,但我可以保护它从我的库本身?

But, if the init function itself is called in multi-thread directly, then it is a problem. Is there a way to protect the init function itself from my library? One way I can think of is to ask the application to create a mutex and protect parallel calls to my init function, but can I protect it from my library itself?

推荐答案

您可能需要使用德默认入口点函数。

You probably want to use teh default entry point functions.

在窗口中,您可以使用的DllMain 以创建和摧毁你的互斥。

In windows you can use DllMain to create and destroy your mutexes.

在Linux和可能是一些其他的Unix系统,你可以使用 __属性__((构造函数)) __属性__((析构函数))你的进入和退出功能。

On Linux and probably some other Unixes you can use __attribute__((constructor)) and __attribute__((destructor)) on your entry and exit functions.

在这两种情况下,该功能将被一次负载叫和卸载

In both these case, the functions will be called once on load and unload

这篇关于如何保护一个基于C的库的初始化函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆