Pages

Wednesday, June 27, 2007

Multi-threading in C in Linux




Today we shall talk about writing multi-threaded programming in C language in a Linux environment. Hope this will add value addition to all the novice, who are new to multi-threading in C, like me.

Here is my first multi-threaded C program, multi_thread1.c.

Step 1: Include these header files:
#include
#include
#include //for POSIX thread support

Step 2: The main program is here.
void *print_message_function( void *ptr );

main()
{
pthread_t thread1, thread2;
char *message1 = "Thread 1";
char *message2 = "Thread 2";
int iret1, iret2;

/* Create independent threads each of which will execute function */

iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);

/* Wait till threads are complete before main continues. Unless we */
/* wait we run the risk of executing an exit which will terminate */
/* the process and all threads before the threads have completed. */

pthread_join( thread1, NULL);
pthread_join( thread2, NULL);

printf("Thread 1 returns: %d\n",iret1);
printf("Thread 2 returns: %d\n",iret2);
exit(0);
}

void *print_message_function( void *ptr )
{
char *message;
message = (char *) ptr;
printf("%s \n", message);
}

Step 3: Compile this program

[kongkon@cadbury multi]$ gcc multi_thread1.c
/tmp/ccwbBUsp.o(.text+0x39): In function `main':
: undefined reference to `pthread_create'
/tmp/ccwbBUsp.o(.text+0x52): In function `main':
: undefined reference to `pthread_create'
/tmp/ccwbBUsp.o(.text+0x65): In function `main':
: undefined reference to `pthread_join'
/tmp/ccwbBUsp.o(.text+0x75): In function `main':
: undefined reference to `pthread_join'
collect2: ld returned 1 exit status
[kongkon@cadbury multi]$

Step 4: Got error, then just compile, do not link(with the library code).

[kongkon@cadbury multi]$ gcc -c multi_thread1.c
[kongkon@cadbury multi]$

Step 5: Good, got success. Now, why is it not building? The error says that
: undefined reference to `pthread_create'
meaning, there is a problem while linking with the library. Question number 1, have you provided the PATH to the library? Question number 2, what is the name of the library?
There is no issue with the path, because otherwise it won't even compile your hello world program, which just use stdio.h. So, the problem is the compiler does not know the library name. Hence the solution is :

[kongkon@cadbury multi]$ gcc -lpthread multi_thread1.c
[kongkon@cadbury multi]$ ls
a.out multi_thread1.c
[kongkon@cadbury multi]$


Step 6: Here you go! Run the program.
[kongkon@cadbury multi]$ ./a.out
Thread 1
Thread 2
Thread 1 returns: 0
Thread 2 returns: 0
[kongkon@cadbury multi]$

Bingo!
The first multi-threaded program in C in Linux environment ran successfully.

Let's introduce a sleep function after the thread creation, and before the thread join.
Now, see the output of the ps command, and what you will find is that, there will be two copies of a.out process. What does that mean?

No comments: