C pthread mutex: Expected expression before `{'

后端 未结 2 658
梦毁少年i
梦毁少年i 2021-01-04 08:24

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 mute

2条回答
  •  佛祖请我去吃肉
    2021-01-04 09:18

    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.

提交回复
热议问题