কম্পিউটার

pthread_self() সি তে


এখানে আমরা সি-তে pthread_self() এর প্রভাব কী হবে তা দেখব। বর্তমান থ্রেডের আইডি পেতে pthread_self() ফাংশন ব্যবহার করা হয়। এই ফাংশনটি বিদ্যমান থ্রেডগুলিকে অনন্যভাবে সনাক্ত করতে পারে। কিন্তু যদি একাধিক থ্রেড থাকে এবং একটি থ্রেড সম্পূর্ণ হয়, তাহলে সেই আইডিটি পুনরায় ব্যবহার করা যেতে পারে। তাই সমস্ত চলমান থ্রেডের জন্য, আইডিগুলি অনন্য।

উদাহরণ

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void* func(void* p) {
   printf("From the function, the thread id = %d\n", pthread_self()); //get current thread id
      pthread_exit(NULL);
   return NULL;
}
main() {
   pthread_t thread; // declare thread
   pthread_create(&thread, NULL, func, NULL);
   printf("From the main function, the thread id = %d\n", thread);
   pthread_join(thread, NULL); //join with main thread
}

আউটপুট

From the main function, the thread id = 1
From the function, the thread id = 1

  1. একটি সহজ থ্রেড তৈরি করতে C# প্রোগ্রাম

  2. একটি থ্রেড হত্যা করার জন্য C# প্রোগ্রাম

  3. থ্রেডের অগ্রাধিকার প্রদর্শনের জন্য C# প্রোগ্রাম

  4. জাভাতে থ্রেড পুল