aoshi001
(usa Kurumin)
Enviado em 18/09/2008 - 12:39h
Caros
To tentando executar esse programa aqui .
quando compilo no gcc no ambiente windows, ta dando a msg abaixo, o que eu preciso fazer para resolver.
C:\trabalhosistema>Gcc -o tp1 -lpthread tp1.c
Gcc: ûo: No such file or directory
Gcc: tp1: No such file or directory
Gcc: ûlpthread: No such file or directory
tp1.c:3:21: pthread.h: No such file or directory
tp1.c:14: error: syntax error before "threads"
tp1.c:14: warning: data definition has no type or storage class
include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#define NUMTHREADS 2
#define VETSIZE 5000
typedef struct
{
int fromidx, length;
}thread_arg, *ptr_thread_arg;
pthread_t threads[NUMTHREADS];
thread_arg arguments[NUMTHREADS];
int nums[VETSIZE];
int sum;
void *thread_func(void *arg);
int main(int argc, char **argv)
{
int i, length, remainder;
for(i=0; i< VETSIZE ; i++)
nums[i] = (int)( drand48() *100.0 );
sum = 0; length = VETSIZE / NUMTHREADS;
remainder = VETSIZE % NUMTHREADS;
for(i=0; i<NUMTHREADS; i++)
{
arguments[i].fromidx = i * length;
arguments[i].length = length;
if(i == (NUMTHREADS - 1))
arguments[i].length += remainder;
pthread_create(&(threads[i]), NULL, thread_func,&(arguments[i]));
}
for(i=0; i<NUMTHREADS; i++)
{
pthread_join(threads[i], NULL);
}
printf("A soma dos numeros do vetor eh %d\n", sum);
}
void *thread_func(void *arg)
{
ptr_thread_arg argument = (ptr_thread_arg)arg;
int i, localsum = 0, endidx;
endidx = argument->fromidx + argument->length;
for(i=argument->fromidx; i<endidx; i++)
{
localsum += nums[i];
}
sum += localsum;
}