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

C++11 Threading - invalid use of non-static member function - works gcc 5.1.0 broken gcc 7.3.1

code: // create class instance MC_THREAD MTHR; // set some values in the class MTHR.setup_mc_thread("com6", &es); // create an thread to run the non-static member function std::thread…
3
votes
2 answers

Forward types in variadic template as values/references according to function signature

This question relates to this, this and potentially this. I have the following class, in which the AddFunction method receives a function and a list of arguments to that function, then spawns an std::thread calling the passed function with the…
definelicht
  • 428
  • 2
  • 14
3
votes
2 answers

Exiting from a function that has a local std::thread object without join

I have the following simple function, just for demonstration: void thread_func(int i) { return i; } And I have another function that calls it in a new thread and does not wait for it to return: void my_nice_function() { std::thread…
SomethingSomething
  • 11,491
  • 17
  • 68
  • 126
3
votes
1 answer

Reference to abstract class can't be passed to thread function?

I want to use a reference to an abstract class (A) as parameter type in an function which is invoked using std::thread. It seems not possible, because the compiler tries for some reason to compile: std::tuple, even that in my code only a…
Constantin
  • 8,721
  • 13
  • 75
  • 126
3
votes
1 answer

Open a MFC dialog in a std::thread

I would like to show a dialog to inform the user that the application is busy. To avoid blocking of the main thread, I was thinking to use a std::thread to show the dialog. Consider the following code: InProcDlg inProcess; std::thread t([ &inProcess…
GregPhil
  • 475
  • 1
  • 8
  • 20
3
votes
2 answers

Populating a vector in parallel, order not important

I have to create a vector of k elements. Every thread will create its portion, let's say k * 25% and must place it into the vector, at any index. Driven from this example, I was about to do something like…
gsamaras
  • 71,951
  • 46
  • 188
  • 305
3
votes
3 answers

std::thread thread spun off in object, when does it terminate?

If I spin off an std::thread in the constructor of Bar when does it stop running? Is it guaranteed to stop when the Bar instance gets destructed? class Bar { public: Bar() : thread(&Bar:foo, this) { } ... void foo() { while (true) {//do…
user695652
  • 4,105
  • 7
  • 40
  • 58
3
votes
2 answers

Parallel simulations using NS3 and std::thread

I am using the NS3 framework to run Wi-Fi simulations with various configurations. I want to use std::thread to run many (hundreds) of simulations simultaneously, within one process. Here is a my code, with some configuration redacted: void…
Blake
  • 695
  • 9
  • 23
3
votes
3 answers

std::thread (detachable) and exception safety

It is a common practice to use guards, RAII and try/catch blocks to ensure that a thread will be eventually joined in all cases. However, what about threads that are meant to be detached? void aFunction() { std::thread t {someOtherFunction}; //…
Marinos K
  • 1,779
  • 16
  • 39
3
votes
2 answers

Run an app and forget in a portable way

I am writing a small updater utility that is being called from the main program. The main program terminates right after the call and lets the updater copy all the updated files and then it should re-launch the main program. However that last bit is…
Resurrection
  • 3,916
  • 2
  • 34
  • 56
3
votes
1 answer

C++: Boost.Asio: Start SSL Server session in a new thread

I wrote a pair of server/client programs based on this example for the server and I'm done of all communication protocols. The server is supposed to receive multiple connections from multiple connections from multiple client, so I want to separate…
The Quantum Physicist
  • 24,987
  • 19
  • 103
  • 189
3
votes
1 answer

move semantics/behaviors in std::bind and std::thread

Please look at the following simple test program, you can just copy and test. I tried with gcc 4.9 it compiles fine. #include #include #include #include class Test { public: Test(const Test &t) {…
user534498
  • 3,926
  • 5
  • 27
  • 52
3
votes
1 answer

Interaction between c++11 std::thread and class friend function

I'm having trouble understanding a compiler error I'm getting trying to use a function declared as a friend to a class in a c++11 std::thread object. I've created a small example to show the issue I'm having. Basically, when the compiler has only…
matth
  • 563
  • 7
  • 22
3
votes
1 answer

Implementing a function that perfect-forwards to std::thread

I am trying to write a wrapper around std::thread: #include #include struct A {}; template void lifted_lambda_1(void *m, F &&entrypoint, Args&&... args) { std::cout << "I will do something…
tohava
  • 5,344
  • 1
  • 25
  • 47
3
votes
1 answer

Can't pass parameters to std::thread?

I'm trying to use std::thread. My thread is supposed to call a method and pass a struct as a parameter, as so many examples show. Except my very simple code won't compile. For the record, I'm aware of this question but nothing there seems to help…
ruipacheco
  • 15,025
  • 19
  • 82
  • 138