Thai Huynh
Thai Huynh

Reputation: 67

gcc -pthread no such fiile or directory ex_thread_creation

I'm trying to print infinite lines "Hello, how are you", "I'm fine, and you"

first I use command "vim ex_thread_creation" then I enter following the code:

#include <pthread.h> 
#include <stdio.h> 
void *thread_print(void * messenge)
{    
    while(1) {          
    printf("Hello, How are you?\n");  
             }  
}  

int main() 
{    
pthread_t idthread;     
pthread_create( &idthread,NULL, &thread_print, NULL);   
while(1) { 
         printf("I’m fine, and you?\n");  
         }     
return 0;  
} 

then I use gcc ex_thread_creation.c -pthread ex_thread_creation then I meet the error: no such file or directory ex_thread_creation.

someone help me, please

edit 1: after I change -pthread to -o I meet another error: ex_thread_creation.c:(.text+0x4a): undefined reference to pthread_create collect2: error: old returned 1 exit status

Upvotes: 0

Views: 2749

Answers (2)

Thai Huynh
Thai Huynh

Reputation: 67

After research a bit longer, I've found the correct command is:

gcc -pthread -o term term.c

Upvotes: 2

DanBrezeanu
DanBrezeanu

Reputation: 552

There are several problems with your approach.

Regarding your gcc issue, the correct command would be:

gcc ex_thread_creation.c -o ex_thread_creation -lpthread

The -o flag stands for outfile or the file resulted from compiling the source file.

Regarding your code there are several issues too other than your code indentation.

The lines "Hello, how are you" and "I'm fine, and you" might not appear one after the other as intended. The output may appear malformed (the lines being intertwined) or the order could be wrong (Hello -> Hello -> I'm fine -> I'm fine).

If you want to keep the output as intended in your question, I suggest you use synchronization mechanisms.

Upvotes: 2

Related Questions