posix threads error

Kaz Kylheku kaz@zeugmasystems.com
Mon Dec 17 07:12:00 GMT 2007


From: "Lucas Prado Melo" <lucaspm@dcc.ufba.br>:
> #define die(msg) do { perror(msg); exit(1); } while(0)

This could be a function.

> struct pack{
>    Stack stck;
>    pthread_mutex_t mutex;
> };
>
> int main(int argc, char *argv[]){
>    void *trashbin;
>    pthread_t peer[2];
>    struct pack pck;
>    //initialize pck mutex
>    if( pthread_mutex_init(&( pck.mutex), NULL) != 0 )
>        die("pthread_mutex_init");

You forgot:

  pck.stck = NULL;  /* create empty stack */

>    //and make it multi-threaded
>    if( pthread_create(&peer[0], NULL, doPush, (void*)&pck) != 0 )
>        die("pthread_create");

The pthread_create function returns an error code, and does not place it 
into errno. So calling perror is useless.

You can use the strerror function to convert the returned value to a 
message.

> //pops an element from stack
> //return NULL if there's no element in the stack
> void * pop(Stack * stck){
>    struct stack * de;
>    void * el;
>    de = *stck;
>    *stck = (*stck)->next;
>    if(de != NULL ){

The variable ``de'' is holds a copy of the original expression ``*stck''.

If ``de'' can be null, don't you think that the original ``*stck'' can also 
be null? That means the stack is empty.

If the stack is empty, ``*stck'' being null, don't you think it's wrong to 
evaluate ``(*stck)->next'' ?



More information about the Gcc-help mailing list