All Questions
Tagged with c++ multithreading
18,165
questions
2204
votes
8
answers
295k
views
C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?
C++11 introduced a standardized memory model, but what exactly does that mean? And how is it going to affect C++ programming?
This article (by Gavin Clarke who quotes Herb Sutter) says that,
The ...
556
votes
21
answers
277k
views
Programmatically find the number of cores on a machine
Is there a way to determine how many cores a machine has from C/C++ in a platform-independent way? If no such thing exists, what about determining it per-platform (Windows/*nix/Mac)?
522
votes
7
answers
266k
views
std::unique_lock<std::mutex> or std::lock_guard<std::mutex>?
I have two use cases.
A. I want to synchronise access to a queue for two threads.
B. I want to synchronise access to a queue for two threads and use a condition variable because one of the threads ...
457
votes
9
answers
128k
views
What is std::promise?
I'm fairly familiar with C++11's std::thread, std::async and std::future components (e.g. see this answer), which are straight-forward.
However, I cannot quite grasp what std::promise is, what it does ...
397
votes
5
answers
486k
views
Start thread with member function
I am trying to construct a std::thread with a member function that takes no arguments and returns void. I can't figure out any syntax that works - the compiler complains no matter what. What is the ...
390
votes
7
answers
986k
views
Simple example of threading in C++
Can someone post a simple example of starting two (Object Oriented) threads in C++.
I'm looking for actual C++ thread objects that I can extend run methods on (or something similar) as opposed to ...
327
votes
4
answers
112k
views
std::lock_guard or std::scoped_lock?
C++17 introduced a new lock class called std::scoped_lock.
Judging from the documentation it looks similar to the already existing std::lock_guard class.
What's the difference and when should I use ...
283
votes
3
answers
278k
views
What exactly is std::atomic?
I understand that std::atomic<> is an atomic object. But atomic to what extent? To my understanding an operation can be atomic. What exactly is meant by making an object atomic? For example if ...
222
votes
7
answers
341k
views
Mutex example / tutorial? [closed]
I was trying to understand how mutexes work. Did a lot of Googling but it still left some doubts of how it works because I created my own program in which locking didn't work.
One absolutely non-...
200
votes
12
answers
339k
views
Thread pooling in C++11
Relevant questions:
About C++11:
C++11: std::thread pooled?
Will async(launch::async) in C++11 make thread pools obsolete for avoiding expensive thread creation?
About Boost:
C++ boost thread ...
200
votes
7
answers
83k
views
What happens to a detached thread when main() exits?
Assume I'm starting a std::thread and then detach() it, so the thread continues executing even though the std::thread that once represented it, goes out of scope.
Assume further that the program does ...
191
votes
9
answers
72k
views
Why is volatile not considered useful in multithreaded C or C++ programming?
As demonstrated in this answer I recently posted, I seem to be confused about the utility (or lack thereof) of volatile in multi-threaded programming contexts.
My understanding is this: any time a ...
191
votes
5
answers
128k
views
C++11 std::thread vs Posix threads
Why should I prefer one or another in practice?
What are the technical differences except that std::thread is a class?
190
votes
3
answers
140k
views
What does the thread_local mean in C++11?
I am confused with the description of thread_local in C++11. My understanding is, each thread has unique copy of local variables in a function. The global/static variables can be accessed by all the ...
190
votes
6
answers
71k
views
Is Meyers' implementation of the Singleton pattern thread safe?
Is the following implementation, using lazy initialization, of Singleton (Meyers' Singleton) thread safe?
static Singleton& instance()
{
static Singleton s;
return s;
}
If not, why and ...
189
votes
13
answers
25k
views
Is incrementing an int effectively atomic in specific cases?
In general, for int num, num++ (or ++num), as a read-modify-write operation, is not atomic. But I often see compilers, for example GCC, generate the following code for it (try here):
void f()
{
int ...
187
votes
4
answers
45k
views
What is the difference between packaged_task and async
While working with the threaded model of C++11, I noticed that
std::packaged_task<int(int,int)> task([](int a, int b) { return a + b; });
auto f = task.get_future();
task(2,3);
std::cout <&...
184
votes
8
answers
345k
views
How do I terminate a thread in C++11?
I don't need to terminate the thread correctly, or make it respond to a "terminate" command. I am interested in terminating the thread forcefully using pure C++11.
172
votes
5
answers
74k
views
When to use volatile with multi threading?
If there are two threads accessing a global variable then many tutorials say make the variable volatile to prevent the compiler caching the variable in a register and it thus not getting updated ...
159
votes
7
answers
46k
views
Is it smart to replace boost::thread and boost::mutex with c++11 equivalents?
Motivation: reason why I'm considering it is that my genius project manager thinks that boost is another dependency and that it is horrible because "you depend on it"(I tried explaining the quality ...
153
votes
6
answers
206k
views
C++ terminate called without an active exception
I am getting a C++ error with threading:
terminate called without an active exception
Aborted
Here is the code:
#include <queue>
#include <thread>
#include <mutex>
#include <...
152
votes
12
answers
108k
views
C++0x has no semaphores? How to synchronize threads?
Is it true that C++0x will come without semaphores? There are already some questions on Stack Overflow regarding the use of semaphores. I use them (posix semaphores) all the time to let a thread wait ...
145
votes
1
answer
56k
views
Does async(launch::async) in C++11 make thread pools obsolete for avoiding expensive thread creation?
It is loosely related to this question: Are std::thread pooled in C++11?. Though the question differs, the intention is the same:
Question 1: Does it still make sense to use your own (or 3rd-party ...
142
votes
17
answers
106k
views
Windows threading: _beginthread vs _beginthreadex vs CreateThread C++
What's a better way to start a thread, _beginthread, _beginthreadx or CreateThread?
I'm trying to determine what are the advantages/disadvantages of _beginthread, _beginthreadex and CreateThread. ...
125
votes
9
answers
58k
views
How can I propagate exceptions between threads?
We have a function which a single thread calls into (we name this the main thread). Within the body of the function we spawn multiple worker threads to do CPU intensive work, wait for all threads to ...
125
votes
9
answers
170k
views
How to check if a std::thread is still running?
How can I check if a std::thread is still running (in a platform independent way)?
It lacks a timed_join() method and joinable() is not meant for that.
I thought of locking a mutex with a std::...
118
votes
6
answers
110k
views
Example for boost shared_mutex (multiple reads/one write)?
I have a multithreaded app that has to read some data often, and occasionally that data is updated. Right now a mutex keeps access to that data safe, but it's expensive because I would like multiple ...
117
votes
13
answers
156k
views
How to get integer thread id in c++11
c++11 has a possibility of getting current thread id, but it is not castable to integer type:
cout<<std::this_thread::get_id()<<endl;
output : 139918771783456
cout<<(uint64_t)std::...
114
votes
4
answers
58k
views
Get the status of a std::future
Is it possible to check if a std::future has finished or not? As far as I can tell the only way to do it would be to call wait_for with a zero duration and check if the status is ready or not, but is ...
113
votes
7
answers
47k
views
Do I have to acquire lock before calling condition_variable.notify_one()?
I am a bit confused about the use of std::condition_variable. I understand I have to create a unique_lock on a mutex before calling condition_variable.wait(). What I cannot find is whether I should ...
112
votes
4
answers
72k
views
Passing object by reference to std::thread in C++11
Why can't you pass an object by reference when creating a std::thread ?
For example the following snippit gives a compile error:
#include <iostream>
#include <thread>
using namespace ...
109
votes
5
answers
43k
views
Understanding std::atomic::compare_exchange_weak() in C++11
bool compare_exchange_weak (T& expected, T val, ..);
compare_exchange_weak() is one of compare-exchange primitives provided in C++11. It's weak in the sense that it returns false even if the ...
107
votes
1
answer
193k
views
std::thread calling method of class [duplicate]
Possible Duplicate:
Start thread with member function
I have a small class:
class Test
{
public:
void runMultiThread();
private:
int calculate(int from, int to);
}
How its possible to run ...
103
votes
6
answers
87k
views
When to use std::async vs std::threads?
Can anybody give a high level intuition about when to use each of them?
References:
Is it smart to replace boost::thread and boost::mutex with c++11 equivalents?
When is it a good idea to use std::...
97
votes
16
answers
41k
views
Are C++ Reads and Writes of an int Atomic? [duplicate]
I have two threads, one updating an int and one reading it. This is a statistic value where the order of the reads and writes is irrelevant.
My question is, do I need to synchronize access to this ...
97
votes
3
answers
14k
views
Understanding std::hardware_destructive_interference_size and std::hardware_constructive_interference_size
C++17 added std::hardware_destructive_interference_size and std::hardware_constructive_interference_size. First, I thought it is just a portable way to get the size of a L1 cache line but that is an ...
95
votes
16
answers
27k
views
I've heard i++ isn't thread safe, is ++i thread-safe?
I've heard that i++ isn't a thread-safe statement since in assembly it reduces down to storing the original value as a temp somewhere, incrementing it, and then replacing it, which could be ...
93
votes
5
answers
19k
views
c++, std::atomic, what is std::memory_order and how to use them?
Can anyone explain what is std::memory_order in plain English, and how to use them with std::atomic<>?
I found the reference and few examples here, but don't understand at all.
http://en....
92
votes
7
answers
182k
views
C++11 thread-safe queue
A project I'm working on uses multiple threads to do work on a collection of files. Each thread can add files to the list of files to be processed, so I put together (what I thought was) a thread-safe ...
92
votes
5
answers
67k
views
What are the correct link options to use std::thread in GCC under linux?
Hi I am trying to use std::thread with G++. Here is my test code
#include <thread>
#include <iostream>
int main(int, char **){
std::thread tt([](){ std::cout<<"Thread!"<<...
91
votes
13
answers
18k
views
Does the C++ volatile keyword introduce a memory fence?
I understand that volatile informs the compiler that the value may be changed, but in order to accomplish this functionality, does the compiler need to introduce a memory fence to make it work?
From ...
86
votes
13
answers
115k
views
Reader/Writer Locks in C++
I'm looking for a good reader/writer lock in C++. We have a use case of a single infrequent writer and many frequent readers and would like to optimize for this. Preferable I would like a cross-...
86
votes
2
answers
3k
views
Is armadillo solve() thread safe?
In my code I have loop in which I construct and over determined linear system and try to solve it:
#pragma omp parallel for
for (int i = 0; i < n[0]+1; i++) {
for (int j = 0; j < n[1]+1; j++...
84
votes
3
answers
25k
views
C++11: why does std::condition_variable use std::unique_lock?
I am a bit confused about the role of std::unique_lock when working with std::condition_variable. As far as I understood the documentation, std::unique_lock is basically a bloated lock guard, with the ...
83
votes
4
answers
182k
views
Pass multiple arguments into std::thread
I'm asking the <thread> library in C++11 standard.
Say you have a function like:
void func1(int a, int b, ObjA c, ObjB d){
//blahblah implementation
}
int main(int argc, char* argv[]){
...
82
votes
3
answers
121k
views
What's the difference between "static" and "dynamic" schedule in OpenMP?
I started working with OpenMP using C++.
I have two questions:
What is #pragma omp for schedule?
What is the difference between dynamic and static?
Please, explain with examples.
81
votes
5
answers
7k
views
Why is the != operator not allowed with OpenMP?
I was trying to compile the following code:
#pragma omp parallel shared (j)
{
#pragma omp for schedule(dynamic)
for(i = 0; i != j; i++)
{
// do something
}
}
but I got the following ...
81
votes
2
answers
11k
views
What does the [[carries_dependency]] attribute mean?
Can someone explain it in a language that mere mortals understand?
80
votes
4
answers
54k
views
Concurrency: Atomic and volatile in C++11 memory model
A global variable is shared across 2 concurrently running threads on 2 different cores. The threads writes to and read from the variables. For the atomic variable can one thread read a stale value? ...
79
votes
18
answers
73k
views
Circular lock-free buffer
I'm in the process of designing a system which connects to one or more stream of data feeds and do some analysis on the data than trigger events based on the result. In a typical multi-threaded ...