C语言 创建线程 - 传递参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13131982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:13:26  来源:igfitidea点击:

create thread - passing arguments

cmultithreadingfunctionparameter-passing

提问by Mankarse

I am attempting on creating multiple threads that each thread calculates a prime. I am trying to pass a second argument to a function using thread create. It keeps throwing up errors.

我正在尝试创建多个线程,每个线程计算一个素数。我正在尝试使用线程创建将第二个参数传递给函数。它不断抛出错误。

void* compute_prime (void* arg, void* arg2)
{

here is my main() with the create thread. &primeArray[i] after &max_prime is giving me the errors.

这是我的 main() 与创建线程。&max_prime 之后的 &primeArray[i] 给了我错误。

 for(i=0; i< num_threads; i++)
 {
    primeArray[i]=0;
    printf("creating threads: \n");
    pthread_create(&primes[i],NULL, compute_prime, &max_prime, &primeArray[i]);
    thread_number = i;
    //pthread_create(&primes[i],NULL, compPrime, &max_prime);
 }

 /* join threads */
 for(i=0; i< num_threads; i++)
{
    pthread_join(primes[i], NULL);
    //pthread_join(primes[i], (void*) &prime);
    //pthread_join(primes[i],NULL);
    //printf("\nThread %d produced: %d primes\n",i, prime);
    printf("\nThread %d produced: %d primes\n",i, primeArray[i]);
    sleep(1);
}

the error i get is:

我得到的错误是:

myprime.c: In function amaina:
myprime.c:123: warning: passing argument 3 of apthread_createa from incompatible pointer type
/usr/include/pthread.h:227: note: expected avoid * (*)(void *)a but argument is of type avoid * (*)(void *, void *)a
myprime.c:123: error: too many arguments to function apthread_createa

It works fine if i take out the second argument.

如果我去掉第二个参数,它工作正常。

回答by Mankarse

You can only pass a single argument to the function that you are calling in the new thread. Create a struct to hold both of the values and send the address of the struct.

您只能将一个参数传递给您在新线程中调用的函数。创建一个结构来保存这两个值并发送结构的地址。

#include <pthread.h>
#include <stdlib.h>
typedef struct {
    //Or whatever information that you need
    int *max_prime;
    int *ith_prime;
} compute_prime_struct;

void *compute_prime (void *args) {
    compute_prime_struct *actual_args = args;
    //...
    free(actual_args);
    return 0;
}
#define num_threads 10
int main() {
    int max_prime = 0;
    int primeArray[num_threads];
    pthread_t primes[num_threads];
    for (int i = 0; i < num_threads; ++i) {
        compute_prime_struct *args = malloc(sizeof *args);
        args->max_prime = &max_prime;
        args->ith_prime = &primeArray[i];
        if(pthread_create(&primes[i], NULL, compute_prime, args)) {
            free(args);
            //goto error_handler;
        }
    }
    return 0;
}

回答by Varun Vijaykumar

In case of std::thread, the user can pass arguments to the thread function in the following method

在 std::thread 的情况下,用户可以通过以下方法将参数传递给线程函数

std::thread(funcName,arg1,arg2);

std::thread(funcName,arg1,arg2);

for instance,

例如,

//for a thread function, 
void threadFunction(int x,int y){
   std::cout << x << y << std::endl;
}

// u can pass x and y values as below
std::thread mTimerThread;
mTimerThread = std::thread(threadFunction,1,12);

回答by Милош Ве?кови?

This is the code of Manakarse, everything is really good but you need a

这是Manakarse的代码,一切都很好,但你需要一个

pthread_join(thread[i],NULL)

pthread_join(线程[i],NULL)

just to be sure all of threads will successfully execute before end of main thread("main will "waiting" while all of threads aren't finished yet) ;

只是为了确保所有线程都将在主线程结束之前成功执行(“主线程将“等待”而所有线程尚未完成);