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
6
votes
1 answer

When is std::thread destructor called?

I know that std::thread destructors are called on main exit, or when a thread object goes out of scope. But is it also destroyed when a function that it is calling is done executing? If not what happens to such a thread, can I still join() it?
mikol
  • 185
  • 1
  • 10
6
votes
1 answer

Detached threads accessing global or static objects

The following text is an excerpt taken from the section 18.2.1† of the book titled The C++ Standard Library: A Tutorial and Reference, 2nd Edition: Note, however, that the lifetime problem also applies to global and static objects, because when the…
JFMR
  • 23,265
  • 4
  • 52
  • 76
6
votes
1 answer

Different behavior when `std::lock_guard` object has no name

I'm learning about std::mutex, std::thread and I am surprised at the different behavior of 2 pieces of code below: #include #include #include using namespace std; std::mutex mtx; void foo(int k) { …
rsy56640
  • 299
  • 1
  • 3
  • 13
6
votes
2 answers

error: no matching function for call to std::thread

I'm trying to run a function called dcp in a thread, I've to run this function three times independently. So here's how I'm implemented that: void dcp(cv::Mat&, int, int, cv::Mat&, double); int main(int argc, char* argv[]) { cv::Mat…
Ja_cpp
  • 2,426
  • 7
  • 27
  • 49
6
votes
1 answer

Unexpectedly able to call derived-class virtual function from base class ctor

Can anyone help explain this unexpected behavior? The Premise I've created class Thread that contains a member std::thread variable. Thread's ctor constructs the member std::thread providing a pointer to a static function that calls a pure virtual…
StoneThrow
  • 5,314
  • 4
  • 44
  • 86
6
votes
1 answer

How to terminate a std::thread?

I am currently developing a program that needs to download some images from the socket server,and the downloading work will execute a long time. So, I create a new std::thread to do that. Once it's downloaded,the std::thread will call a member…
tobin
  • 127
  • 2
  • 8
6
votes
2 answers

Does a detached std::thread need to be deleted after it terminates?

I create a new std::thread object, and then detach() it. The thread runs for an arbitrary amount of time, and then terminates itself. Since I created the object with new, do I need to delete it at some point to free up its resources? Or does the…
Stuart Barth
  • 301
  • 2
  • 10
6
votes
4 answers

std::thread constructor with no parameter

According to cppreference.com, the std::thread constructor with no parameter means: Creates new thread object which does not represent a thread. My questions are: Why do we need this constructor? And if we create a thread using this constructor,…
james
  • 1,107
  • 14
  • 29
6
votes
1 answer

How to pass variadic args to a std::thread?

I would like to use my own Thread implementation by wrapping the std::thread class from C++11 so I will be able handle exceptions like I want. Here is my wrap class: #include #include #include #include…
didil
  • 693
  • 8
  • 22
6
votes
4 answers

Is A Member Function Thread Safe?

I have in a Server object multiple thread who are doing the same task. Those threads are init with a Server::* routine. In this routine there is a infinite loop with some treatments. I was wondering if it was thread safe to use the same method for…
Thibaud Auzou
  • 129
  • 10
6
votes
1 answer

Details in the process of constructing a std::thread object

I'm interested in (and confused about) the details of constructing a std::thread object. According to cppreference, both the thread function and all arguments are value-copied to some thread-accessible storage, and then invoke. 1) What exactly is…
Lingxi
  • 14,579
  • 2
  • 37
  • 93
6
votes
3 answers

How to write lambda function with arguments? c++

I want to call a method (for this example std::thread constructor) with lambda function, passing int value: int a=10; std::thread _testThread = thread([a](int _a){ //do stuff using a or _a ? }); _testThread.detach(); I don't know how to…
Dainius Kreivys
  • 525
  • 2
  • 8
  • 19
6
votes
1 answer

C++11 Threads: Error passing a vector to a thread function

I am working on a multithreaded median function as part of a larger project. I have little C++ experience. The median function below should take a vector of 3 dimensional int vectors, and return a 3 dimensional vector of ints where each entry is…
chaffdog
  • 163
  • 1
  • 1
  • 5
6
votes
1 answer

How do OpenMP, MPI, POSIX threads, std::thread, boost::thread correlate?

There are several ways to implement multithreading. std::thread was eventually brought by C++11 standard but boost::thread could be effectively used instead. Each technology has specific syntax and stuff, but - roughly - used for CPU parallel…
kazarey
  • 814
  • 1
  • 15
  • 32
6
votes
2 answers

std::thread Visual Studio 2012 Warning

I am trying to understand how to use the new std::thread using Visual Studio 2012. I am trying to compile the following code. #include #include class scoped_thread { std::thread t_; public: explicit…
Ram
  • 3,045
  • 3
  • 27
  • 42