Salve,compilando il codice che segue ottengo il seguente errore:
error: expected expression before ‘{’ token
Avete suggerimenti?Grazie.
codice:
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
void *prod_rtn(void *);
void *cons_rtn(void *);
struct data{
int a;
pthread_mutex_t mutex;
};
int main(){
pthread_t prod;
pthread_t cons;
struct data *arg;
int err;
if( (arg=(struct data*)malloc(sizeof(struct data))) == NULL){
perror("malloc() error");
exit(1);
}
arg->a=0;
arg->mutex=PTHREAD_MUTEX_INITIALIZER;
if( (err=pthread_create(&prod,NULL,prod_rtn,(void*)arg)) != 0){
printf("pthread_create() error %s\n",strerror(err));
exit(1);
}
if( (err=pthread_create(&cons,NULL,cons_rtn,(void*)arg)) != 0){
printf("pthread_create() error %s\n",strerror(err));
exit(1);
}
while(1){
if( arg->a > 10)
break;
else
printf("Master thread %d\n",arg->a);
}
}
void *prod_rtn( void* arg){
int err;
struct data *args=(struct data*)arg;
if( (err=pthread_mutex_lock(&(args->mutex))) != 0){
printf("pthread_mutex_lock() error %s\n",strerror(err));
exit(1);
}
printf("PRODUTTORE\n");
args->a=(args->a)+2;
if( (err=pthread_mutex_unlock(&(args->mutex))) != 0){
printf("pthread_mutex_lock() error %s\n",strerror(err));
exit(1);
}
}
void *cons_rtn( void* arg){
int err;
struct data *args=(struct data*)arg;
if( (err=pthread_mutex_lock(&(args->mutex))) != 0){
printf("pthread_mutex_lock() error %s\n",strerror(err));
exit(1);
}
printf("CONSUMATORE\n");
args->a=(args->a)+1;
if( (err=pthread_mutex_unlock(&(args->mutex))) != 0){
printf("pthread_mutex_lock() error %s\n",strerror(err));
exit(1);
}
}