Pthread runtime error

I tried running a simple Pthread program. It compiled fine but gave a runtime error. I don't know how to fix it. It says error in line 4 . However, line 4 sounds fine to me. Can someone help me?


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

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

void main()
{
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);
}

----------------------------------------------------------
Error:-
--------------------------

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#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);
}


seems to work for me...
Did you try to run it? The errors I mentioned above occur during runtime. When I compile this program using "cc -lpthread programName.c " (I am using linux) , it compiles fine. but when I try to run it, then it gives errors.


----------------------------------------------------------
Error:-
--------------------------

./2.c: line 4: syntax error near unexpected token `('
./2.c: line 4: `void *thread_routine(void* arg)'
Last edited on
Those don''t look like run time errors. It looks to me like you might be trying to run the source code. After compiling there should be a binary file that you should run instead. You can specify the name of that file in your compile command:
1
2
3
cc -o progName -lpthread programName.c

./progName


Do not try to run progName.c
Last edited on
Yes, it works now. You are right, I was trying to run the source code. Thanks a lot for your help. I really appreciate this forum as a great tool to learning. Thanks again.
Topic archived. No new replies allowed.