- Κώδικας: Επιλογή όλων
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
int a=0;
pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
void *mythread(void *string)
{
int arithmos,i,rc,local,found,thesi;
int A[50];
for (i=0; i<10; i++)
{
rc=pthread_mutex_lock(&mutex);
local=a;
sleep(1);
if(A[i]==arithmos)
{
found=1;
break;
}
if(found==1)
{
thesi=i+1;
printf("\n The element has been found at position %d!",thesi);
}
else
printf("\n %d was not found!",arithmos);
local=local+1;
a=local;
rc=pthread_mutex_unlock(&mutex);
}
int main ()
{
pthread_t thread1;
pthread_t thread2;
pthread_t thread3;
pthread_t thread4;
pthread_t thread5;
int rc,i;
long t;
int arithmos;
int A[50];
printf("Enter 50 values\n");
for (i=0;i<50;i++)
scanf("%d",&A[i]);
printf("enter the value for serial search in array\n");
scanf("%d",&arithmos);
for(t=0; t<5; t++) {
printf("Main: creating thread %ld\n", t);
rc = pthread_create(&thread[t], &attr,mythread, (void *)t);
if (rc) {
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
for(t=0; t<5; t++) {
rc = pthread_join(thread[t],NULL);
if (rc) {
printf("ERROR; return code from pthread_join() is %d\n", rc);
exit(-1);
}
}
printf("Main finished. a=%d\n",a);
pthread_exit(NULL);
}
}
τα error ειναι:
[color=#FF0000]askisi3.c: In function ‘main’:
askisi3.c:61: error: ‘thread’ undeclared (first use in this function)
askisi3.c:61: error: (Each undeclared identifier is reported only once
askisi3.c:61: error: for each function it appears in.)
askisi3.c:61: error: ‘attr’ undeclared (first use in this function)[/color]