17

I am using the pthread library to create two threads. I am using two queues to communicate the data between the two threads (producer-consumer) and hence want to have a mutex to sync the push-pops in the queue by the threads.

But I get a compile error as follows:

$ gcc simple-tun.c simple-tun -lpthread
simple-tun.c: In function ‘new_queue’:
simple-tun.c:920:13: error: expected expression before ‘{’ token

The the function where I get the error is:

908 struct queue * new_queue () {
909 
910     struct queue * q;
911     q = (struct queue *) malloc (sizeof(struct queue));
912 
913     if (q == NULL)
914         return NULL;
915 
916 
917     q->head = NULL;
918     q->tail = NULL;
919     q->is_empty = 1;
920     q->mutex = PTHREAD_MUTEX_INITIALIZER;
921 
922     return q;
923 }

structure queue is:

    struct queue {
 80     struct node * head;
 81     struct node * tail;
 82     int is_empty;
 83     pthread_mutex_t mutex;
 84 };

If I comment out line 920, the linker starts giving 'multiple declaration errors'

$ gcc simple-tun.c simple-tun -lpthread
simple-tun: In function `settun':
(.text+0x2b7): multiple definition of `settun'
/tmp/cc5Ms4xP.o:simple-tun.c:(.text+0x1cb): first defined here
simple-tun: In function `_fini':
(.fini+0x0): multiple definition of `_fini'
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crti.o:(.fini+0x0): first defined here
simple-tun: In function `mktun':
(.text+0x1e2): multiple definition of `mktun'
/tmp/cc5Ms4xP.o:simple-tun.c:(.text+0xf6): first defined here
simple-tun: In function `net_connect':
(.text+0xe27): multiple definition of `net_connect'
/tmp/cc5Ms4xP.o:simple-tun.c:(.text+0x1115): first defined here
simple-tun: In function `data_start':
(.data+0x0): multiple definition of `__data_start'
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crt1.o:(.data+0x0): first defined here
simple-tun: In function `client_connect':
(.text+0xe95): multiple definition of `client_connect'
/tmp/cc5Ms4xP.o:simple-tun.c:(.text+0x1183): first defined here
simple-tun: In function `data_start':
(.data+0x8): multiple definition of `__dso_handle'
/usr/lib/gcc/x86_64-linux-gnu/4.7/crtbegin.o:(.data+0x0): first defined here
simple-tun:(.rodata+0x0): multiple definition of `_IO_stdin_used'
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crt1.o:(.rodata.cst4+0x0): first defined here
simple-tun: In function `server_connect':
(.text+0xfa2): multiple definition of `server_connect'
/tmp/cc5Ms4xP.o:simple-tun.c:(.text+0x1290): first defined here
simple-tun: In function `print_usage':
(.text+0xe05): multiple definition of `print_usage'
/tmp/cc5Ms4xP.o:simple-tun.c:(.text+0x10f3): first defined here
simple-tun: In function `_init':
(.init+0x0): multiple definition of `_init'
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crti.o:(.init+0x0): first defined here
/usr/lib/gcc/x86_64-linux-gnu/4.7/crtend.o:(.tm_clone_table+0x0): multiple definition of `__TMC_END__'
simple-tun:(.data+0x10): first defined here
/usr/bin/ld: error in simple-tun(.eh_frame); no .eh_frame_hdr table will be created.
collect2: error: ld returned 1 exit status

I was not able to find a solution in my searches. Is there something fundamentally wrong with my code? Can someone help me spot what I am doing wrong?

Please let me know if I need to post more snippets or more outputs.

4
  • no syntax error in function you have shown Jul 25, 2013 at 16:54
  • @GrijeshChauhan - yes there is, how would OP be getting an error otherwise?
    – Carl Norum
    Jul 25, 2013 at 17:04
  • @CarlNorum you found it? Jul 25, 2013 at 17:07
  • ... well, I wrote an answer about it, so I guess so?
    – Carl Norum
    Jul 25, 2013 at 17:08

2 Answers 2

24
  1. You can't use PTHREAD_MUTEX_INITIALIZER like that - it has to be used as an initializer, not in a regular assignment expression. You have two choices to fix it - either call pthread_mutex_init(), or add a typecast to use PTHREAD_MUTEX_INITIALIZER as a compound literal. Your choice of:

    pthread_mutex_init(&q->mutex, NULL);
    

    or:

    q->mutex = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;
    
  2. Your linker error problem is due to this command line:

    gcc simple-tun.c simple-tun -lpthread
    

    You're missing a -o, so you're trying to link the program with itself. That's bad news. What you probably want is:

    gcc simple-tun.c -o simple-tun -lpthread
    

    And really, you should add some warning flags in there, too.

3
  • Hi, Thanks a lot. I used pthread_mutex_init instead of assigning PTHREAD_MUTEX_INITIALIZER and got read of linker error by using -o. The program compiles now. Does this mean that we can only use PTHREAD_MUTEX_INITIALIZER while declaring the mutex?
    – aoak
    Jul 25, 2013 at 17:02
  • @CarlNorum: FYI, using PTHREAD_MUTEX_INITIALIZER as a compound literal or as an initializer for a named object with automatic storage invokes UB. Per POSIX, PTHREAD_MUTEX_INITIALIZER is only for static initialization, and might contain hideous compiler-specific hacks for machines where mutexes physically need to be located in special memory, or where they need an allocated resource. Jul 25, 2013 at 17:50
  • 1
    I did a test here and memcmp says the static initialization and the compound literal matched identically. That's not to say it's safe, though. I didn't actually try to use any of them.
    – Carl Norum
    Jul 25, 2013 at 18:01
4

PTHREAD_MUTEX_INITIALIZER is exactly that, an initializer to use in declarations:

pthread_mutex_t foo = PTHREAD_MUTEX_INITIALIZER;

if you have a pthread_mutex_t that was created elsewhere - such as by malloc - initialize it with pthread_mutex_init:

pthread_mutex_init(&q->mutex, NULL);

from the SUSV2 documentation for pthread_mutex_init:

In cases where default mutex attributes are appropriate, the macro PTHREAD_MUTEX_INITIALIZER can be used to initialise mutexes that are statically allocated. The effect is equivalent to dynamic initialisation by a call to pthread_mutex_init() with parameter attr specified as NULL, except that no error checks are performed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.