如何创建一个互斥体使用只有pthread的链接库? [英] How to create a library which uses mutexes only if pthread is linked?

查看:127
本文介绍了如何创建一个互斥体使用只有pthread的链接库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Linux上创建一个C库,有多种功能,在一些全球数据一起进行操作。为了使这些功能是线程安全的,他们必须在code中的适当位置使用互斥。

I'm creating a C library on Linux which has several functions, which together operate upon some global data. In order for these functions to be thread safe, they must employ mutexes at the appropriate points in the code.

在Linux上,才能在应用程序中使用pthreads的,需要在相应的库链接, -lpthread 的。在我的图书馆的情况下一旦编译,我想,使其工作既如果它的用户决定在他们的应用程序中使用pthreads的,以及如果他们不知道。

In Linux, in order to use pthreads in an application, one needs to link in the appropriate library, -lpthread. In the case of my library once compiled, I'd like to make it work both if the user of it decided to use pthreads in their application, as well as if they don't.

在那里开发者没有在他们的应用程序中使用线程的情况下,他们不会对链接的pthreads。因此,我想我的编译库不需要它,而且,使用互斥在单线程应用程序使用不必要的开销(更不用说是愚蠢的)。

In the case where a developer does not use threads in their application, they will not link against pthreads. Therefore I'd like my compiled library to not require it, and furthermore, employing mutexes in a single threaded application uses needless overhead (not to mention is silly).

是否有某种方式来写code(与海湾合作委员会的扩展如有必要),如果某些符号在链接code的某块将只运行?我知道我可以使用的的dlopen()的朋友,但是这本身就需要一些什么,我试图避免。我想象我在寻找必须存在,几个标准功能都在同一条船上,并需要互斥是线程安全的(他们),但不与pthreads连接甚至工作。

Is there some kind of way to write code (with GCC extensions if necessary) that a certain block of code will only run if certain symbols were linked in? I'm aware I can use dlopen() and friends, but that in itself would require some of what I'm trying to avoid. I imagine what I'm looking for must exist, as several standard functions are in the same boat, and would require mutexes to be thread safe (and they are), but work even when not linked with pthreads.

在这一点上,我注意到的 FreeBSD的的popen()函数线66安培; 67采用非便携式检查 - 的 isthreaded 的,以确定是否使用或不线程,以及是否使用互斥。我怀疑这样的事情以任何方式标准化。但更重要的是这样的code不能编译和链接,如果该符号不被认可,这在Linux中,互斥符号甚至不会present如果pthread的未链接。

On this point, I notice that FreeBSD's popen() function on line 66 & 67 employs a non portable check - isthreaded, to determine if threads are used or not, and whether to use mutexes. I doubt anything like that is standardized in any way. But more to the point such code can't compile and link if the symbols aren't recognized, which in Linux, the mutex symbols won't even be present if pthread is not linked.

要总结:在Linux,一个人如何创建库,它知道当线程还用于,如果是这样,采用互斥酌情且不需要连接依靠pthreads,除非应用程序开发特别希望使用穿线什么地方?

To summarize: On Linux, how does one create a library, which knows when threads are also used, and if so, employs mutexes where appropriate, and does not require linking against pthreads, unless the application developer specifically wants to use threading somewhere?

推荐答案

一些测试后,似乎已经的Linux做什么,我想自动!您只需要如果使用线程,而如果你只是想Pthread互斥的支持,依靠pthreads链接。

After some testing, it seems that Linux already does what I want automatically! You only need to link against pthreads if you use threading, not if you just want pthread mutex support.

在这个测试用例:

#include <stdio.h>
#include <errno.h>
#include <pthread.h>

int main()
{
  pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

  if (!(errno = pthread_mutex_lock(&mutex))) { puts("Mutex locked!"); }
  else { perror("Could not lock mutex"); }

  if (!(errno = pthread_mutex_lock(&mutex))) { puts("Mutex locked!"); }
  else { perror("Could not lock mutex"); }

  return 0;
}

在此编译链接没有pthread中,我看到了互斥锁!两次。这表明pthread_mutex_lock()的本质上是一种非运算。但随着pthreads连接,运行此应用程序会在第一时间之后排档互斥锁!被打印出来。

When compiling this without pthreads linked, I see "Mutex locked!" twice. Which indicates that pthread_mutex_lock() is essentially a non-op. But with pthreads linked, running this application will stall after the first time "Mutex locked!" is printed.

因此​​,我可以在我的图书馆在适当情况下使用互斥,而不必要求pthreads的使用,而且在不需要它没有(发生了重大的?)的开销。

Therefore, I can use mutexes in my library where appropriate, and don't need to require pthreads to use, and no (signifigant?) overhead where it isn't needed.

这篇关于如何创建一个互斥体使用只有pthread的链接库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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