← Back to C++ Course | Chapter 17: Advanced C++ | Lesson 12 of 17

C++ के Threads (std::thread)

एक thread एक program के अंदर काम की एक separate line है, एक kitchen में एक ही समय पर दो cooks होने की तरह। यह एक program को कई चीज़ें साथ करने देता है।
Syntax
cpp
#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

cpp
#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;
}

Threads Join करना

आपको अपने thread object destroy होने से पहले इस पर join() call करना ज़रूरी है। join() function main program को block करता है और thread के अपना काम खत्म करने का wait करता है, एक clean shutdown सुनिश्चित करते हुए।

उदाहरण: Joining Threads

cpp
#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;
}

Threads Detach करना

Alternatively, आप एक thread को background में independently चलने देने के लिए detach() call कर सकते हैं। एक बार detach हो जाए, आप thread को rejoin नहीं कर सकते, और यह खत्म होने पर खुद clean up कर लेगा।

उदाहरण: Detaching Threads

cpp
#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;
}

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

cpp
#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;
}

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

cpp
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. thread object destroy होने से पहले join() या detach() call करना भूल जाना, जो std::terminate call करता है।
  2. बिना std::ref के एक thread को एक reference pass करना, इसलिए एक copy इस्तेमाल होती है।
  3. एक thread detach करना जो local variables इस्तेमाल करता है जो पहले destroy हो जाते हैं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.