I am trying to run the following simple program which uses multithreading using pthreads. I compile it like: gcc -lpthread programName.c and it compiles fine. However, when I run it gives errors which I have pasted at the bottom of program.

#include<pthread.h>
#include<stdio.h>

void *thread_routine(void* arg)
{
    printf("Inside newly created thread \n");
}

int main(int argc, char* argv[])
{
    pthread_t thread_id;
    void *thread_result;
    pthread_create(&thread_id, NULL, thread_routine, NULL);
    printf("Inside main thread \n");
    pthread_join(thread_id, &thread_result);
}

-----------------------
Errors:
---------------------

/2.c: line 4: syntax error near unexpected token `('
./2.c: line 4: `void *thread_routine(void* arg)'

Recommended Answers

All 4 Replies

>>I compile it like: gcc -lpthread programName.c and it compiles fine

How can you say that and then post the error messages that your compiler gave you???

Try reversing the order of the two includes. There might be something in pthread.h that depends on stdio.h but just a guess.

>>I compile it like: gcc -lpthread programName.c and it compiles fine

How can you say that and then post the error messages that your compiler gave you???

Try reversing the order of the two includes. There might be something in pthread.h that depends on stdio.h but just a guess.

Thanks for your reply "Ancient Dragon". I was able to run this program recently. Actually, I was trying to run the sources code. A binary file is to be run rather than the source code. Just for sake of information of readers, the correct way to do is:

cc -o progName -lpthread programName.c

./progName

Well, in thread_routine(), you have declared it to return a void*, but it returns nothing. However, since that is not the error you are getting, it problem is likely, as Ancient Dragon noted, that there is an issue with the include files. However, you state that your error is a runtime one, but that is inconsistent with the information provided. The errors shown are compile-time errors, not run-time errors.

Well, in thread_routine(), you have declared it to return a void*, but it returns nothing. However, since that is not the error you are getting, it problem is likely, as Ancient Dragon noted, that there is an issue with the include files. However, you state that your error is a runtime one, but that is inconsistent with the information provided. The errors shown are compile-time errors, not run-time errors.

This problem is already solved and this program runs now. Actually, I was trying to run the source code. see my 2nd posting on this page for more details. Thanks all for their input.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.