简体   繁体   English

在C中将“线程入口点”函数称为“常规”函数,这是不好的代码习惯吗?

[英]Is calling a “thread entry point” function, in C, as “normal” function, a bad code practice?

I recently faced a very particular situation in which I had to think about using the same function both in a "normal" function call and as a "thread entry point" function, using POSIX threads, in C. 我最近遇到了一个非常特殊的情况,在这种情况下,我不得不考虑在C中使用POSIX线程在“正常”函数调用中和“线程入口点”函数中使用相同的函数。

Let me better explain the situation: I have a program which, depending on the user choices, should perform different tasks. 让我更好地解释这种情况:我有一个程序,该程序应根据用户的选择执行不同的任务。

Two tasks are quite similar but they differ in the sense that, in one case, one set of operations should be performed in parallel to another one, and in the second case, instead, almost the same set of operations should be necessarily performed before the other one, eliminating the need to create a dedicated thread. 两项任务非常相似,但是它们的区别在于,在一种情况下,一组操作应与另一项并行执行,而在第二种情况下,几乎必须在执行同一组操作之前执行同一组操作。另一种方法,无需创建专用线程。

Since the "common" set of operations is very similar in the two cases, I actually wrote a code like the following: 由于这两种情况下的“通用”操作集非常相似,因此我实际上编写了如下代码:

if(task_type==0) {
    // Create two threads
    pthread_create(&tid[0],NULL,&common_operations,(void *) &args);
    pthread_create(&tid[1],NULL,&parallel_operations,(void *) &args);

    pthread_join(tid[0],NULL);
    pthread_join(tid[1],NULL);
} else if(task_type==1) {
    common_operations(&args); // Thread entry point function called as "normal function"
    sequential_operations(&args); // Different with respect to "parallel_operations"
}

Defining the "common_operations" function as: 将“ common_operations”函数定义为:

static void *common_operations (void *arg) {
    // Properly casting void *arg to the right struct, containing, among the other things, "task_type"      

    // Function body....

    if(arg->task_type==0) {
        pthread_exit(NULL);
    } else {
        return NULL;
    }
}

I've never seen a thread entry point function used in this way and I was wondering whether this can be considered acceptable or if it is a bad code pratice to mix things in this way. 我从未见过以这种方式使用线程入口点函数,而且我想知道这是否可以接受,或者以这种方式混合事物是否是不好的代码实践。

Moreover, are there better solutions for this specific case? 此外,针对此特定情况是否有更好的解决方案?

It's certainly valid to call a "thread function" like any other function. 与任何其他函数一样,调用“线程函数”当然是有效的。 There's nothing wrong as such as long as, you convert (cast) to the correct type, it should be fine. 只要您将(转换)转换为正确的类型,就没有问题,应该没问题。 Whether it's a good practice is subjective. 好的做法是否主观。 This is a so uncommon that it'd be hard to find anyone calling it outright "a bad practice, you must avoid" :) 这非常罕见,以至于很难找到任何人直截了当地称它为“不良做法,必须避免” :)

However, you can rewrite slightly so that the "normal" function vs. thread function distinction is clear. 但是,您可以稍微重写一下,以使“常规”函数与线程函数的区别清晰可见。 And make it a void function so that it doesn't return anything in the single-threaded call, which would otherwise look a tad odd. 并使其成为void函数,以便它在单线程调用中不返回任何内容,否则将显得有些奇怪。 This would also avoid potential spaghetti code. 这也将避免潜在的意大利面条代码。

/* Use it in direct, single-threaded call */
static void common_operations(common_arg_type *arg) {
    // Function body....
}

/* Use it in pthread_create call */
static void *common_operations_thread(void *arg) {
    common_operations(args);
    return NULL; /* equivalent to pthread_exit(NULL); */
}

IMO, the considerations you should give are just same for any multi-threaded program: the function remains thread-safe and any function you call from the thread-function are also thread-safe and so on. IMO,对于任何多线程程序,您应该考虑的因素都是相同的:该函数保持线程安全,并且从该线程函数调用的任何函数也是线程安全的,依此类推。 Otherwise, there's not anything to be concerned about. 否则,没有什么可担心的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM