c - 在 C 中使用 pthreads 时如何避免代码膨胀?

标签 c multithreading pthreads c-preprocessor

在用 C 编写线程代码时,我首先必须创建一些 struct,其中包括所有参数和一个包装函数。这会导致大量代码膨胀并且不易阅读。见:

struct some_function_args {
  int arg1;
  int arg2;
  int arg3;
};

void some_function_wrapper(struct some_function_args* args) {
  some_function(args->arg1, args->arg2, args->arg3);
}

int main() {
  struct my_args;
  my_args.arg1 = 1;
  my_args.arg2 = 2;
  my_args.arg3 = 3;

  pthread_create(..., some_function_wrapper, &my_args);
  pthread_join(...);
}

是否有某种宏或库(可能使用 varargs)自动为我创建所需的结构和包装函数,就像这样?或者这在 C 中根本不可能吗?

int main() {
  MY_THREAD thread = IN_THREAD {
    some_function(1, 2, 3);
  }

  JOIN_THREAD(thread);
}

最佳答案

EDIT: I released some code. See threadify.h.

使用下面的宏你可以做这样的事情:

char a = 'A';
int  b = 23;
char[] c = "Example";


pthread_t thread;

// THREAD3 because it takes 3 arguments of variable type.
THREAD3(thread, a, b, c, {
  printf("test: %c %d %s\n", a, b, c);
});

JOIN(thread);

在 GCC 中是可能的,因为 GCC 有两个非标准扩展:

  • 嵌套函数
  • typeof() 运算符

这个庞大的宏有一些缺点:

  • 您不能传递右值(因为不可能创建指向它们的指针)
  • 您需要根据参数的数量使用适当的 THREAD0THREAD1 ... 宏(不确定是否可以解决此问题使用可变参数宏)。

    #include <stdio.h>
    #include <pthread.h>
    
    // Nested definition to work around preprocessor prescan
    #define __CAT(arg1, arg2) arg1 ## arg2
    #define CAT(arg1, arg2) __CAT(arg1, arg2)
    
    // Use the current line number to create a unique name for objects
    #define NAME(arg1) CAT(arg1, __LINE__)
    
    // Creates a thread without any arguments
    #define THREAD0(thread, code) \
    void NAME(__pthread_wrapper)(void) {\
      do {code;} while(0); \
    }; \
    pthread_create(&thread, NULL, (void*)NAME(__pthread_wrapper), NULL);
    
    // Creates a thread with one argument by creating a struct
    // and passing all values via this struct.
    #define THREAD1(thread, arg1, code) \
    typedef struct { \
      typeof(arg1)* NAME(__pthread_arg1); \
    } NAME(__pthread_struct); \
      void NAME(__pthread_wrapper)(NAME(__pthread_struct)* data) {\
      do {code;} while(0); \
    }; \
    NAME(__pthread_struct) NAME(__data); \
    NAME(__data).NAME(__pthread_arg1) = &arg1; \
    pthread_create(&thread, NULL, (void*)NAME(__pthread_wrapper), &NAME(__data));
    
    #define THREAD2(thread, arg1, arg2, code) \
    typedef struct { \
      typeof(arg1)* NAME(__pthread_arg1); \
      typeof(arg2)* NAME(__pthread_arg2); \
    } NAME(__pthread_struct); \
      void NAME(__pthread_wrapper)(NAME(__pthread_struct)* data) {\
      do {code;} while(0); \
    }; \
    NAME(__pthread_struct) NAME(__data); \
    NAME(__data).NAME(__pthread_arg1) = &arg1; \
    NAME(__data).NAME(__pthread_arg2) = &arg2; \
    pthread_create(&thread, NULL, (void*)NAME(__pthread_wrapper), &NAME(__data));
    
    #define THREAD3(thread, arg1, arg2, arg3, code) \
    typedef struct { \
      typeof(arg1)* NAME(__pthread_arg1); \
      typeof(arg2)* NAME(__pthread_arg2); \
      typeof(arg3)* NAME(__pthread_arg3); \
    } NAME(__pthread_struct); \
      void NAME(__pthread_wrapper)(NAME(__pthread_struct)* data) {\
      do {code;} while(0); \
    }; \
    NAME(__pthread_struct) NAME(__data); \
    NAME(__data).NAME(__pthread_arg1) = &arg1; \
    NAME(__data).NAME(__pthread_arg2) = &arg2; \
    NAME(__data).NAME(__pthread_arg3) = &arg3; \
    pthread_create(&thread, NULL, (void*)NAME(__pthread_wrapper), &NAME(__data));
    
    /* THREAD4, THREAD5, ... */
    
    #define JOIN(thread) pthread_join(thread, NULL);
    

关于c - 在 C 中使用 pthreads 时如何避免代码膨胀?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28330956/

相关文章:

C 随机数生成器 - 它的变化如此缓慢。该怎么办?

c - 如何使 Gtk3 应用程序在 Ubuntu 上看起来更好?

c - 关于 process 和 sleep()

linux - 在调用 pthread_join(th,NULL) 之后调用 pthread_detach(th) 有什么意义吗?

c++ - 进程是否在退出时自动清理 pthreads 占用的资源

linux - 为 pthread 函数传递结构指针 als 参数

c - 程序意外删除字符串中的空格

c# - 线程传递数据和窗口关闭

c# - 线程状态异常

android - 显示屏旋转时 AudioRecord 锁定