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
28
votes
4 answers

C++ std::vector of independent std::threads

I'm building a real time software where I have a main infinite loops on main() and threads used to read and process data. One of the issues is keeping a std::vector of running threads to send signals to them and to monitor execution. So I put…
Mendes
  • 17,489
  • 35
  • 150
  • 263
26
votes
5 answers

std::thread::join() hangs if called after main() exits when using VS2012 RC

The following example runs successfully (i.e. doesn't hang) if compiled using Clang 3.2 or GCC 4.7 on Ubuntu 12.04, but hangs if I compile using VS11 Beta or VS2012 RC. #include #include #include #include…
Fraser
  • 74,704
  • 20
  • 238
  • 215
25
votes
4 answers

Is it possible to define an std::thread and initialize it later?

My aim is to keep an std::thread object as data member, and initialize it when needed. I'm not able to do this (as in my code below) because the copy constructor of the std::thread class is deleted. Is there any other way to do it? class MyClass { …
hkBattousai
  • 10,583
  • 18
  • 76
  • 124
25
votes
2 answers

When is it a good idea to use std::promise over the other std::thread mechanisms?

I am trying to establish some heuristics to help me decide the appropriate std::thread class to use. As I understand it, from highest level (simplest to use, but least flexible) to lowest level, we have: std::async with/std::future…
kfmfe04
  • 14,936
  • 14
  • 74
  • 140
21
votes
1 answer

std::thread error

I am trying to spawn a thread from within my class and the thread executes a particular method in my class. The code looks like this: class ThreadClass{ int myThread(int arg){ // do something } void createThread(){ thread t…
CPS
  • 677
  • 2
  • 6
  • 9
20
votes
4 answers

using std::cout in multiple threads

I write a simple program for testing Thread in c++11 but std::cout doesnt work as I expect. class Printer { public: void exec() { mutex m; m.lock(); cout<<"Hello "<
uchar
  • 2,552
  • 4
  • 29
  • 50
19
votes
1 answer

Threads in a vector can't be joined

I want to store a collection of threads in a vector, and join them all before exiting my program. I receive the following error when trying to join the first thread no matter how many I place in the collection: system_error: thread::join failed: No…
Rich Henry
  • 1,837
  • 15
  • 25
19
votes
4 answers

Qt - emit a signal from a c++ thread

I want to emit a signal from a C++ thread (std::thread) in Qt. How can I do it?
18
votes
8 answers

How to wake a std::thread while it is sleeping

I am using C++11 and I have a std::thread which is a class member, and it sends information to listeners every 2 minutes. Other that that it just sleeps. So, I have made it sleep for 2 minutes, then send the required info, and then sleep for 2…
TheWaterProgrammer
  • 7,055
  • 12
  • 70
  • 159
18
votes
5 answers

C++11 'native_handle' is not a member of 'std::this_thread'

In the following code snippet, void foo() { std::this_thread::native_handle().... //error here } int main() { std::thread t1(foo); t1.join(); return 0; } How do you get the native_handle from std::this_thread from within the function foo?
pyCthon
  • 11,746
  • 20
  • 73
  • 135
18
votes
4 answers

Is it necessary to use a std::atomic to signal that a thread has finished execution?

I would like to check if a std::thread has finished execution. Searching stackoverflow I found the following question which addresses this issue. The accepted answer proposes having the worker thread set a variable right before exiting and having…
Robert Rüger
  • 851
  • 9
  • 21
16
votes
2 answers

Capturing a `thread_local` in a lambda

Capturing a thread_local in a lambda: #include #include #include struct Person { std::string name; }; int main() { thread_local Person user{"mike"}; Person& referenceToUser = user; // Works fine -…
Mike Vine
  • 9,468
  • 25
  • 44
16
votes
1 answer

vector of std::threads

C++11 I am trying to make a vector of std::threads. The combination of the following three points says I can. 1.) According to http://en.cppreference.com/w/cpp/thread/thread/thread, thread’s default constructor creates a thread object which…
CodeBricks
  • 1,771
  • 3
  • 17
  • 37
16
votes
5 answers

Calling overloaded member functions using std::thread

Is it possible to have overloads for functions that we need to span using threads ? I have a simple class called Complex. class Complex { public: Complex():realPart_(0), imagPart_(0){} Complex(double rp, double ip) : realPart_(rp),…
Ram
  • 3,045
  • 3
  • 27
  • 42
15
votes
2 answers

Is there any reason C++ 11+ std::mutex should be declared as a global variable instead of passed into a std::thread as a function parameter?

I've seen most examples using std::mutex where the mutex is global. I was wondering is there any specific reason why this is done? I've had programs of my own where I don't do this, and simply pass the mutex in as a std::thread std::ref. Isn't it…
Krupip
  • 4,404
  • 2
  • 32
  • 54
1
2
3
39 40