在C语言的pthread函数中传递一个int数组



我正在编写一个多线程程序进行练习。给定一个随机数字数组(100个位置),我必须将其除以5个数组,并将它们分配给5个pthread,以便找到最大值,并将这些值返回给找到它们之间最大值的主函数。这是我目前的代码:

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#define NUM_THREADS 5
#define DIM_VETTORE 100

void *Calcola_max(void* args){

}

int main(){
    int vettore[DIM_VETTORE];
    int t;
    int i;
    srand(time(NULL));
/*riempio il vettore con numeri random*/
        for (i=0; i<DIM_VETTORE; i++){
        vettore[i]=rand() % 500 + 1;
        printf("Numero in posizione %d: %dn", i,vettore[i]);
        }
/*indico le dimensioni di ogni array splittato*/
    int dimensione_split=DIM_VETTORE/NUM_THREADS;
    printf("Dimensione degli array splittati: %dn", dimensione_split);
/*creo tutti i thread*/
        pthread_t thread[NUM_THREADS];
        for (t=0;t<NUM_THREADS; t++){
        printf("Main: creazione thread %dn", t);
        int rc;
            rc=pthread_create(&thread[t], NULL, Calcola_max, &vettore);
                if (rc) {
                printf("ERRORE: %dn", rc);
                exit(-1);
                }
        }
}

我的问题是:我如何分割数组?我如何将每个数组传递给每个线程?提前感谢


所以,我已经编辑了我的代码,但这次它给了我在pthread创建后的分割错误。在我看来,以这种方式传递线程函数的参数是错误的:

...   
pthread_create(&thread[t], NULL, Calcola_max, (void *)&start[i]);
...
void *Calcola_max(void *a){
...
s = *(int *)a;
...

下面是我的全部代码:

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#define NUM_THREADS 5
#define DIM_VETTORE 100
int vettore[DIM_VETTORE];
int    start[100];
int max[100];       //vettore dove vanno tutti i minimi calcolati dai pthread

void *Calcola_max(void *a){
    int array;
    int n=DIM_VETTORE/NUM_THREADS;
    int s, i;
    int start, stop;
    int massimo;
    s = *(int *)a;
    start = s * n;
    if ( s != (NUM_THREADS-1) )
   {
      stop = start + n;
   }
   else
   {
      stop = DIM_VETTORE;
   }
    massimo=vettore[start];
    for (i = start+1; i < stop; i++ )
   {

      if ( vettore[i] > massimo )
         massimo = vettore[i];
   }
   max[s] = massimo;

    //array = (int) a;
    int k;
    int max=0;  
        for (k=0; k<DIM_VETTORE; k++){      //qui devo mettere il range corrente del vettore, o mettere uno split di vettore
        printf("Massimo corrente: %dn",max);
            if (vettore[k]>max) max=vettore[k];     
        }
//return(NULL);     /* Thread exits (dies) */   
pthread_exit;
}

int main(){
    //int vettore[DIM_VETTORE];
    int massimo;       //vettore dei minimi finale in cui opero confronto e calcolo il minimo
    int t;
    int i, j;
    srand(time(NULL));
/*riempio il vettore con numeri random*/
        for (i=0; i<DIM_VETTORE; i++){
        //int num;          //contenitore numero random
        vettore[i]=rand() % 500 + 1;
        //printf("Numero in posizione %d: %dn", i,vettore[i]);
        }
/*indico le dimensioni di ogni array splittato*/
    int dimensione_split=DIM_VETTORE/NUM_THREADS;
    printf("Dimensione degli array splittati: %dn", dimensione_split);
/*creo tutti i thread*/
        pthread_t thread[NUM_THREADS];
        for (t=0;t<NUM_THREADS; t++){
        start[i] = i;
        printf("Main: creazione thread %dn", t);
        int rc;
        //int pos_vettore;
            //for (pos_vettore=0; pos_vettore<100; pos_vettore+20){
            rc=pthread_create(&thread[t], NULL, Calcola_max, (void *)&start[i]);
                if (rc) {
                printf("ERRORE: %dn", rc);
                exit(-1);
                }
            //}
        }
        /*joino i threads*/
        for (i = 0; i < NUM_THREADS; i++)
      pthread_join(thread[i], NULL);
    massimo= max[0];
    sleep(3);
        for (i = 1; i < NUM_THREADS; i++)
                if ( max[i] > massimo )
            massimo = max[i];
    printf("Il massimo è: %dn", massimo);

}

您的pthread可以轻松地访问主程序中的数组。您不需要为此拆分数组。只需确保pthread正在修改主数组的不同部分。使用structtypedef将相关信息传递给pthread函数

如前所述,不能分割或复制数组。

线程与创建它们的进程共享相同的内存区域,因此您可以只传递数组。

如果游戏的目的是通过使用线程获得一些性能增益,那么你几乎肯定不想使用堆分配内存。

不得不说可能有更好的方法,我可能会在线程之前寻找SIMD或其他SSE扩展,但无论如何…

下面的示例经过了大约5分钟的思考,并且需要更好的错误检查和逻辑验证(因为现在是周日上午9点),演示了我认为如何执行计算的最有效方法。

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <pthread.h>
#define THREADS 5
#define DATA 100
typedef struct _pthread_arg_t {
    pthread_t thread;
    int *data;
    unsigned int end;
    int max;
} pthread_arg_t;
void* pthread_routine(void *arg) {
    pthread_arg_t *info = (pthread_arg_t*) arg;
    int *it = info->data,
        *end = info->data + info->end;
    while (it < end) {
        if (*it > info->max) {
            info->max = *it;
        }
        it++;
    }
    pthread_exit(NULL);
}
int main(int argc, char *argv[]) {
    pthread_arg_t threads[THREADS];
    int data[DATA],
        thread = 0,
        limit = 0,
        result = 0;
    memset(&threads, 0, sizeof(pthread_arg_t) * THREADS);   
    memset(&data, 0, sizeof(int) * DATA);
    while (limit < DATA) {
        /* you can replace this with randomm number */
        data[limit] = limit;
        limit++;
    }
    limit = DATA/THREADS;
    while (thread < THREADS) {
        threads[thread].data = &data[thread * limit];
        threads[thread].end = limit;
        if (pthread_create(&threads[thread].thread, NULL, pthread_routine, &threads[thread]) != 0) {
            /* do something */
            return 1;
        }
        thread++;
    }
    thread = 0;
    while (thread < THREADS) {
        if (pthread_join(threads[thread].thread, NULL) != 0) {
            /* do something */
            return 1;
        }
        thread++;
    }
    thread = 0;
    result = threads[0].max;
    printf("result:n");
    while (thread < THREADS) {
        printf("t%d - %d: %dn",
            thread * limit,
            thread * limit + limit - 1,
            threads[thread].max);
        if (threads[thread].max > result) {
            result = threads[thread].max;
        }
        thread++;
    }
    printf("maxt%dn", result);
    return 0;
}

请注意,这是锁和malloc自由的,您可以通过更多的操作来进一步减少指令…

最新更新