C pthread mutex:`{‘之前的预期表达式

前端之家收集整理的这篇文章主要介绍了C pthread mutex:`{‘之前的预期表达式前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用pthread库来创建两个线程.我使用两个队列来传递两个线程(生产者 – 消费者)之间的数据,因此希望有一个互斥体来同步线程中队列中的push-pops.

但我得到一个编译错误如下:

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

我得到错误函数是:

  1. 908 struct queue * new_queue () {
  2. 909
  3. 910 struct queue * q;
  4. 911 q = (struct queue *) malloc (sizeof(struct queue));
  5. 912
  6. 913 if (q == NULL)
  7. 914 return NULL;
  8. 915
  9. 916
  10. 917 q->head = NULL;
  11. 918 q->tail = NULL;
  12. 919 q->is_empty = 1;
  13. 920 q->mutex = PTHREAD_MUTEX_INITIALIZER;
  14. 921
  15. 922 return q;
  16. 923 }

结构队列是:

  1. struct queue {
  2. 80 struct node * head;
  3. 81 struct node * tail;
  4. 82 int is_empty;
  5. 83 pthread_mutex_t mutex;
  6. 84 };

如果我注释掉第920行,链接器会开始提供“多个声明错误

  1. $gcc simple-tun.c simple-tun -lpthread
  2. simple-tun: In function `settun':
  3. (.text+0x2b7): multiple definition of `settun'
  4. /tmp/cc5Ms4xP.o:simple-tun.c:(.text+0x1cb): first defined here
  5. simple-tun: In function `_fini':
  6. (.fini+0x0): multiple definition of `_fini'
  7. /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crti.o:(.fini+0x0): first defined here
  8. simple-tun: In function `mktun':
  9. (.text+0x1e2): multiple definition of `mktun'
  10. /tmp/cc5Ms4xP.o:simple-tun.c:(.text+0xf6): first defined here
  11. simple-tun: In function `net_connect':
  12. (.text+0xe27): multiple definition of `net_connect'
  13. /tmp/cc5Ms4xP.o:simple-tun.c:(.text+0x1115): first defined here
  14. simple-tun: In function `data_start':
  15. (.data+0x0): multiple definition of `__data_start'
  16. /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crt1.o:(.data+0x0): first defined here
  17. simple-tun: In function `client_connect':
  18. (.text+0xe95): multiple definition of `client_connect'
  19. /tmp/cc5Ms4xP.o:simple-tun.c:(.text+0x1183): first defined here
  20. simple-tun: In function `data_start':
  21. (.data+0x8): multiple definition of `__dso_handle'
  22. /usr/lib/gcc/x86_64-linux-gnu/4.7/crtbegin.o:(.data+0x0): first defined here
  23. simple-tun:(.rodata+0x0): multiple definition of `_IO_stdin_used'
  24. /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crt1.o:(.rodata.cst4+0x0): first defined here
  25. simple-tun: In function `server_connect':
  26. (.text+0xfa2): multiple definition of `server_connect'
  27. /tmp/cc5Ms4xP.o:simple-tun.c:(.text+0x1290): first defined here
  28. simple-tun: In function `print_usage':
  29. (.text+0xe05): multiple definition of `print_usage'
  30. /tmp/cc5Ms4xP.o:simple-tun.c:(.text+0x10f3): first defined here
  31. simple-tun: In function `_init':
  32. (.init+0x0): multiple definition of `_init'
  33. /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crti.o:(.init+0x0): first defined here
  34. /usr/lib/gcc/x86_64-linux-gnu/4.7/crtend.o:(.tm_clone_table+0x0): multiple definition of `__TMC_END__'
  35. simple-tun:(.data+0x10): first defined here
  36. /usr/bin/ld: error in simple-tun(.eh_frame); no .eh_frame_hdr table will be created.
  37. collect2: error: ld returned 1 exit status

我无法在搜索中找到解决方案.我的代码有什么根本原因吗?有人能帮助我发现我做错了吗?

如果我需要发布更多片段或更多输出,请告诉我.

解决方法

>您不能像这样使用PTHREAD_MUTEX_INITIALIZER – 它必须用作初始值设定项,而不是常规赋值表达式.您有两种方法可以解决它 – 调用pthread_mutex_init()或添加类型转换以使用PTHREAD_MUTEX_INITIALIZER作为复合文字.您的选择:
  1. pthread_mutex_init(&q->mutex,NULL);

要么:

  1. q->mutex = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;

>您的链接错误问题是由此命令行引起的:

  1. gcc simple-tun.c simple-tun -lpthread

你错过了-o,所以你试图将程序与自己联系起来.那是个坏消息.你可能想要的是:

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

实际上,你也应该在那里添加一些警告标志.

猜你在找的C&C++相关文章