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

Should class be not trivially destructible, if standard specifies that it has a destructor?

Consider std::latch [thread.latch.class]: namespace std { class latch { public: static constexpr ptrdiff_t max() noexcept; constexpr explicit latch(ptrdiff_t expected); ~latch(); latch(const latch&) = delete; latch&…
Alex Guteniev
  • 12,039
  • 2
  • 34
  • 79
3
votes
2 answers

Problem with thread function arguments in C++

The objective of this main function is to find the number of prime numbers in a range using threading to divide the problem into the selected number of threads. I'm having issues with std::thread and getting an error because of the arguments. I'm…
3
votes
0 answers

Does std::thread use parameter packs and if yes then how? :)

I want to achieve a similar result: void somefunc(int var1, char var2){ dosomething(); } int main(){ std::thread t1(somefunc, 'a', 3); return EXIT_SUCCESS; } std::thread takes a function pointer and any number of argments. In my class that…
3
votes
1 answer

using std::thread in shared library causes SIGSEGV

I just came to Linux c++ programming from Windows. Trying to make a shared library libso.so, which uses std::thread. The shared library will be loaded by other people and call the export function. The test code: // so.cpp, the .so library #include…
aj3423
  • 2,003
  • 3
  • 32
  • 70
3
votes
2 answers

Embedded device -> std::thread -> FreeRTOS?

so currently i am investigating the possibility in using a pure C++17 Project for an embedded device (Cortex m4). But based on the fact that it is an embedded device we have port and use an RTOS Such as FreeRTOS or uc-OS and i would highly prefer in…
3
votes
1 answer

What is the lifetime of C++ member variables when running in a std::thread?

#include #include #include using namespace std; struct safe_thread : public thread { using thread::thread; safe_thread& operator=(safe_thread&&) = default; ~safe_thread() { if (joinable()) …
Liang Wang
  • 41
  • 3
3
votes
1 answer

Calling vector of threads from destructor

Why can't I call my vector of threads from the destructor? Is there some rule for using destructors? void p () { std::cout << "thread running " << std::endl; } class VecThreads // Error: In instantiation of member function…
3
votes
1 answer

0 as a timeout in std::condition_variable::wait_for

For instance I have next code: void waitForResponse(const std::optional& ms){ std::unique_lock lk{_mtx}; _cv.wait_for(lk, std::chrono::milliseconds{ms ? *ms : 0}, []() { return someCondition; } } Is it specified…
Igor
  • 1,029
  • 9
  • 21
3
votes
2 answers

Why are we not allowed to pass pure reference arguments to std::thread but are allowed to pass raw pointers?

Lets say I want to pass some reference argument to the thread - the standard only allows this by using std::ref. Now lets consider this code that has undefined behaviour (see comments) void thread_fun(int& x) { …
ampawd
  • 968
  • 8
  • 26
3
votes
1 answer

Setting Thread Affinity of std::threads

I am trying to figure out how to set the thread affinity of std::thread or boost::thread using the win32 API. I want to use the SetThreadAffinityMask function to pin each thread to a particular core in my machine. I used the thread native_handle…
Joe Schmoe
  • 33
  • 1
  • 6
3
votes
2 answers

How to let std::thread delete the object automatically after excuting its member function

I want want to implement a cmmand class which does some work in an another thread, and I don't want to let users to delete that object manually.My command class like this: class Cmd { public: void excute() { std::cout << "thread begins" <<…
maidamai
  • 712
  • 9
  • 26
3
votes
3 answers

Is it a good practice to call pthread_sigmask in a thread created by std::thread?

1) I'm new to std::thread and I would like to know whether it is a good practice to call pthread_sigmask() to block some signals in a particular thread created by std::thread. I don't want the new thread to receive signals such as SIGTERM, SIGHUP,…
void
  • 338
  • 5
  • 19
3
votes
0 answers

How do I get the native handle of the current thread, with standard C++11?

I know about std::this_thread::get_id(), and about std::thread::native_handle(). But - the latter is a method of std::thread, while the former gets you an ID - and you can't construct an std::thread from an std::thread::id. So - how can I otherwise…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
3
votes
3 answers

std::thread that isn't a global variable but doesn't go out of scope when the end of the function that created it is reached?

So I ran across something that seems to defeat the purpose of std::thread or at least makes it less convenient. Say I want to spawn an std::thread to perform a task one time and don't want to worry about it again after that. I create this thread…