【问题标题】:create thread - passing arguments创建线程 - 传递参数
【发布时间】:2012-10-19 09:05:16
【问题描述】:

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

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

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

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

我得到的错误是:

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

如果我去掉第二个参数就可以了。

【问题讨论】:

  • 查看下面的答案,还可以查看link 以获得良好的 pthread 参考站点。

标签: c multithreading function parameter-passing


【解决方案1】:
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

typedef struct 
{
    int stop_flag;
    char name[30];
} _process_data;

typedef struct 
{
    int meter_no;
    int port_no;
} _process_control;

typedef struct 
{
   _process_data *process_data;
   _process_control *process_control;
} compute_prime_struct;

void *compute_prime (void *args) 
{
    compute_prime_struct *actual_args = args;
      printf("actual_args->process_data->stop_flag [%d]\n",actual_args->process_data->stop_flag);
      printf("actual_args->process_data->name [%s]\n",actual_args->process_data->name);
      printf("actual_args->process_control->meter_no [%d]\n",actual_args->process_control->meter_no);
      printf("actual_args->process_control->port_no [%d]\n",actual_args->process_control->port_no);
    free(actual_args);
    return 0;
}
void fill_data(_process_data *process_data,_process_control *process_control)
{
    process_data->stop_flag=1;
    process_data->name[0]='P';
    process_control->meter_no=6;
    process_control->port_no=22;
    
    pthread_t tid;
    
    compute_prime_struct *args = malloc(sizeof (*args));
    args->process_data = malloc(sizeof (*args->process_data));
    args->process_control = malloc(sizeof (*args->process_control));

    memcpy (args->process_data, process_data, sizeof (args->process_data));
    memcpy (args->process_control, process_control, sizeof (*args->process_control));
        
        if(pthread_create(&tid, NULL, compute_prime, args)) 
        {
            free(args);
            printf("Error here");
        }
        sleep(1);
}

int main() 
{
     _process_data process_data;
     _process_control process_control;
     
     fill_data(&process_data,&process_control);
    return 0;
}

【讨论】:

  • 请不要只发布代码作为答案,还要解释您的代码的作用以及它如何解决问题的问题。带有解释的答案通常更有帮助、质量更好,并且更有可能吸引投票。
【解决方案2】:

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

pthread_join(thread[i],NULL)

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

【讨论】:

    【解决方案3】:

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

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

    例如,

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

    【讨论】:

    • 错误的语言,C,不是 C++
    【解决方案4】:

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

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

    【讨论】:

      猜你喜欢
      • 2022-06-11
      • 2016-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-09
      • 2011-08-04
      • 2011-02-18
      相关资源
      最近更新 更多