ÇPthread互斥:前`{'预计前pression [英] C pthread mutex: Expected expression before `{'

查看:171
本文介绍了ÇPthread互斥:前`{'预计前pression的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的pthread库创建两个线程。我使用的两个队列通信的两个线程之间的数据(生产者 - 消费者的事情),因此希望有一个互斥体的线程同步队列推持久性有机污染物。

我现在面临的问题是,我得到一个编译错误如下:

  $ GCC简单tun.c简单屯-lpthread
简单tun.c:在函数'NEW_QUEUE:
简单tun.c:920:13:错误:在{令牌预计前pression

在那里我得到错误的功能是:

  908结构队列* NEW_QUEUE(){
909
910结构队列* Q;
911 Q =(结构队列*)malloc的(的sizeof(结构队列));
912
如果913(Q == NULL)
914返回NULL;
915
916
917 Q->头= NULL;
918 Q->尾= NULL;
919 Q-> is_empty = 1;
920 Q->互斥量= PTHREAD_MUTEX_INITIALIZER;
921
922回q;
923}

结构队列:

 结构队列{
 80结构节点*头;
 81结构节点*尾;
 82 INT is_empty;
 83 pthread_mutex_t互斥;
 84};

如果我注释掉线920,连接器开始给'多申报错误

  $ GCC简单tun.c简单屯-lpthread
简单-TUN:在功能`settun:
(+的.text 0x2b7):`settun的多重定义
/tmp/cc5Ms4xP.o:simple-tun.c:(.text+0x1cb):首先这里定义
简单-TUN:在功能`_fini:
(调用.fini +为0x0):多重定义`_fini
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crti.o:(.fini+0x0):首先这里定义
简单-TUN:在功能`mktun:
(+的.text 0x1e2):`mktun的多重定义
/tmp/cc5Ms4xP.o:simple-tun.c:(.text+0xf6):首先这里定义
简单-TUN:在功能`net_connect:
(+的.text 0xe27):`net_connect的多重定义
/tmp/cc5Ms4xP.o:simple-tun.c:(.text+0x1115):首先这里定义
简单-TUN:在功能`DATA_START:
(。数据+为0x0):`__data_start的多重定义
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crt1.o:(.data+0x0):首先这里定义
简单-TUN:在功能`client_connect:
(+的.text 0xe95):`client_connect的多重定义
/tmp/cc5Ms4xP.o:simple-tun.c:(.text+0x1183):首先这里定义
简单-TUN:在功能`DATA_START:
(。数据+ 0x8中):`__dso_handle的多重定义
/usr/lib/gcc/x86_64-linux-gnu/4.7/crtbegin.o:(.data+0x0):首先这里定义
简单-TUN :( rodata +为0x0):`的_IO_stdin_used'多重定义
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crt1.o:(.rodata.cst4+0x0):首先这里定义
简单-TUN:在功能`server_connect:
(+的.text 0xfa2):`server_connect的多重定义
/tmp/cc5Ms4xP.o:simple-tun.c:(.text+0x1290):首先这里定义
简单-TUN:在功能`print_usage:
(+的.text 0xe05):`print_usage的多重定义
/tmp/cc5Ms4xP.o:simple-tun.c:(.text+0x10f3):首先这里定义
简单-TUN:在功能`_init:
(.init +为0x0):`_init的多重定义
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crti.o:(.init+0x0):首先这里定义
/usr/lib/gcc/x86_64-linux-gnu/4.7/crtend.o:(.tm_clone_table+0x0):`__TMC_END__的多重定义
。简单-TUN :(数据+ 0×10):首先这里定义
在/ usr /斌/劳工处:错误简单-TUN(.eh_frame);没有.eh_frame_hdr表将被创建。
collect2:错误:LD返回1退出状态

我无法找到我的搜索解决方案。是不是有什么根本性的错误与我的code?有人能帮助我察觉什么,我做错了什么?

请让我知道如果我需要发布更多的片段或多个输出。

非常感谢。


解决方案

  1. 您不能使用 PTHREAD_MUTEX_INITIALIZER 这样的 - 它被用作初始化,而不是在常规任务前pression。你有两个选择来解决它 - 要么调用调用pthread_mutex_init(),或添加类型转换使用 PTHREAD_MUTEX_INITIALIZER 作为一个复合文字。您所选择的:

     调用pthread_mutex_init(安培; Q->互斥,NULL);

      Q->互斥量=(pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;


  2. 您链接错误问题是由于此命令行:

      GCC简单tun.c简单屯-lpthread

    您错过一个 -o ,所以你想程序与自身联系起来。这是个坏消息。你可能想要的是:

      GCC简单tun.c -o简单屯-lpthread

    和真的,你应该添加在那里一些警告标志了。


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

The problem I am facing is, 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 the 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 spotting what I am doing wrong?

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

Thanks a lot.

解决方案

  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.

这篇关于ÇPthread互斥:前`{'预计前pression的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆