gcc编译期望')'之前';'标记,错误:预期表达式在'}'标记之前}

时间:2018-01-19 08:20:46

标签: c linux gcc pthreads

我从Linux pthread pdf文件中复制此代码。这段代码描述了客户和生产者的问题,演示使用链表来实现这个模型。  当我通过gcc编译时,显示此错误,我该如何解决?我尝试修改if( - > if()或pthread_mutex_lock(& mtx); - > pthread_mutex_lock(& mtx));在36行之前添加或删除},但不起作用。

<div class="gridCss"  style="page-break-before:always;"   >
    <br />
    <br />          
    <center CssClass="rotateText" > <asp:Label ID="MainLbl" runat="server"  />  &nbsp;и</center> <br />
    <center CssClass="rotateText" ><asp:Label ID="Lab1" runat="server" Text=""></asp:Label></center> <br />
    <br />
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="DSI" Width="100%"   >
        <Columns>
            <asp:BoundField DataField="one" HeaderText=""  SortExpression="1" HtmlEncode="True" />
            <asp:BoundField DataField="fio" HeaderText="two" SortExpression="2" />
            <asp:BoundField DataField="3" HeaderText="" SortExpression="3" />
            <asp:BoundField DataField="4" HeaderText="К4" SortExpression="4" />
            <asp:BoundField DataField="Date" HeaderText="5" SortExpression="Date" />
          ...
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="DSI" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectSta %>" SelectCommand="Command" SelectCommandType="StoredProcedure">
        <SelectParameters>
            <asp:SessionParameter Name="Sess" SessionField="Sess" Type="String" />

            <asp:Parameter DefaultValue="1" Name="st" Type="Int32" />
            <asp:Parameter DefaultValue="1" Name="nr" Type="String" />

        </SelectParameters>
    </asp:SqlDataSource>
    <br />
    <br />
    <br />
    <br />
    <tr>
        <td class="abz"/>
        <td align="right"> 
        <pre>        
            &#9;_______
            <asp:Label ID="Label_gv_2"        runat="server" Text="_____"></asp:Label>
            _______    __________________ &#9;Дата:___
            <asp:Label ID="Label_dt_2" runat="server" Text="_____"></asp:Label>
            ________ &#9;&#9;(ФИО)&#9;&#9;&#9;&#9;(подпись)<br />
        </pre>
</div>

2 个答案:

答案 0 :(得分:1)

您的代码存在很多问题。您需要了解编译器报告的内容,否则修复代码并不容易。我建议你从学习C的基础知识开始。

来自GCC编译器的错误:

xx.c: In function ‘thread_func’:
xx.c:28:29: error: expected ‘)’ before ‘;’ token
     pthread_mutex_lock(&mtx);
                             ^
xx.c:36:5: error: expected expression before ‘}’ token
     }
     ^
xx.c: In function ‘main’:
xx.c:53:1: error: unknown type name ‘be’
 be add lock/mutex
 ^
xx.c:53:8: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘lock’
 be add lock/mutex
        ^

很容易理解错误并解决问题。我建议你开始学习如何调试你的程序。

  1. 其他语法错误
  2. 该标志不是由任何人设置的,该变量的使用是徒劳的。我刚把它删除了。
  3. 了解pthreads的一些有用链接:

    1. Pthreads examples.
    2. Understanding GCC compiler error and warnings
    3. 更正后的代码

      在下面的代码中,我刚刚更正了编译器错误并删除了未使用的变量以便工作。

      我从您的问题中学到了什么,您使用了Linux书籍示例,在编辑器中复制并运行,并且您最终遇到了问题,所以我建议您学习基本的C语法并理解GCC警告和错误。< / p>

      #include <pthread.h>
      #include <unistd.h>
      #include <stdio.h>
      #include <malloc.h>
      
      static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
      static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
      
      struct node {
          int n_number;
          struct node *n_next;
      } *head = NULL;
      
      static void cleanup_handler(void *arg)
      {
          printf("Cleanup handler of second thread\n");
          free(arg);
          pthread_mutex_unlock(&mtx);
      }
      
      static void *thread_func(void *arg)   // customer
      {
          struct node *p = NULL;
      
          pthread_cleanup_push(cleanup_handler, p);
          while (1)
          {
          pthread_mutex_lock(&mtx);
          while (head == NULL){
            pthread_cond_wait(&cond,&mtx);
          }
          p = head;
          head = head->n_next;
          printf("Got %d from front of queue\n", p->n_number);
          free(p);
          pthread_mutex_unlock(&mtx);
          }
      
          pthread_exit(NULL);
          pthread_cleanup_pop(0); // must be put in last line
      }
      
      int main(void)
      {
          pthread_t tid;
          int i;
          struct node *p;
          pthread_create(&tid, NULL, thread_func, NULL);
          for (i = 0; i < 10; i++)  // producer
          {
          p = (struct node*)malloc(sizeof(struct node));
          p->n_number = i;
          // because head is share,visit share data must be add lock/mutex
          pthread_mutex_lock(&mtx);
          p->n_next = head;
          head = p;
          pthread_cond_signal(&cond);
          pthread_mutex_unlock(&mtx);
          sleep(1);
          }
          printf("thread 1 wanna end the line.So cancel thread 2.\n");
          pthread_cancel(tid);
          pthread_join(tid, NULL);
          printf("All done------exiting\n");
          return 0;
      }
      

答案 1 :(得分:0)

来自word文件的Corrent代码。书中的PDF代码是培训机构数据,我认为员工/教师编辑错误。

#include <pthread.h> 
#include <unistd.h>
#include <stdio.h>
#include <malloc.h>    // 1.work on Linux  
                       // 2. error on OS X,fatal error:'malloc.h' file not 
                       //   found #include <malloc.h> ^~~~~~~~~~1 error 
                       // generated.

#include <stdlib.h>    // work on OS X      

static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
struct node {
        int n_number;
        struct node *n_next;
} *head = NULL;

static void cleanup_handler(void *arg)
{
    printf("Cleanup handler of second thread\n");
    free(arg);
    pthread_mutex_unlock(&mtx);
}

static void *thread_func(void *arg)// customer
{
    struct node *p = NULL;
    pthread_cleanup_push(cleanup_handler, p);
    while (1)
    {
        pthread_mutex_lock(&mtx);
        while (head == NULL){ 
            pthread_cond_wait(&cond,&mtx);
      }
        p = head;
        head = head->n_next;
        printf("Got %d from front of queue\n", p->n_number);
        free(p);
        pthread_mutex_unlock(&mtx);
    }
    pthread_exit(NULL);
    pthread_cleanup_pop(0);   //must be put in last line 
}
int main(void)
{
    pthread_t tid;
    int i;
    struct node *p;
    pthread_create(&tid, NULL, thread_func, NULL);
    for (i = 0; i < 10; i++) // producer
    {
            p = (struct node*)malloc(sizeof(struct node));
            p->n_number = i;
            pthread_mutex_lock(&mtx);// because head is share,visit share data
                                     // must be add lock/muck
            p->n_next = head;
            head = p;
            pthread_cond_signal(&cond);
            pthread_mutex_unlock(&mtx);
            sleep(1);
    }
    printf("thread 1 wanna end the line.So cancel thread 2.\n");
    pthread_cancel(tid);
    pthread_join(tid, NULL);
    printf("All done------exiting\n");
    return 0;
}