Questo è il codice datomi dal mio prof:
#include <stdio.h> /*printf, perror*/
#include <pthread.h> /*thread*/
#include <errno.h> /*per gli errori, perror*/
#include <stdlib.h> /*exit*/
/* thread1: stampa i TID del main thread e di due altri thread */
void *start_func(void *arg) /* funzione di avvio */
{
printf("%s", (char *)arg);
printf(" and my TID is: %d\n",(int)pthread_self());
}
int main(void)
{
int en;
pthread_t tid1, tid2;
char *msg1 = "Hello world, I am thread #1";
char *msg2 = "Hello world, I am thread #2";
printf("The launching process has PID:%d\n",(int)getpid());
printf("The main thread has TID:%d\n",(int)pthread_self());
//crea il 1°thread
if ((en = pthread_create(&tid1, NULL, start_func, msg1)!=0))
errno=en, perror("Pthread_create"), exit(1);
//crea il 2°thread
if ((en = pthread_create(&tid2, NULL, start_func, msg2)!=0))
errno=en, perror("Pthread_create2"), exit(2);
//attende per il 1° thread
if ((en = pthread_join(tid1, NULL)!=0))
errno=en, perror("Pthread_join"), exit(1);
//attende per il 2° thread
if ((en = pthread_join(tid2, NULL)!=0))
errno=en, perror("Pthread_join2"), exit(2);
return 0;
}
************************************************** ***********************
Quando lo compilo con cc -c thread1.c non mi dà errori, ma quando voglio creare l'eseguibile con cc -o thread1 thread1.o mi dice:
thread1.o: In function `main':
thread1.c.text+0x97): undefined reference to `pthread_create'
thread1.c.text+0xee): undefined reference to `pthread_create'
thread1.c.text+0x136): undefined reference to `pthread_join'
thread1.c.text+0x17e): undefined reference to `pthread_join'
collect2: ld returned 1 exit status
come posso farlo compilare correttamente?