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

Why std::thread accepts generic lambdas but not templated functions (without explicit instantiation)?

Why is it legal to pass generic lambdas to std::thread() without any specialization, while, on the other hand, this is illegal with function templates. The following demonstrates my query. #include template void f(T t) { } int…
alex35833
  • 107
  • 6
4
votes
1 answer

Understanding condition_variable::wait for blocking a thread

While implementing a thread pool pattern in C++ based on this, I came across a few questions. Let's assume minimal code sample: std::mutex thread_mutex; std::condition_variable thread_condition; void thread_func() { std::unique_lock
Rhino R.
  • 81
  • 7
4
votes
2 answers

Thread ID reuse between std::thread and tbb::task_group causing deadlock in OpenMP

*** UPDATE: changing code to a real case that reproduces the problem *** I'm working in some preexisting code that uses a number of multi-threading techniques; std::thread, plus Intel TBB TaskGroup, plus OpenMP. It looks like I've hit a race…
Sue Loh
  • 83
  • 1
  • 9
4
votes
2 answers

std::thread vs pthead, what am I doing wrong?

I'm working on a project that requires to execute some processes inside a docker container. I want to handle the case when the process doesn't terminate on time (let's say within 10 s). I'm using this DockerClientpp library for managing the…
fedemengo
  • 626
  • 2
  • 7
  • 24
4
votes
0 answers

For same task, why more threads lead to less instructions

Code I ran my program 30 times, and n passed to run_and_join_threads() changed from 1 to 30 accordingly. Note that jobs passed to run_and_join_threads() were populated by exactly the same way in each execution. void do_job(JobQueue *jobs) { Job…
David Chen
  • 1,777
  • 2
  • 12
  • 23
4
votes
1 answer

Boost process continuously read output

I'm trying to read outputs/logs from different processes and display them in a GUI. The processes will be running for long time and produce huge output. I'm planning to stream the output from those processes and display them according to my needs.…
Surya
  • 1,139
  • 1
  • 11
  • 30
4
votes
1 answer

Why move constructor is called twice when passing temporaries to thread function?

In below code I could not understand why move constructor of class is called twice considering that my thread function is taking argument by rvalue reference and so I was hoping move constructor will be called only once when arguments will be moved…
PapaDiHatti
  • 1,841
  • 19
  • 26
4
votes
2 answers

Purpose of condition_variable

Application without std::condition_variable: #include #include #include #include #include #include std::mutex mutex; std::queue queue; int counter; void loadData() { …
Akasata Akasata
  • 333
  • 4
  • 10
4
votes
1 answer

Starting a member function with arguments in a separate thread

I have a member function MyClass::doStuff(QString & str) I am trying to call that function from a thread like this: std::thread thr(&MyClass::doStuff, this); However, that results in an error: /usr/include/c++/4.8.2/functional:1697: error: no type…
Metal Wing
  • 1,065
  • 2
  • 17
  • 40
4
votes
2 answers

scoped thread wrapper for std::thread

I am trying to make a scoped thread. #include #include class ScopedThread { public: template< class Function, class... Args> explicit ScopedThread( int id, Function&& f, Args&&... args) : m_thread( std::ref(f),…
asit_dhal
  • 1,239
  • 19
  • 35
4
votes
1 answer

std::thread throws Access violation exception when created with arguments?

I'm using VS2015 and encounter a extremely strange problem when using std::thread. void Klass::myfunc(int a, int b) { std::cout << a << ' ' << b << std::endl; } // ... auto t = std::thread(&Klass::myfunc, this, 100, 200); <- runtime error after…
4
votes
1 answer

Moving a unique_lock to another thread

I was wondering what happens when you move a unique_lock that holds a recursive_mutex. Specifically, I was looking at this code: recursive_mutex g_mutex; #define TRACE(msg) trace(__FUNCTION__, msg) void trace(const char* function, const char*…
Mr. Anderson
  • 1,609
  • 1
  • 13
  • 24
4
votes
2 answers

Why c++ threads are movable but not copiable?

As the title of the question says, why C++ threads (std::thread and pthread) are movable but not copiable? What consequences are there, if we do make it copiable?
james
  • 1,107
  • 14
  • 29
4
votes
2 answers

std::thread initialization with class argument results with class object being copied multiple times

It seems that if you create an object of a class, and pass it to the std::thread initialization constructor, then the class object is constructed and destroyed as much as 4 times overall. My question is: could you explain, step by step, the output…
Marcin L
  • 73
  • 10
4
votes
2 answers

std::thread finishes before I can detach it

If I create an std::thread that terminates before I am able to call detatch() on it, what is the expected behavior? Should an exception be thrown due to the fact that joinable is already false? If this is so, then is there a way to create a thread…
Michael Landes
  • 969
  • 1
  • 8
  • 13