Visualizzazione dei risultati da 1 a 6 su 6

Discussione: [C++] Thread in Unix

  1. #1
    Utente di HTML.it
    Registrato dal
    Jun 2007
    Messaggi
    80

    Thread in Unix

    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?

  2. #2
    Utente di HTML.it L'avatar di ibykos
    Registrato dal
    Feb 2005
    Messaggi
    201
    prova a compilare linkando la libreria pthread.h a "mano", aggiungendo l'opzione -lpthread

    gcc ... -lpthread

  3. #3
    Moderatore di Programmazione L'avatar di alka
    Registrato dal
    Oct 2001
    residenza
    Reggio Emilia
    Messaggi
    24,466

    Moderazione

    Il linguaggio va indicato anche nel titolo, come descritto nel Regolamento.

    Qui l'ho aggiunto io.
    MARCO BREVEGLIERI
    Software and Web Developer, Teacher and Consultant

    Home | Blog | Delphi Podcast | Twitch | Altro...

  4. #4
    Utente di HTML.it
    Registrato dal
    Jun 2007
    Messaggi
    80
    Scusa, non lo sapevo, starò più attento!!!!

  5. #5
    Utente di HTML.it
    Registrato dal
    Jun 2007
    Messaggi
    80
    ok!! funziona!! come mai si deve inserire questa opzione?

  6. #6
    Utente di HTML.it
    Registrato dal
    Jun 2007
    Messaggi
    80
    ora invece esercitandomi sulle code di messaggi in Linux mi dà un errore simile:
    mycoda1.o: In function `main':
    mycoda1.c.text+0xb2): undefined reference to `mq_open'
    mycoda1.c.text+0xe5): undefined reference to `mq_getattr'
    mycoda1.c.text+0x163): undefined reference to `mq_send'
    mycoda1.c.text+0x19e): undefined reference to `mq_getattr'
    mycoda1.c.text+0x1d9): undefined reference to `mq_close'
    collect2: ld returned 1 exit status

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.