Threads: Light-weight Processes (LWP); mostly useful if created in user-space.
Program:
#include <stdio.h>
#include <pthread.h>
int gvar;
void* t1_fun(void *msg)
{
int i;
for(i = 0; i < 10; i++)
{
gvar++;
/*printf("[%d:%s] : gvar = %d.\n", pthread_self(), (char*) msg, gvar);*/
printf("[%d:%s] : gvar = %d.\n", (long int)syscall(224), (char*) msg, gvar);
}
}
int main()
{
pthread_t t1, t2;
char *msg1 = "thread1";
char *msg2 = "thread2";
int rc1 = pthread_create(&t1, NULL, t1_fun, (void *) msg1);
if( rc1 ) { printf("Error for thread 1.\n"); perror("pthread_create"); }
int rc2 = pthread_create(&t2, NULL, t1_fun, (void *) msg2);
if( rc2 ) { printf("Error for thread 1.\n"); perror("pthread_create"); }
printf("Threads execution over.\n");
return 0;
}
Points to Note:
1. The function prototype of the thread should be
void * fun( void * args);
2. Single arguments (like above) can be directly passed after casting to "void *"; where as
multiple arguments can be passed in the form of structures.
3. To print the LWP (thread) ID, I am using the method
(long int) syscall (224) ==> This is the system call for "gettid" system call.
And note, pthread_self() returns "pthread_t"; and not the "thread id".
4. And for both the threads, I can give the same function (t1_fun).
5. Format of pthread_create: pthread_create(&t1, <thread_attr>, <function>, <function_args>)
Program Output:
$> gcc sample.c -lpthread
$> ls
a.out sample.c
$> ./a.out
Threads execution over.
[5164:thread1] : gvar = 1.
[5164:thread1] : gvar = 2.
Why such output?:
The reason is, we have "just triggered the threads and did not bother about them after that".
To make sure, we get all the printfs of both the threads, the main thread should "wait" for those two threads to "join" it.
Hence, if you provide "pthread_join" call for both the threads, you main thread will not exit UNTIL both the threads completes its execution.
...
...
/* If the 2nd arg is NOT NULL, the return value of t1 and t2 will be stored there.
* This pthread_join will suspend the main thread until the t1/t2 completes, terminates or by calling
* pthread_exit.
*/
pthread_join(t1, NULL);
pthread_join(t2, NULL);
Why such output?:
The reason is, we have "just triggered the threads and did not bother about them after that".
To make sure, we get all the printfs of both the threads, the main thread should "wait" for those two threads to "join" it.
Hence, if you provide "pthread_join" call for both the threads, you main thread will not exit UNTIL both the threads completes its execution.
...
...
int rc2 = pthread_create(&t2, NULL, t1_fun, (void *) msg2);
if( rc2 ) { printf("Error for thread 1.\n"); perror("pthread_create"); }
/* If the 2nd arg is NOT NULL, the return value of t1 and t2 will be stored there.
* This pthread_join will suspend the main thread until the t1/t2 completes, terminates or by calling
* pthread_exit.
*/
pthread_join(t1, NULL);
pthread_join(t2, NULL);
printf("Threads execution over.\n");
return 0;
}
Program Output:
$> gcc sample.c -lpthread
$> ls
a.out sample.c
$> ./a.out
[5164:thread1] : gvar = 1.
[5164:thread1] : gvar = 2.
[5164:thread1] : gvar = 3.
[5164:thread1] : gvar = 4.
...
...
[5164:thread1] : gvar = 5.
[5164:thread1] : gvar = 6.
...
...
[5165:thread2] : gvar = 19.
[5165:thread2] : gvar = 20.
Nice post Sir. It really helps a beginner like me. Thanks for being my teacher.
ReplyDelete