Bucket Sort + Thread

Asked

Viewed 131 times

1

I’m having the following error in this code:

#define TAM 10000 /*Tamanho do vetor*/
#define NUM 10000 /*base para gerador de numeros aleatorios*/

using std::cout;
using std::cin;
using std::endl;

pthread_t thread[TAM];

void gerarVet(long*);
void bucketSort(long*);
void imprimaVet(long*);

int main(){

    long vet[TAM],tinicio,tfim,tempo, i, Troca=0;
    long rt1, rt2;

   tinicio=time(NULL);

   gerarVet(vet);
   //imprimaVet(vet);

    do {

        Troca = 0;

        for(i = 0; i < TAM; i+=2)
        {       
            pthread_t t = thread[i];
            rt1 = pthread_create(&t, NULL, bucketSort(vet), (void*) &i);
        }
        for(i = 0; i < TAM; i+=2)
            pthread_join(thread[i], NULL);  

        Troca = 0;

        for(i = 1; i < TAM; i+=2)
        {
            pthread_t t = thread[i];
            rt2 = pthread_create(&t, NULL, bucketSort(vet), (void*)&i);
        }

        for(i = 1; i < TAM; i+=2)
            pthread_join(thread[i], NULL);


    } while(Troca == 1);

   //bucketSort(vet);   
   imprimaVet(vet);

Error message:

39  79  C:  [Error] invalid use of void expression
49  61  C:  [Error] invalid use of void expression
  • 1

    I’m not entirely sure if that’s the problem, but you shouldn’t store the thread pointer in the array thread after have created it? When you create the thread and get the pointer on t, you simply ignore it and then use the value of thread[i] that does not point to a necessarily correct area... Right? Passing test &thread[i] in place of &t, in the call for pthread_create.

  • It didn’t work... I tried to make him(i) take on a pointer, but I still have the same problem

  • Are you passing a pointer to a local variable (i) by reference? This is not recipe for disaster?

3 answers

1

The definition of the function pthread_create is

   int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                      void *(*start_routine) (void *), void *arg);

As you can see by the definition, the function pthread_create wait as third argument a function with the following signature:

void *f(void *);

Your job bucketSort has signature:

void bucketSort(long *)

Hence the error message:

Error] invalid use of void expression

0

In addition to changing the subscription you need to give a cast on the variable before doing operation with it.

void funcao( void * long_var )  {
    long plus1 = *(long*)long_var + 1;
}

0

I tried to pass to void, but give the following error

[Warning] Pointer of type 'void ' used in arithmetic [-Wpointer-arith] [Error] 'void'' is not a Pointer-to-Object type

  • Ola Gustavo, this area you can post the solution of your problem as answer. Take a tour to learn how to use the site:http://answall.com/tour

Browser other questions tagged

You are not signed in. Login or sign up in order to post.