CPP 057 – Thread Giriş

Thread ID

  • Unique’tir
  • Tekrardan kullanılabilir
  1. An object of type thread::id provides a unique identifier for each thread of execution and a single distinct value for all thread objects that do not represent a thread of execution (30.3.1). Each thread of execution has an associated thread::id object that is not equal to the thread::id object of any other thread of execution and that is not equal to the thread::id object of any std::thread object that does not represent threads of execution.
  2. thread::id shall be a trivially copyable class (Clause 9). The library may reuse the value of a thread::id of a terminated thread that can no longer be joined.
void hi() {
	cout << "hi thread: " << this_thread::get_id() << endl;
}

int main() {
	cout << "main thread: " << this_thread::get_id() << endl;

	thread t(hi);
	cout << "t.get_id: " << t.get_id() << endl;
	t.join();
	cout << "t.get_id (after execution): " << t.get_id() << endl;
}
// main thread : 20328
// t.get_id : 17748
// hi thread : 17748
// t.get_id(after execution) : 0

sleep_for()

belirtilen süre kadar thread i uyutur:

void hi() {
	//this_thread::sleep_for(chrono::seconds(1));
	this_thread::sleep_for(1s);
	cout << "Hello World" << endl;
}

int main() {
	thread t(hi);
	t.join();
}

Thread Object

Thread RAII idiomunu implemente etmiştir ve sadece move edilebilir:

void hi() {
	cout << "hi thread: " << this_thread::get_id() << endl;
}

//void func(thread t) { // Not give an error but 
void func(thread&& t) { // use like this is better
	cout << "funt function t.get_id(): " << t.get_id() << endl;
	t.join();
}

int main() {
	thread t(hi);

	//func(t); // Error: thread object is move only 
	func(move(t));

}
// funt function t.get_id(): 5364
// hi thread : 5364

Return ederken bizim yerimize compiler optimize ediyor:


void hi() {
	cout << "hi thread: " << this_thread::get_id() << endl;
}

std::thread thread_creator() {

	thread t(hi);
	return t; 
	// dont use std::move (it can confuse compiler)
}

int main() {
	thread t = thread_creator();
	t.join(); 
}

Exceptions

Thread içinde handle edlimeli.

If I have a C++11 program running two threads, and one of them throws an unhandled exception, what happens? Will the entire program die a fiery death? Will the thread where the exception is thrown die alone (and if so, can I obtain the exception in this case)? Something else entirely?


Nothing has really changed. The wording in n3290 is:

If no matching handler is found, the function std::terminate() is called

The behavior of terminate can be customized with set_terminate, but:

Required behavior: A terminate_handler shall terminate execution of the program without returning to the caller.

So the program exits in such a case, other threads cannot continue running.

Detach

  • Join yerine detach() fonksiyonunu çağırabiliriz.
  • child thread tamamlanana kadar ya da program terminate edilene kadar çalışır
void hi() {
	cout << "hi thread: " << this_thread::get_id() << endl;
}

int main() {
	thread t(hi);
	t.detach(); 

	cout << "This is main thread!" << endl;
	this_thread::sleep_for(1s);
}
// This is main thread!
// hi thread : 15420

Başka bir deyişle join de thread’in katılmasını bekliyor ama detach’de herkes kendi yoluna gidiyor (ta ki program bitene kadar)

Data Race

Birden fazla thread’in aynı memory bölgesini kullanması durumunda sıkıntı çıkması. Bu sıkıntı genelde okumada değil yazmada olur. 2 kişinin aynı tabure aynı anda oturması nasıl sıkıntılı bir durum ise iki thread’in aynı anda aynı değişkeni değiştirmesi de aynı şekilde sıkıntılı bir durumdur.

Data race örneği:

#include <iostream>
#include <thread>

using namespace std;

void print(string str) {
	for (int i = 0; i < 5; i++)
		cout << str[0] << str[1] << str[2] << str[3] << str[4] << "\n";
}

int main() {
	thread t1(print, "Metin");
	thread t2(print, "Ahmet");
	thread t3(print, "Selin");
	thread t4(print, "Fatma");

	t1.join();
	t2.join();
	t3.join();
	t4.join();
}
// Output:
// ASelin
// Selin
// Selin
// Selin
// Selin
// Fatma
// Metin
// Metin
// Metin
// Metin
// Metin
// Fatma
// Fatma
// Fatma
// Fathmet
// Ahmet
// Ahma
// met
// Ahmet
// Ahmet


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *