এই টিউটোরিয়ালে, আমরা C/C++-তে থ্রেড ফাংশন আলোচনা করব।
থ্রেড ফাংশন ইউজারদের কনকারেন্ট ফাংশন একই সময়ে ইমপ্লিমেন্ট করতে দেয়, যেগুলো এক্সিকিউশনের জন্য একে অপরর ওপর নির্ভরশীল বা স্বতন্ত্র হতে পারে।
উদাহরণ
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
void* func(void* arg){
// কারেন্ট থ্রেড ডিট্যাচ
pthread_detach(pthread_self());
printf("Inside the thread\n");
pthread_exit(NULL);
}
void fun(){
pthread_t ptid;
// নতুন থ্রেড তৈরি
pthread_create(&ptid, NULL, &func, NULL);
printf("This line may be printed before thread terminates\n");
if(pthread_equal(ptid, pthread_self())
printf("Threads are equal\n");
else
printf("Threads are not equal\n");
// তৈরি করা থ্রেড টার্মিনেট হওয়ার জন্য অপেক্ষা
pthread_join(ptid, NULL);
printf("This line will be printed" " after thread ends\n");
pthread_exit(NULL);
}
int main(){
fun();
return 0;
}
আউটপুট
This line may be printed before thread terminates Threads are not equal Inside the thread This line will be printed after thread ends