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

时间:2015-02-04 20:37:44

标签: 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);
}

2 个答案:

答案 0 :(得分:0)

  

编辑:我发布了一些代码。请参阅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()运算符

这一大块宏存在一些缺点:

  • 您无法传递rvalues(因为无法创建指向它们的指针)
  • 您需要使用适当的THREAD0THREAD1,...宏,具体取决于参数的数量(不确定是否可以使用可变参数宏解决此问题)。 / p>

    #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);
    

答案 1 :(得分:-1)

&#39;在C中编写线程代码时,我首先要创建一些包含所有参数和包装函数的结构。这会导致大量代码膨胀,并且不易阅读。当然。这是您为制作应用程序所支付的费用(假设它不是愚蠢的),要么快X倍,要么X倍容易实现,因为它更容易被描述为独立功能。你什么都不需要?