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
15
votes
2 answers

Delete std::thread after calling join?

I have some code that dynamically allocates a new std::thread from the C++11 header, like this: std::thread *th = new thread( /* my args */); Some time later, I call join: th->join(); Since I dynamically allocated the thread, do I also…
bobroxsox
  • 902
  • 2
  • 10
  • 26
14
votes
3 answers

using std::thread in a library loaded with dlopen leads to a sigsev

I recently discovered a strange behaviour using std::thread and dlopen. Basically, when I execute a std::thread in a library which is loaded using dlopen I receive a sigsev. The library itself is linked against pthread, the executable that calls…
A. Gocht
  • 143
  • 1
  • 7
14
votes
1 answer

Correct way to pause & resume an std::thread

I am using an std::thread in my C++ code to constantly poll for some data & add it to a buffer. I use a C++ lambda to start the thread like this: StartMyThread() { thread_running = true; the_thread = std::thread { [this] { …
TheWaterProgrammer
  • 7,055
  • 12
  • 70
  • 159
14
votes
1 answer

error: use of deleted function ‘std::thread::thread(const std::thread&)'

The code bellow compiles and works as expected. The structure (class) A derives from std::thread and expands with an int more. The main code creates some threads and afterwards waits them to finish. The problem is that while the code compiles…
user3723779
  • 287
  • 1
  • 2
  • 12
13
votes
1 answer

Why does add function have no effect in c++ 11 thread?

I am trying to learn the c++ 11 thread and have following code : #include #include #include #include #include void add(int& i){ std::mutex some_mutex; // std::cout << " I am " << std::endl; …
pokche
  • 1,141
  • 12
  • 36
13
votes
1 answer

Efficiently waiting for all tasks in a threadpool to finish

I currently have a program with x workers in my threadpool. During the main loop y tasks are assigned to the workers to complete, but after the tasks are sent out I must wait for all tasks for finish before preceding with the program. I believe my…
Syntactic Fructose
  • 18,936
  • 23
  • 91
  • 177
12
votes
2 answers

Why does std::condition_variable as a class member cause compile errors with std::thread?

I tried including std::condition_variable as a class member, and got a lot of compilation errors when passing an object of this class to a std::thread. I cut down all the other code from my actual program, and ended up with the below minimal code.…
Masked Man
  • 1
  • 7
  • 40
  • 80
12
votes
4 answers

is it safe to detach a thread and then let it go out of scope (and have it still running)?

I have the following code, which I think works ok (forgive the silly/contrived example). void run_thread() { std::thread t([]{ while(true) { // keep getting chars... to stop peoples eye's hurting : ) char…
code_fodder
  • 15,263
  • 17
  • 90
  • 167
12
votes
2 answers

C++ Thread taking reference argument failed compile

#include #include using namespace std; void f1(double& ret) { ret=5.; } int main() { double ret=0.; thread t1(f1, ret); t1.join(); cout << "ret=" << ret << endl; } The above code fails compilation with the…
Rich
  • 1,669
  • 2
  • 19
  • 32
12
votes
3 answers

How can I declare an std::thread anonymously?

Consider the following short program: #include int Foo() { while (1); } int main(){ std::thread t(Foo); std::thread s(Foo); // (std::thread(Foo)); t.join(); } This compiles and runs (forever), with g++…
merlin2011
  • 71,677
  • 44
  • 195
  • 329
12
votes
1 answer

Error creating std::thread on Mac OS X with clang: "attempt to use a deleted function"

Consider my test code: #include class Foo { public: void threadFunc() {} void startThread() { _th = std::thread(&Foo::threadFunc, *this); } private: std::thread _th; }; int main(int argc, char *argv[]) { …
Violet Giraffe
  • 32,368
  • 48
  • 194
  • 335
12
votes
4 answers

Non-obvious lifetime issue with std::promise and std::future

This question is very similar to a previous one here: race-condition in pthread_once()? It is essentially the same issue - the lifetime of a std::promise ending during a call to promise::set_value (ie: after the associated future has been flagged,…
Steve Lorimer
  • 27,059
  • 17
  • 118
  • 213
11
votes
3 answers

Invoking a function automatically on std::thread exit in C++11

I want to set up an invocation of a function (or lambda function) to happen automatically when the current thread exits, but I cannot see any way to do it that works with std::thread unless I take over the entire task of thread creation or manually…
markt1964
  • 2,638
  • 2
  • 22
  • 54
11
votes
2 answers

Telling an std::thread to kill/stop itself when a condition is met

Say I have a worker thread tWorker, which is initialized when Boss is constructed and tells it to do work(), until bRetired is true. An std::mutex, mtx, locks some data (vFiles) so that tWorker owns it when he's working on it. How do I make tWorker…
alxcyl
  • 2,722
  • 7
  • 31
  • 47
11
votes
2 answers

MinGW and std::thread

So I've been trying to get the following code to compile and run on Windows by using a MinGW compiler. #include #include void test() { std::cout << "test" << std::endl; } int main() { std::thread t(test); } I'm…
Lukas
  • 2,461
  • 2
  • 19
  • 32
1 2
3
39 40