Re: pthread & its bug

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



John (Eljay) Love-Jensen wrote:
> Hi Mohsen,
> 
> Your inquiry is off topic for this forum.  I do not say this to chastize you.  I say it because you may get better / faster / more accurate information from a more appropriate forum that is pthread savvy.
> 
> There are a whole bunch of errors in your test source code.
> 
> For example, the pthread_create's third parameter takes a function pointer to a function that looks like:
> 
> void* task(void* ptr);
> 
> You are passing in a function pointer to a function that looks like:
> 
> void task(int* counter);
> 
> And you are casting the function pointer to a data pointer:
> 
> (void*)task1
> 
> That doesn't fit.
> 
> Another example, pthread_create's first parameter takes a pointer to a pthread_t.  You are passing in a poiter to a pointer to a pthread_t.  (And that pointer-to-a-pointer has not been allocated anywhere.)
> 
> That doesn't fit.
> 
> Work through all the mismatched data types, and then see where things end up.  GCC helps you, by emitting a lot of warnings and errors.  Heed them.
> 
> Don't forget to:
> 
> gcc test.c -lpthread
> 
> HTH,
> --Eljay

You might want to define your task functions to have the type expected
by phtread_create instead of casting them, something like;

void *task1(void *p)
{
  int *counter=(int *)p;

    while(*counter < 5 ){
        printf("task1 count: %d\n",*counter);
        (*counter)++;
    }

    return NULL;
}

The type system is there to help you, dont cast things unless you really
need to.

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux