How to protect init function of a C based library?

被刻印的时光 ゝ 提交于 2019-12-04 15:12:50

You probably want to use teh default entry point functions.

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

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

POSIX has the pthread_once function

int pthread_once(pthread_once_t *once_control,
              void (*init_routine)(void));

In the linux man page they then have the following instructive example

static pthread_once_t random_is_initialized = PTHREAD_ONCE_INIT;
extern int initialize_random();

int random_function()
{
 (void) pthread_once(&random_is_initialized, initialize_random);
              ... /* Operations performed after initialization. */
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!