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

How does this simple multithread code lead to memory corruption?

The following code reliably produces a segfault. #include #include class Class { public: Class(const int integer) : cinteger(integer), carray(std::array{0}) {} const int operator()() { …
Takirion
  • 173
  • 4
5
votes
1 answer

Why does std::this_thread::get_id() function returns the same number in CentOS?

In the following code, the .get_id() call returns the same value when run on CentOS; but on Windows, the same code returns different values. Why? #include #include #include void dosomework() { std::cout <<…
JY Lee
  • 51
  • 2
5
votes
2 answers

c++ lambda function calls pure virtual function

I am trying to create a wrapper class for std::thread. This class provides a kick method which starts the thread and calls a pure virtual function. I am using a derived class to call this kick method and derived class also has implemented the…
theadnangondal
  • 1,546
  • 3
  • 14
  • 28
5
votes
2 answers

Can one retrieve the return value of a thread function in C++11?

If a function has a non-void return value and I join it using the .join function then is there any way to retrieve its return value? Here is a simplified example: float myfunc(int k) { return exp(k); } int main() { std::thread…
Reza
  • 388
  • 2
  • 14
5
votes
1 answer

c++11 threads vs async

Consider the following two snippets of code where I am trying to launch 10000 threads: Snippet 1 std::array, 10000> furArr_; try { size_t index = 0; for (auto & fut : furArr_) { …
Arun
  • 3,138
  • 4
  • 30
  • 41
5
votes
1 answer

How to get thread object by id?

Let's say that some thread has std::thread::id myId of some other thread. Now I want to retrieve the std::thread object associated with myId so I can .join() it. Is it possible in std? Or do I have to track it manually?
freakish
  • 54,167
  • 9
  • 132
  • 169
5
votes
1 answer

Is it safe to never retrieve the result of a std::future from a std::packaged_task?

Is it safe to create a std::future from a std::packaged_task, which executes on a separate thread, but not always retrieve its result? #include #include class Result { Result() {} ~Result() {} }; void foo() { …
Pol
  • 3,848
  • 1
  • 38
  • 55
5
votes
3 answers

Retrieve the Windows thread identifier from the std::thread::id structure

I have access to a std::thread::id in my code, and need to use some native functions that receive as argument ThreadId as DWORD ( same as returned by GetCurrentThreadId() ). I cannot find any way to convert from std::thread::id to a Win32 DWORD…
cprogrammer
  • 5,503
  • 3
  • 36
  • 56
5
votes
2 answers

Assigning a new task to a thread after the thread completes in C++

I have the following code in C++. The code is from C++ Concurrency In Action: Practical Multithreading void do_work(unsigned id); void f() { std::vector threads; for(unsigned i = 0; i < 20; ++i) { …
Mutating Algorithm
  • 2,604
  • 2
  • 29
  • 66
5
votes
2 answers

Launching runnable objects in threads out of a C++ std::vector

I have a C++11 program which configures a number of runnable objects, puts them in a std::vector, then launches them all in separate threads. Unfortunately when I iterate over the objects in the vector, I am getting threads launched only for the…
lcikgl
  • 759
  • 1
  • 8
  • 20
5
votes
1 answer

Perfect Forwarding Variadic Template to Standard Thread

I'm trying to make a form of std::thread that puts a wrapper around the code executed in the thread. Unfortunately I can't get it to compile due likely to my poor understanding of rvalues and the Function templated type I'm trying to pass. Here's my…
5
votes
1 answer

C++ 11 : Start thread with member function and this as parameter

Using this code, I got and error : Error 1 error C2064: term does not evaluate to a function taking 1 arguments c:\program files (x86)\microsoft visual studio 11.0\vc\include\functional 1152 1 Pipeline class PipelineJob { private: …
user1401072
5
votes
1 answer

How standard is std::thread?

I've noticed that on a lot of the classic C++ reference sources that HAVE been updated for C++11, such as cplusplus.com and the Josuttis Standard Library Reference book, don't seem to cover / have any documentation at all on the C++11 concurrency…
David Adrian
  • 1,079
  • 2
  • 9
  • 24
5
votes
2 answers

timeout in std::async

Is there a way to implement timeout in the std::async method, so I want this call to timeout and complete if the thread hasnt completed for the specified amount of time. How can I implement this functionality.
Adobri
  • 471
  • 7
  • 15
5
votes
6 answers

A parallel for using std::thread?

I'm new with std::thread and I try to code a parallel_for. I coded the following thing: // parallel_for.cpp // compilation: g++ -O3 -std=c++0x parallel_for.cpp -o parallel_for -lpthread // execution: time ./parallel_for 100 50000000 // (100: number…
Vincent
  • 57,703
  • 61
  • 205
  • 388