Thread in C++11

Yesterday’s bug marks got almost disappeared by adding -std=c++11 in Preprocessor Include Path,

However it’s not perfect. For example, thread::join() should be valid and there is no error mark at ‘t.join()’. But if you store the handle in vector element t2, it says ‘Method “join” could not be resolved” although it’s valid 🙁

Anyway, I tried C++11 threads. I borrowed range from here.

void thread_test() {
	const int numThreads = 10;
	mutex mtx;

    vector<thread> threads(numThreads);
    for(int i:range(0,numThreads)){
    	threads[i] = thread([&mtx]() {
    		lock_guard<mutex> guard(mtx);
    		cout << "[" << this_thread::get_id() << "] Hoge!" << endl;
    	});
    }

    for(int i:range(0,numThreads)){
    	threads[i].join();
    }
}

Leave a Reply

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