C++ के Threads (std::thread)
In this page:
#include <thread>
std::thread thread_name(function_name, arguments);
thread_name.join(); // wait for the thread to finish
Threads का Introduction
C++ में 'std::thread' class आपको अपने program के कई हिस्से parallel में चलाने देती है। यह multitasking enable करता है, जो multi-core processors पर आपके program की performance improve कर सकता है।
उदाहरण: Introduction to Threads
#include <iostream>
#include <thread>
void task() { std::cout << "Running in a thread" << std::endl; }
int main() {
std::thread t(task); // runs in parallel
t.join();
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Threads Join करना
आपको अपने thread object destroy होने से पहले इस पर join() call करना ज़रूरी है। join() function main program को block करता है और thread के अपना काम खत्म करने का wait करता है, एक clean shutdown सुनिश्चित करते हुए।
उदाहरण: Joining Threads
#include <iostream>
#include <thread>
void task() { std::cout << "Thread work" << std::endl; }
int main() {
std::thread t(task);
t.join(); // waits for the thread to finish
std::cout << "Main continues after join" << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Threads Detach करना
Alternatively, आप एक thread को background में independently चलने देने के लिए detach() call कर सकते हैं। एक बार detach हो जाए, आप thread को rejoin नहीं कर सकते, और यह खत्म होने पर खुद clean up कर लेगा।
उदाहरण: Detaching Threads
#include <iostream>
#include <thread>
#include <chrono>
void task() { std::cout << "Detached thread running" << std::endl; }
int main() {
std::thread t(task);
t.detach(); // runs independently, cannot be rejoined
std::this_thread::sleep_for(std::chrono::milliseconds(50));
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Threads को Arguments Pass करना
आप thread functions को arguments pass कर सकते हैं। Default से, arguments copy होते हैं।
अगर आप एक argument reference से pass करना चाहें, इसे 'std::ref' से wrap करें। std::ref के बिना एक reference pass करना actually value copy कर देगा, जो कम ही आपका intent होता है जब एक thread को shared state modify करना हो।
उदाहरण: Passing Arguments to Threads
#include <iostream>
#include <thread>
#include <string>
void greet(std::string name) { std::cout << "Hello, " << name << std::endl; }
int main() {
std::thread t(greet, "Alex"); // argument copied by default
t.join();
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Thread IDs Manage करना
हर thread का एक unique identifier होता है। आप std::this_thread::get_id() इस्तेमाल करके current thread की ID पा सकते हैं, या execution pause करने के लिए sleep_for() इस्तेमाल कर सकते हैं।
Thread IDs compare करना यह check करने का standard तरीका है कि दो std::thread objects same underlying OS thread को refer करते हैं या नहीं।
उदाहरण: Managing Thread IDs
// Include std::cout and std::cin
#include <iostream>
// Include std::thread
#include <thread>
// Program execution starts in main()
int main() {
// Print to the console with cout
std::cout << "Main thread ID: " << std::this_thread::get_id() << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- thread object destroy होने से पहले
join()याdetach()call करना भूल जाना, जोstd::terminatecall करता है। - बिना
std::refके एक thread को एक reference pass करना, इसलिए एक copy इस्तेमाल होती है। - एक thread detach करना जो local variables इस्तेमाल करता है जो पहले destroy हो जाते हैं।
Chapter Quiz — Complete all 17 topics to unlock
0/17 topics done
Complete these topics first:
- C++ vs C Differences
- C++ Interview Questions
- C++ Debugging Techniques
- C++ Input Validation
- C++ Namespaces
- C++ Header Files
- C++ Multi-file Programming
- C++ static_cast
- C++ dynamic_cast
- C++ const_cast
- C++ reinterpret_cast
- C++ Threads (std::thread)
- C++ Mutex & Locks
- C++ async & future
- C++ Mini Project — Calculator
- C++ Mini Project — Student Management
- C++ Interview Questions Advanced