与qsort相比,重入qsort_r函数的可移植性如何?

daj*_*obe 10 c portability

qsort_r()是可重入版本qsort()的另一个'thunk'参数并将其传递给compare函数,我希望能够在便携式C代码中使用它. qsort()是POSIX和无处不​​在,但qsort_r()似乎是BSD扩展.作为一个特定问题,这是否存在或在Windows C运行时具有等效?

Isa*_*ner 10

我试图用一个例子来编写一个可移植版本的qsort_r/qsort_s(称为sort_r).我也把这个代码放在git repo中(https://github.com/noporpoise/sort_r)

struct sort_r_data
{
  void *arg;
  int (*compar)(const void *a1, const void *a2, void *aarg);
};

int sort_r_arg_swap(void *s, const void *aa, const void *bb)
{
  struct sort_r_data *ss = (struct sort_r_data*)s;
  return (ss->compar)(aa, bb, ss->arg);
}

void sort_r(void *base, size_t nel, size_t width,
            int (*compar)(const void *a1, const void *a2, void *aarg), void *arg)
{
  #if (defined _GNU_SOURCE || defined __GNU__ || defined __linux__)

    qsort_r(base, nel, width, compar, arg);

  #elif (defined __APPLE__ || defined __MACH__ || defined __DARWIN__ || \
         defined __FREEBSD__ || defined __BSD__ || \
         defined OpenBSD3_1 || defined OpenBSD3_9)

    struct sort_r_data tmp;
    tmp.arg = arg;
    tmp.compar = compar;
    qsort_r(base, nel, width, &tmp, &sort_r_arg_swap);

  #elif (defined _WIN32 || defined _WIN64 || defined __WINDOWS__)

    struct sort_r_data tmp = {arg, compar};
    qsort_s(*base, nel, width, &sort_r_arg_swap, &tmp);

  #else
    #error Cannot detect operating system
  #endif
}
Run Code Online (Sandbox Code Playgroud)

用法示例:

#include <stdio.h>

/* comparison function to sort an array of int, inverting a given region
   `arg` should be of type int[2], with the elements
   representing the start and end of the region to invert (inclusive) */
int sort_r_cmp(const void *aa, const void *bb, void *arg)
{
  const int *a = aa, *b = bb, *p = arg;
  int cmp = *a - *b;
  int inv_start = p[0], inv_end = p[1];
  char norm = (*a < inv_start || *a > inv_end || *b < inv_start || *b > inv_end);
  return norm ? cmp : -cmp;
}

int main()
{
  /* sort 1..19, 30..20, 30..100 */
  int arr[18] = {1, 5, 28, 4, 3, 2, 10, 20, 18, 25, 21, 29, 34, 35, 14, 100, 27, 19};
  /* Region to invert: 20-30 (inclusive) */
  int p[] = {20, 30};
  sort_r(arr, 18, sizeof(int), sort_r_cmp, p);

  int i;
  for(i = 0; i < 18; i++) printf(" %i", arr[i]);
  printf("\n");
}
Run Code Online (Sandbox Code Playgroud)

编译/运行/输出:

$ gcc -Wall -Wextra -pedantic -o sort_r sort_r.c
$ ./sort_r
 1 2 3 4 5 10 14 18 19 29 28 27 25 21 20 34 35 100
Run Code Online (Sandbox Code Playgroud)

我在mac和linux上测试过.如果您发现错误/改进,请更新此代码.您可以根据需要自由使用此代码.


Gab*_*abe 8

对于Windows,您可以使用qsort_s:http://msdn.microsoft.com/en-us/library/4xc60xas(VS.80).aspx

显然有一些关于BSD和GNU有不兼容版本的争议qsort_r,所以在生产代码中使用它要小心:http://sourceware.org/ml/libc-alpha/2008-12/msg00003.html

BTW,_s代表"安全"和_r代表"重入",但两者都意味着有一个额外的参数.


R..*_*R.. 5

它没有在任何可移植性标准中指定.另外我认为把它称为"线程安全"版本是错误的qsort.该标准qsort是线程安全的,但qsort_r有效地允许您传递闭包作为比较函数.

显然,在单线程环境中,您可以使用全局变量获得相同的结果qsort,但这种用法不是线程安全的.线程安全的另一种方法是使用特定于线程的数据,并让比较函数从特定于线程的数据中检索其参数(pthread_getspecific使用POSIX线程,或__threadgcc中的变量和即将推出的C1x标准).

  • 你是对的。它不是线程安全的,它是可重入的。这意味着即使在单线程环境中,您仍然可能需要它。 (2认同)