clab/c/pthreadex6.c
2024-06-28 09:09:03 +01:00

40 lines
1 KiB
C

#include <pthread.h>
#include <stdio.h>
void *thread_func(void *arg) {
pthread_t thread_id = pthread_self();
printf("Inside thread_func, thread ID = %ld\n", thread_id);
// Return the thread ID as the thread's return value
return (void *)thread_id;
}
int main() {
pthread_t thread1, thread2;
int thread1_arg = 10, thread2_arg = 20;
// Create two threads
pthread_create(&thread1, NULL, thread_func, &thread1_arg);
pthread_create(&thread2, NULL, thread_func, &thread2_arg);
void *thread1_result, *thread2_result;
// Wait for the threads to finish
pthread_join(thread1, &thread1_result);
pthread_join(thread2, &thread2_result);
// Compare the thread IDs
if (pthread_equal(thread1, thread2)) {
printf("Thread IDs are equal\n");
} else {
printf("Thread IDs are not equal\n");
}
// Print the thread IDs and return values
printf("Thread 1 ID = %ld, return value = %ld\n", thread1,
(long)thread1_result);
printf("Thread 2 ID = %ld, return value = %ld\n", thread2,
(long)thread2_result);
return 0;
}