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 वर्तमान थ्रेड आईडी लौटाएगा
printf("थ्रेड्स समान हैं\n");
} else {
printf("थ्रेड्स समान नहीं हैं\n");
}
}
int main() {
pthread_t th1;
sample_thread = th1; // थ्रेड th1 को दूसरे थ्रेड ऑब्जेक्ट में असाइन करें
pthread_create(&th1, NULL, my_thread_function, NULL); // my_thread_function का उपयोग करके थ्रेड बनाएं
pthread_join(th1, NULL); // मुख्य थ्रेड के साथ जुड़ने के लिए थ्रेड की प्रतीक्षा करें
}
आउटपुट
थ्रेड्स समान हैं
अब हम परिणाम देखेंगे यदि हम दो अलग-अलग थ्रेड्स के बीच तुलना करते हैं।
उदाहरण
#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(); // थ्रेड 1 की आईडी असाइन करें
}
void* my_thread_function2(void* p) {
if (pthread_equal(sample_thread, pthread_self())) { // pthread_self वर्तमान थ्रेड आईडी लौटाएगा
printf("थ्रेड्स समान हैं\n");
} else {
printf("थ्रेड्स समान नहीं हैं\n");
}
}
int main() {
pthread_t th1, th2;
pthread_create(&th1, NULL, my_thread_function1, NULL); // my_thread_function1 का उपयोग करके थ्रेड बनाएं
pthread_create(&th2, NULL, my_thread_function2, NULL); // my_thread_function2 का उपयोग करके थ्रेड बनाएं
pthread_join(th1, NULL); // मुख्य थ्रेड के साथ जुड़ने के लिए थ्रेड की प्रतीक्षा करें
pthread_join(th2, NULL);
}
आउटपुट
थ्रेड्स समान नहीं हैं