pthread_equal() in C with example
Last Updated :
14 Oct, 2019
Prerequisite : Multithreading, pthread_self() in C with Example
pthread_equal() = This compares two thread which is equal or not. This function compares two thread identifiers. It return ‘0’ and non zero value. If it is equal then return non zero value else return 0.
Syntax:- int pthread_equal (pthread_t t1, pthread_t t2);
First Method :- Compare with self thread
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <pthread.h>
pthread_t tmp_thread;
void * func_one( void * ptr)
{
if (pthread_equal(tmp_thread, pthread_self())) {
printf ( "equal\n" );
} else {
printf ( "not equal\n" );
}
}
int main()
{
pthread_t thread_one;
tmp_thread = thread_one;
pthread_create(&thread_one, NULL, func_one, NULL);
pthread_join(thread_one, NULL);
}
|
Output:
equal
Second Method :- Compare with other thread
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <pthread.h>
pthread_t tmp_thread;
void * func_one( void * ptr)
{
tmp_thread = pthread_self();
}
void * func_two( void * ptr)
{
pthread_t thread_two = pthread_self();
if (pthread_equal(tmp_thread, thread_two)) {
printf ( "equal\n" );
} else {
printf ( "not equal\n" );
}
}
int main()
{
pthread_t thread_one, thread_two;
pthread_create(&thread_one, NULL, func_one, NULL);
pthread_create(&thread_two, NULL, func_two, NULL);
pthread_join(thread_one, NULL);
pthread_join(thread_two, NULL);
}
|
Output:
not equal
Share your thoughts in the comments
Please Login to comment...