Home > Backend Development > C++ > In C language, the pthread_equal() function is used to compare whether two thread IDs are equal.

In C language, the pthread_equal() function is used to compare whether two thread IDs are equal.

WBOY
Release: 2023-09-22 16:29:02
forward
1250 people have browsed it

In C language, the pthread_equal() function is used to compare whether two thread IDs are equal.

pthread_equal() function is used to check whether two threads are equal. It returns 0 or non-zero value. It returns non-zero value for equal threads, 0 otherwise. The syntax of this function is as follows:

int pthread_equal (pthread_t th1, pthread_t th2);
Copy after login

Now let’s see what pthread_equal() actually does. In the first case, we check the self-thread to check the result.

Example

#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</p><p>");
   } else {
      printf("Threads are not equal</p><p>");
   }
}
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
}
Copy after login

Output

Threads are equal
Copy after login

Now if we compare between two different threads, we will see the result.

Example

#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</p><p>");
   } else {
      printf("Threads are not equal</p><p>");
   }
}

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);
}
Copy after login

Output

Threads are not equal
Copy after login

The above is the detailed content of In C language, the pthread_equal() function is used to compare whether two thread IDs are equal.. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template