Questions tagged [stdthread]

std::thread is a C++11 standard library class which represents a single thread of execution. Threads allow multiple functions to execute concurrently.

std::thread is a C++11 standard library type which creates a new thread and runs a function (or other callable object) in that new thread.

Include the <thread> header to use std::thread.

C++20 added std::jthread which automatically .join()s the thread on destruction instead of std::terminate()ing, a generally preferable behavior.

593 questions
203
votes
6 answers

When should I use std::thread::detach?

Sometime I have to use std::thread to speed up my application. I also know join() waits until a thread completes. This is easy to understand, but what's the difference between calling detach() and not calling it? I thought that without detach(), the…
Jinbom Heo
  • 7,248
  • 14
  • 52
  • 59
195
votes
12 answers

Thread pooling in C++11

Relevant questions: About C++11: C++11: std::thread pooled? Will async(launch::async) in C++11 make thread pools obsolete for avoiding expensive thread creation? About Boost: C++ boost thread reusing threads boost::thread and creating a pool of…
Yktula
  • 14,179
  • 14
  • 48
  • 71
191
votes
7 answers

What happens to a detached thread when main() exits?

Assume I'm starting a std::thread and then detach() it, so the thread continues executing even though the std::thread that once represented it, goes out of scope. Assume further that the program does not have a reliable protocol for joining the…
Marc Mutz - mmutz
  • 24,485
  • 12
  • 80
  • 90
122
votes
9 answers

How to check if a std::thread is still running?

How can I check if a std::thread is still running (in a platform independent way)? It lacks a timed_join() method and joinable() is not meant for that. I thought of locking a mutex with a std::lock_guard in the thread and using the try_lock() method…
kispaljr
  • 1,942
  • 2
  • 16
  • 22
113
votes
11 answers

How to get integer thread id in c++11

c++11 has a possibility of getting current thread id, but it is not castable to integer type: cout<
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
108
votes
4 answers

Passing object by reference to std::thread in C++11

Why can't you pass an object by reference when creating a std::thread ? For example the following snippit gives a compile error: #include #include using namespace std; static void SimpleThread(int& a) // compile error //static…
austinmarton
  • 2,278
  • 3
  • 20
  • 23
81
votes
6 answers

Portable way of setting std::thread priority in C++11

What is the correct way in the post C++11 world for setting the priority of an instance of std::thread Is there a portable way of doing this that works at least in Windows and POSIX (Linux) environments? Or is it a matter of getting a handle and…
Gerdiner
  • 1,435
  • 3
  • 15
  • 19
52
votes
4 answers

C++11: std::thread pooled?

In C++03 I used pthread with a self-built thread pool that always kept a couple of threads running (since pthread_create is slow), this way I was able to launch threads for small tasks without thinking about performance issues. Now, in C++11 we have…
lucas clemente
  • 6,255
  • 8
  • 41
  • 61
48
votes
3 answers

Confusion about threads launched by std::async with std::launch::async parameter

I am a little bit confused by the std::async function. The specification says: asynchronous operation being executed "as if in a new thread of execution" (C++11 §30.6.8/11). Now, what is that supposed to mean? In my understanding, the…
krispet krispet
  • 1,648
  • 1
  • 14
  • 25
39
votes
3 answers

Difference between pointer and reference as thread parameter

This is the example: #include #include using namespace std; void f1(double& ret) { ret=5.; } void f2(double* ret) { *ret=5.; } int main() { double ret=0.; thread t1(f1, ret); t1.join(); cout << "ret=" << ret <<…
Predrag
  • 1,527
  • 2
  • 13
  • 17
37
votes
3 answers

C++11: What happens if you don't call join() for std::thread

Given below: void test() { std::chrono::seconds dura( 20 ); std::this_thread::sleep_for( dura ); } int main() { std::thread th1(test); std::chrono::seconds dura( 5 ); std::this_thread::sleep_for( dura ); return 0; } main will exit…
kiki
  • 381
  • 1
  • 3
  • 6
36
votes
4 answers

Massive CPU load using std::lock (c++11)

My recent efforts to implement a thread/ mutex manager ended up in an 75% CPU load (4 core), while all four running threads were either in sleep or waiting for a mutex beeing unlocked. The specific class is far too large for being posted here…
mac
  • 395
  • 1
  • 3
  • 8
34
votes
2 answers

std::thread pass by reference calls copy constructor

Well I have an issue with passing data into a thread using std::thread. I thought I understood the general semantics of copy constructors, etc. but it seems I don't quite grasp the problem. I have a simple class called Log that has hidden it's copy…
xaviersjs
  • 1,579
  • 1
  • 15
  • 25
30
votes
2 answers

I want to kill a std::thread using its thread object?

Possible Duplicate: C++0x thread interruption I am trying to kill/stop a c++ std::thread by using its thread object. How can we do this?
CPS
  • 677
  • 2
  • 6
  • 9
29
votes
3 answers

How to convert std::thread::id to string in c++?

How to typecast std::thread::id to string in C++? I am trying to typecast output generated by std::this_thread::get_id() to a string or char array.
user2859777
  • 293
  • 1
  • 3
  • 5
1
2 3
39 40