我正在编写一个用于锻炼的多线程程序。给定一个随机数的数组(100个位置),我必须将它除以5个数组并给它们5个pthreads以便找到最大值并将这些值返回到主函数中,找到它们之间的最大值。到目前为止,这些是我的代码:
...
pthread_create(&thread[t], NULL, Calcola_max, (void *)&start[i]);
...
void *Calcola_max(void *a){
...
s = *(int *)a;
...
我的问题是:如何拆分阵列?我怎样才能将每个数组传递给每个pthread?提前致谢
所以,我编辑了我的代码,但这次它在创建pthread后给了我分段错误。 IMO我错了以这种方式传递线程函数的参数:
#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: %d\n",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: %d\n", i,vettore[i]);
}
/*indico le dimensioni di ogni array splittato*/
int dimensione_split=DIM_VETTORE/NUM_THREADS;
printf("Dimensione degli array splittati: %d\n", 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 %d\n", 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: %d\n", 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 è: %d\n", massimo);
}
这是我的整个代码:
scale_x_longitude
答案 0 :(得分:0)
您的pthread可以轻松访问主程序中的阵列。您不需要为此分割数组。只需确保pthreads正在修改主阵列的不同部分。使用struct
或typedef
将相关信息传递给pthread函数。
答案 1 :(得分:0)
如上所述,您不要拆分或复制数组。
线程与创建它们的进程共享相同的内存区域,因此您可以只传递数组。
如果游戏的目的是通过使用线程找到一些性能增益,那么你几乎肯定不想使用堆分配的内存。
我们可以说有更好的方法,我可能会在线程之前看看SIMD或其他一些SSE扩展,但无论如何......
以下example,已经考虑了大约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: %d\n",
thread * limit,
thread * limit + limit - 1,
threads[thread].max);
if (threads[thread].max > result) {
result = threads[thread].max;
}
thread++;
}
printf("max\t%d\n", result);
return 0;
}
请注意,这是锁定和malloc免费的,你可以通过更多的摆弄来进一步减少指令......