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

Console output order slows down multi-threaded program

When compiling the following code #include #include #include #include #include std::mutex cout_mut; void task() { for(int i=0; i<10; i++) { double d=0.0; for(size_t cnt=0;…
levzettelin
  • 2,600
  • 19
  • 32
8
votes
1 answer

Threading opencl compiling

[Update:] I'm spawning multiple processes now and it works fairly well, though the basic threading problem still exists. [/] I'm trying to thread a c++ (g++ 4.6.1) program that compiles a bunch of opencl kernels. Most of the time taken is spent…
starship
  • 93
  • 6
7
votes
2 answers

Why arguments moved twice when constructing std::thread

Consider this program that essentially creates std::thread that calls the function func() with arg as argument: #include #include struct foo { foo() = default; foo(const foo&) { std::cout << "copy ctor" << std::endl; } …
Shaoyan
  • 107
  • 6
7
votes
1 answer

"terminate called without an active exception" after pthread_cancel

In probing the conditions of this question, a problem arose, exemplified by the code below. #include #include #include #include #include using namespace std; // mocking external library call…
arayq2
  • 2,502
  • 17
  • 21
7
votes
2 answers

Accessing a moved std::string in a new thread

Consider the case below The name string is moved as an argument to the thread. void start(std::string&& name) { t = std::thread{&ThreadRunner::run, this, std::forward(name)}; } The thread's run function also takes a…
themagicalyang
  • 2,493
  • 14
  • 21
7
votes
1 answer

Issue with std::thread from c++11

I have some troubles trying to compile a program with multi-threading from the standard template library. It return me a obscure error when i try to compile the following program : #include #include void foo() { std::cout <<…
Harry333Cover
  • 379
  • 3
  • 11
7
votes
2 answers

Why running std::thread with empty function spend a lot of memory

I wrote a simple program which should run two threads, sort small array (~4096 Byte) and write into an output file. Input data contain in the one big file (~4Gb). Computer has 128MB memory. I found that running just empty main function use 14MB…
Miltiad
  • 71
  • 1
  • 3
7
votes
3 answers

Approach of using an std::atomic compared to std::condition_variable wrt pausing & resuming an std::thread in C++

This is a separate question but related to the previous question I asked here I am using an std::thread in my C++ code to constantly poll for some data & add it to a buffer. I use a C++ lambda to start the thread like this: StartMyThread() { …
TheWaterProgrammer
  • 7,055
  • 12
  • 70
  • 159
7
votes
2 answers

Implementing a simple, generic thread pool in C++11

I want to create a thread pool for experimental purposes (and for the fun factor). It should be able to process a wide variety of tasks (so I can possibly use it in later projects). In my thread pool class I'm going to need some sort of task…
krispet krispet
  • 1,648
  • 1
  • 14
  • 25
7
votes
2 answers

What special purpose does unique_lock have over using a mutex?

I'm not quite sure why std::unique_lock is useful over just using a normal lock. An example in the code I'm looking at is: {//aquire lock std::unique_lock lock(queue_mutex); //add task …
Syntactic Fructose
  • 18,936
  • 23
  • 91
  • 177
7
votes
1 answer

Can the thead joinable-join have a race condition? how do you get around it?

Lets say I have the following class class A { public: A() { my_thread=std::thread(std::bind(&A::foo, this)); } ~A() { if (my_thread.joinable()) { my_thread.join(); } } private: …
IdeaHat
  • 7,641
  • 1
  • 22
  • 53
7
votes
4 answers

2 threads slower than 1?

I was playing around with std::thread and something weird popped up: #include int k = 0; int main() { std::thread t1([]() { while (k < 1000000000) { k = k + 1; }}); std::thread t2([]() { while (k < 1000000000) { k = k + 1; }}); …
user123
  • 8,970
  • 2
  • 31
  • 52
7
votes
3 answers

C++11 std::thread giving error: no matching function to call std::thread::thread

I'm testing c++11 threads with this code, but when creating the thread, I'm having the error no matching function for call to 'std::thread::thread()'. It's like if there was something wrong with the function I'm giving to std::thread ctr, but I…
Roman Rdgz
  • 12,836
  • 41
  • 131
  • 207
6
votes
2 answers

Why std::thread() passes arguments by value (and why the reason given by Dr. Stroustrup is incorrect)?

Quoting from The C++ Programming Language (by Bjarne Stroustrup), page 1213 The thread constructors are variadic templates (§28.6). This implies that to pass a reference to a thread constructor, we must use a reference wrapper (§33.5.1). For…
alex35833
  • 107
  • 6
6
votes
2 answers

How do you use std::jthread::get_stop_token()?

I'm confused how std::jthread::get_stop_token is designed to work, because it seems to have an inherent race condition. Namely, the executing thread can't simply call std::jthread on itself (as in this example) because it has no guarantee that the…
user541686
  • 205,094
  • 128
  • 528
  • 886