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
10
votes
0 answers

Why isn't a thread_local variable destroyed when the thread returns?

For a better understanding of this question, here is the code: // code 1 #include #include struct tls_test { tls_test() { std::cout << "tls_test ctor\n"; } ~tls_test() { std::cout << "tls_test dtor\n"; } …
iTruth
  • 123
  • 7
10
votes
2 answers

Storing an std::thread in C++11 smart pointer

In C++ 11 & above what are the advantages or disadvantages when storing an std::thread as a member of class directly like so: std::thread my_thread; As opposed to storing a std::shared_ptr or std::unique_ptr to the thread like…
TheWaterProgrammer
  • 7,055
  • 12
  • 70
  • 159
10
votes
2 answers

Printing std::this_thread::get_id() gives "thread::id of a non-executing thread"?

This used to work perfectly fine (and then aliens must have hacked my PC): #include #include int main() { std::cout << std::this_thread::get_id() << std::endl; return 0; } and now it prints thread::id of a…
Kiril Kirov
  • 37,467
  • 22
  • 115
  • 187
10
votes
3 answers

undefined reference to `pthread_create' Error when making C++11 application with ASIO and std::thread

I set Eclipse (Actually Xilinx SDK but based on Eclipse), and g++4.9.2, to compile a project which uses standalone ASIO and I used -std=c++11 in the Properties -> C/C++ Build -> Settings -> Tool Settings -> Other flags so it can compile using all…
Splash
  • 1,288
  • 2
  • 18
  • 36
10
votes
2 answers

Is std::thread::id unique across processes?

From my experience, it seems that the result of std::this_thread::get_id() is unique across process: ids are different from one process to another. Is this guaranteed by the standard?
Benoit Blanchon
  • 13,364
  • 4
  • 73
  • 81
10
votes
1 answer

Copy constructor calls when creating a new thread

I'm reading the book C++ Concurrency in Action to learn more about threading and the C++ memory module. I'm curious about the number of times the copy constructor is called in the following code: struct func { func() = default; func(const…
10
votes
1 answer

cancelling std::thread using native_handle() + pthread_cancel()

I am converting a previous thread wrapper around pthreads to std::thread. However c++11 does not have any way to cancel the thread. I REQUIRE, nonetheless, to cancel threads since they may be performing a very lengthy task inside an external…
João Leal
  • 123
  • 1
  • 7
9
votes
1 answer

std::thread causes segmentation fault in Raspbian using gcc-linaro-4.9.4

I'm getting a segmentation fault on code that looks perfectly valid to me. Here's a minimal recreating example: #include #include void func() { /* do nothing; thread contents are irrelevant */ } int main() { for…
Shachar
  • 91
  • 2
9
votes
3 answers

Is behaviour well-defined when `sleep_until()` specifies a time point in the past?

The C++11 standard talks about what should happen if the system clock is adjusted such that the time point passed to sleep_until() is now in the past - but I can't see anywhere that addresses the case when the specified time point is already in the…
Jeremy
  • 5,055
  • 1
  • 28
  • 44
9
votes
0 answers

How do you use std::atomic to achieve thread-safety without locking mutexes?

I know, that in some situations you can avoid having to lock mutexes (std::mutex) by using std::atomic, thus increasing performance. Can you name a situation like this, and preferably show some example code on how to do this (how do you use…
krispet krispet
  • 1,648
  • 1
  • 14
  • 25
8
votes
2 answers

std::thread supposedly leading to unusable stack trace

The question is related to alleged disadvantage of std::thread. Yesterday I was casually traversing the popular open source distributed proxy envoy by Lyft. When I was studying their threading portion i came across a comment which caught my eye. The…
sujat
  • 287
  • 2
  • 16
8
votes
2 answers

Is it safe to pass const reference to temporary/annonymous lambda into std::thread constructor?

Following on from this question: can-a-temperary-lambda-by-passed-by-reference? I have a fixed code snippet: // global variable std::thread worker_thread; // Template function template void start_work(const Functor &worker_fn) …
code_fodder
  • 15,263
  • 17
  • 90
  • 167
8
votes
1 answer

How to check if std::thread is valid

I got a class object, which only needs in some cases to start a thread. In the destructor it would be convenient for me to know whenever or not there is/was a thread. The question is, how do I detected if a std::thread object is or was a valid…
Martin Schlott
  • 4,369
  • 3
  • 25
  • 49
8
votes
1 answer

Eclipse content assist doesn't recognize std::thread, but compiles correctly

I am running Ubuntu 14.04. Steps I took to reproduce: Create a new C++ project (New -> C++ -> Hello World project), which I called TestStdThread Change the code in the main file to this: #include #include int main() { …
tckmn
  • 57,719
  • 27
  • 114
  • 156
8
votes
2 answers

std::thread::detach causes crash after original caller is destroyed

struct Test { bool active{true}; void threadedUpdate() { std::this_thread::sleep_for(std::chrono::milliseconds(1)); if(!active) // crashes here after Test instance is destroyed return; } Test() { …
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416