pthread_equal() ফাংশন দুটি থ্রেড সমান কি না তা পরীক্ষা করতে ব্যবহৃত হয়। এটি 0 বা অ-শূন্য মান প্রদান করে। সমান থ্রেডের জন্য, এটি অ-শূন্য প্রদান করবে, অন্যথায় এটি 0 প্রদান করবে। এই ফাংশনের সিনট্যাক্স নিচের মত -
int pthread_equal (pthread_t th1, pthread_t th2);
এখন চলুন pthread_equal() কে অ্যাকশনে দেখা যাক। প্রথম ক্ষেত্রে, আমরা ফলাফল পরীক্ষা করার জন্য স্ব-থ্রেড পরীক্ষা করব।
উদাহরণ
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <pthread.h>
pthread_t sample_thread;
void* my_thread_function(void* p) {
if (pthread_equal(sample_thread, pthread_self())) { //pthread_self will return current thread id
printf("Threads are equal\n");
} else {
printf("Threads are not equal\n");
}
}
main() {
pthread_t th1;
sample_thread = th1; //assign the thread th1 to another thread object
pthread_create(&th1, NULL, my_thread_function, NULL); //create a thread using my thread function
pthread_join(th1, NULL); //wait for joining the thread with the main thread
} আউটপুট
Threads are equal
এখন আমরা ফলাফল দেখতে পাব, যদি আমরা দুটি ভিন্ন থ্রেডের মধ্যে তুলনা করি।
উদাহরণ
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <pthread.h>
pthread_t sample_thread;
void* my_thread_function1(void* ptr) {
sample_thread = pthread_self(); //assign the id of the thread 1
}
void* my_thread_function2(void* p) {
if (pthread_equal(sample_thread, pthread_self())) { //pthread_self will return current thread id
printf("Threads are equal\n");
} else {
printf("Threads are not equal\n");
}
}
main() {
pthread_t th1, th2;
pthread_create(&th1, NULL, my_thread_function1, NULL); //create a thread using my_thread_function1
pthread_create(&th1, NULL, my_thread_function2, NULL); //create a thread using my_thread_function2
pthread_join(th1, NULL); //wait for joining the thread with the main thread
pthread_join(th2, NULL);
} আউটপুট
Threads are not equal