gpt4 book ai didi

linux - Pthread 程序返回预期的声明说明符或 â&â 标记之前的 â...â

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:29:21 25 4
gpt4 key购买 nike

linux 中编译以下 pthreads 程序时,我多次遇到相同的错误:

gcc -c -lpthread proj2_part1.c -lrt

    #include <unistd.h>     
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <semaphore.h>
#define BUFFER_SIZE 10
#define TRUE 1

struct cQueue{
int *buffer;
int front, rear;
}*queue;

int count;
pthread_mutex_t mutex;
sem_t full, empty;

........
/*MAIN*/
int main(int argc,char* argv[])
{
int a=0, b=0, count =0, buff[BUFFER_SIZE];
int p = atoi(argv[1]), c = atoi(argv[2]);
unsigned int s = atoi(argv[0]);
pthread_t prothread[p], conthread[c];

/*Initialize Semaphores*/
if (sem_init(&full,0,0) == -1)
printf("%s\n",strerror(errno));

if (sem_init(&empty,0,BUFFER_SIZE) == -1)
printf("%s\n",strerror(errno));

pthread_mutex_init(&mutex, NULL);

/*Initialize Circular Queue*/
queue->buffer=buff;
queue->front = queue->buffer[0];
queue->rear = queue->buffer[0];

if(argc!=3)
{
fprintf(stderr,"Syntax: ./a.out <int> <int> <int>");
return -1;
}
if(s<0)
{
fprintf(stderr,"Argument %d must be positive value\n",s);
return -1;
}
else
{
/*Create producer threads*/
int i;
for (i=0; i<p; i++)
{
b = pthread_create(&prothread[i], NULL, producerThread, (void*)argv[1]);
if (b<0)
{
printf("Error: unable to create thread, %d\n",b);
return -1;
}
}
/*Create consumer threads*/
int j;
for (j=0; j<c; j++)
(
a = pthread_create(&conthread[j], NULL, consumerThread, (void*)argv[2]);
if (a<0)
{
printf("Error: unable to create thread, %d\n",a);
return -1;
}
}
sleep(atoi(argv[0]));
}
return 0;
}

我收到以下错误。我认为这与我的信号量声明有关。

    proj2_part1.c:147:81: error: expected â)â before â;â token
a = pthread_create(&conthread[j], NULL, consumerThread, (void*)argv[2]);
^
proj2_part1.c:153:6: error: expected â;â before â}â token}
^
proj2_part1.c: At top level:
proj2_part1.c:156:6: error: expected identifier or â(â before âreturnâ
return 0;
^
proj2_part1.c:157:1: error: expected identifier or â(â before â}â token
}

最佳答案

你使用了 ( 你的意思是 {:

for (j=0; j<c; j++)
(

关于linux - Pthread 程序返回预期的声明说明符或 â&â 标记之前的 â...â,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29933953/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com