在C语言中,pthread_equal()函数用于比较两个线程ID是否相等
pthread_equal()函数用于检查两个线程是否相等。它返回0或非零值。对于相等的线程,它将返回非零值,否则返回0。该函数的语法如下:
int pthread_equal (pthread_t th1, pthread_t th2);登录后复制
示例
#include #include #include #include #include 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
"); } } 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 }