
pthread_equal()函数用于检查两个线程是否相等。它返回0或非零值。对于相等的线程,它将返回非零值,否则返回0。该函数的语法如下:
int pthread_equal (pthread_t th1, pthread_t th2);
现在让我们看看 pthread_equal() 的实际作用。第一种情况,我们会检查自线程来检查结果。
示例
#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"); } else { printf("Threads are not 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}
输出
Threads are equal
现在,如果我们在两个不同的线程之间进行比较,我们将看到结果。
示例
#include #include #include #include #include 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"); } else { printf("Threads are not equal
"); }}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
以上就是在C语言中,pthread_equal()函数用于比较两个线程ID是否相等的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1445596.html
微信扫一扫
支付宝扫一扫