2

I have used the following code to create two threads:

//header files
#include <pthread.h>
struct thread_arg
{
    int var1;
    int var2;
};
void *serv_com(void *pass_arg)
{
    struct thread_arg *con = pass_arg;
    //required statements irrelevant to the issue
    pthread_exit(NULL);
}
void *cli_com(void *pass_arg)
{
    struct thread_arg *con = pass_arg;
    //required statements irrelevant to the issue
    pthread_exit(NULL);
}
int main()
{
    pthread_t inter_com;
    //necessary code
    while(1)
    {
        th_err_s = pthread_create(&inter_com, NULL, serv_com, (void *)&pass_arg);
        th_err_c = pthread_create(&inter_com, NULL, cli_com, (void *)&pass_arg);
        if (th_err_s || th_err_c)
        {
            printf("Alert! Error creating thread! Exiting Now!");
            exit(-1);
        }
    }
    pthread_exit(NULL);
    return 1;
}

Then I compiled the above code in linux using the following command:

gcc -o sample sample.c

It returned the following error message:

inter.c:(.text+0x374): undefined reference to `pthread_create'
inter.c:(.text+0x398): undefined reference to `pthread_create'
collect2: ld returned 1 exit status

What should I do to correctly compile this file. I am sure it is no syntax error or anything since when I commented off everything inside the while loop, the program was compiling correctly and I verified that the pthread_create syntax is correct. Do I have to issue some other command to compile the file?

EDIT: Is there any problem with the creating of two threads in the above code? The program is just exiting with the error message once it is running. What can be the possible issue and how can I solve it? Thanks in advance.

Harikrishnan
  • 7,765
  • 13
  • 62
  • 113
  • possible duplicate of [Undefined reference to 'pthread_create'](http://stackoverflow.com/questions/9253200/undefined-reference-to-pthread-create) – Ozair Kafray May 18 '12 at 10:53
  • possible duplicate of [undefined reference to pthread_create in linux (c programming)](http://stackoverflow.com/questions/1662909/undefined-reference-to-pthread-create-in-linux-c-programming) – Mat May 18 '12 at 10:58

3 Answers3

4

Try doing this :

gcc -lpthread sample.c

or

gcc -pthread sample.c

above 2 commands will directly create executable a.out

Answer after edit:

1) Wait for the two threads to join main thread using call

int pthread_join(pthread_t thread, void **value_ptr);

2) Create both threads with different ids

3) Also avoid calling pthread_exit from main() if you can, although there is no harm doing that

4) you are calling pthread_create in while(1) this will create infinite threads .. I do not know what are you trying to achieve .

nav_jan
  • 2,473
  • 4
  • 24
  • 42
  • There is no other threads. These are two independent threads. – Harikrishnan May 18 '12 at 11:46
  • These two independent threads exits when main thread exits so avoid exiting from main thread unless theses two thread have completed their job i will edit my answer – nav_jan May 18 '12 at 11:50
  • a small doubt in that answer. I am calling the two threads from inside a `while(1)` and the `pthread_exit()` from outside it. Then how can the `pthread_exit()` can affect the functioning of the `pthread_create()`? – Harikrishnan May 18 '12 at 12:04
2

Link to pthread Library when compiling...

gcc -o sample -lpthread sample.c

Anerudhan Gopal
  • 379
  • 4
  • 13
0

I am not too sure myself but I would think you could do something like

pthread_t inter_com, inter_com2;

and

th_err_s = pthread_create(&inter_com, NULL, serv_com, (void *)&pass_arg);
        th_err_c = pthread_create(&inter_com2, NULL, cli_com, (void *)&pass_arg);

I think it should give you 2 ids of threads. But careful when sharing variables etc between threads. But am glad you resolved it yourself.

James Smith
  • 73
  • 10