Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C

pthread_equal() ใน C


pthread_equal() ตรวจสอบว่า thread สองตัวเท่ากันหรือไม่ คืนค่า 0 หรือ non-zero เท่ากันคืน non-zero ไม่เท่ากันคืน 0 ไวยากรณ์:

int pthread_equal(pthread_t th1, pthread_t th2);

ดู pthread_equal() ในการทำงาน กรณีแรก เช็ค self-thread

ตัวอย่าง

#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())) { printf("Threads are equal\n"); } else { printf("Threads are not equal\n"); } } main() { pthread_t th1; sample_thread = th1; pthread_create(&th1, NULL, my_thread_function, NULL); pthread_join(th1, NULL); }

ผลลัพธ์

Threads are equal

กรณีเปรียบเทียบ 2 threads ต่างกัน

ตัวอย่าง

#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(); } void* my_thread_function2(void* p) { if(pthread_equal(sample_thread, pthread_self())) { 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); pthread_create(&th2, NULL, my_thread_function2, NULL); pthread_join(th1, NULL); pthread_join(th2, NULL); }

ผลลัพธ์

Threads are not equal