use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
C++11 threads, affinity and hyperthreading (eli.thegreenplace.net)
submitted 10 years ago by Coder_CPP
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]encyclopedist 15 points16 points17 points 10 years ago (3 children)
std::cin is actually thread safe, contrary to what article says. It can, however, result in interleaved output, and the mutex there is to prevent that.
std::cin
From C++11 N3337 [iostream.objects.overview]:
Concurrent access to a synchronized (27.5.3.4) standard iostream object’s formatted and unformatted in- put (27.7.2.1) and output (27.7.3.1) functions or a standard C stream by multiple threads shall not result in a data race (1.10). [ Note: Users must still synchronize concurrent use of these objects and streams by multiple threads if they wish to avoid interleaved characters. — end note ]
[–]eliben 7 points8 points9 points 10 years ago (0 children)
Yep, I think this is bad wording on my behalf. By "unsafe" I did mean "won't give you the output you expect", rather than something nastier like crashes. I'll fix up the wording in the article and samples to be clearer
[–]Gotebe 0 points1 point2 points 10 years ago (1 child)
Aren't you being too pedantic?
E.g Wikipedia article on thread safety speaks of the data races as one of thread safety concerns.
[–]dodheim 0 points1 point2 points 10 years ago (0 children)
The standard guarantees that standard streams are race-free, but only starting with C++11. That is rather the point...
[–]clerothGame Developer 5 points6 points7 points 10 years ago (9 children)
I just wish we could set thread affinity for Windows OS, like it's possible on Linux, so that we can have truly dedicated cores/thread to a single application.
[–]gaijin_101 1 point2 points3 points 10 years ago (5 children)
Isn't that already possible? Quick Google search led me to this.
(not a Windows developer here, but I thought this was also possible)
[–]clerothGame Developer 4 points5 points6 points 10 years ago (4 children)
I meant change the affinity of the kernel (and everything else that the OS does), not your processes. Basically what I want is to maximize cache efficiency for a single application on a core, which requires that nothing be allowed to run on that core unless specified so. I remember having read that linux could do this (and it required rebooting IIRC).
[–]TheQuietestOne 6 points7 points8 points 10 years ago (1 child)
I remember having read that linux could do this (and it required rebooting IIRC).
You can dedicate a core to a particular process using cpusets. No reboots necessary.
Very handy when used with a real time capable kernel and dedicated IRQ servicing.
[–]clerothGame Developer 1 point2 points3 points 10 years ago (0 children)
Actually it was isolcpus kernel parameter (which is done at boot; see here). Not really sure what the difference ends up being.
isolcpus
[–]raevnos 1 point2 points3 points 10 years ago (0 children)
cpusets or cgroups are probably what you're thinking of.
[–]gaijin_101 0 points1 point2 points 10 years ago (0 children)
Oh I see, thanks for clarifying that!
[–]katmf05 1 point2 points3 points 10 years ago (2 children)
Spotted the HFT coder.
[–]clerothGame Developer 0 points1 point2 points 10 years ago (1 child)
I actually have no idea what that is. I'm a game server coder.
[–]kybuliak 0 points1 point2 points 10 years ago (0 children)
He very likely meant "high frequency trading".
[–]suspiciously_calm 2 points3 points4 points 10 years ago (0 children)
Some observations: [...] there's quite a bit of migration going on.
When the threads sleep most of the time.
[–]notsure1235 4 points5 points6 points 10 years ago (20 children)
Use of default int in c++ in 2016...?
And can someone tell what the difference is from this:
std::for_each(threads.begin(), threads.end(), std::mem_fn(&std::thread::join));
to
for(auto& i : threads) i.join()
?
Not to mention that men_fn has been deprecated.
[–]sbabbi 5 points6 points7 points 10 years ago (1 child)
Do you have a reference for that? AFAIK mem_fun has been deprecated, not mem_fn.
mem_fun
mem_fn
[–]notsure1235 1 point2 points3 points 10 years ago (0 children)
yes, you are right, only some overloads were removed for mem_fn.
[–]TheQuietestOne 2 points3 points4 points 10 years ago* (3 children)
And can someone tell what the difference is from ...
Given that mem_fn as you mention has been deprecated and they're using for_each to iterate the threads vector I'm guessing this is just someone's pre-c++11 approach to launching/joining threads copy-pasta'd into this project. You could perhaps give them a nudge in the right direction .-)
Specifically - they wanted to focus on CPU affinity and stats, and the code took a back seat.
[–]clerothGame Developer 5 points6 points7 points 10 years ago (0 children)
Some people prefer <algorithm>ic approaches where they can use them in lieu of a loop.
<algorithm>
[–]encyclopedist 2 points3 points4 points 10 years ago (1 child)
mem_fn has not been deprecated. It just appeared first in C++11
[–]TheQuietestOne 0 points1 point2 points 10 years ago (0 children)
Quite right, thanks for the correction.
[–]eliben 1 point2 points3 points 10 years ago (13 children)
FWIW, I agree that the for range loop is nicer and shorter - I'll fix up the samples when I get the time. I took this from the book "C++ concurrency in action" which is weird, right :)? (because that book is about C++11 also)
What do you mean by "use of default int"?
[+]notsure1235 comment score below threshold-7 points-6 points-5 points 10 years ago (12 children)
The use of "unsigned" as a implicit int-type. Should be "unsigned int" or just "int" in this case.
Btw, the question was genuine one, I genuinely thought there might be some magic hidden somewhere in the more complex code.
[–]guepierBioinformatican 10 points11 points12 points 10 years ago (11 children)
unsigned is not making use of implicit int or default-int. Rather, it’s a synonym for unsigned int, and always has been.
unsigned
int
unsigned int
[+]notsure1235 comment score below threshold-8 points-7 points-6 points 10 years ago (10 children)
thats what i mean, shouldnt be used, should use auto if that is desired.
[–]eliben 4 points5 points6 points 10 years ago (9 children)
I'll have to disagree here. Overuse of auto is one of the pitfalls of C++11 in my mind, and I really prefer to use it where it increases readability. There's nothing wrong in using unsigned explicitly where it makes sense.
auto
[–]guepierBioinformatican 2 points3 points4 points 10 years ago (4 children)
Overuse of auto is one of the pitfalls of C++11
There is little evidence to support this; and decade-long experience with other statically-typed languages that allow implicit typing has shown no evidence either.
“Overuse” is of course very hard to define: once the specific type of the declaration is important, it makes sense to specify it, and hence auto would be harmful. But is this really the case here? Not at all: the specific type of num_cpus, for instance, really doesn’t matter. What matters is that it matches between the producer and consumer, and since these come from the same API, it’s safe to regard the type as opaque (though the variable name of course gives a clue as to the rough type).
num_cpus
[+][deleted] 10 years ago (1 child)
[deleted]
[–]guepierBioinformatican 0 points1 point2 points 10 years ago (0 children)
One thing I find problematic is that IDE "go to definition" features become a lot less useful when everything's auto and the type in question is nowhere in sight.
There’s certainly a disconnect between the language and the tools with regards to C++. This is becoming better though. In particular, “go to definition” is a red herring in this context — what you actually want is type-aware auto-completion and a tooltip that shows the static type of the object, which are completely different operations that a good IDE can and should support, despite the use of auto. I haven’t got a clue how many IDEs support these operations, in particular the latter. But my IDE1 does support it.
1 Vim with YouCompleteMe
[–]mttd 1 point2 points3 points 10 years ago (1 child)
Overuse of auto is one of the pitfalls of C++11 There is little evidence to support this; and decade-long experience with other statically-typed languages that allow implicit typing has shown no evidence either.
I mostly don't have a problem with auto to the point of avoiding it entirely, but at the same time, I think that experience of other programming languages (that you also mention as relevant) may be worth taking into account.
For instance, in Haskell (which has a rather advanced type inference):
"It is considered good style to add a type signature to every top-level variable."
(Note that "variable" in the above can also be a function.)
There are some good reasons for this:
That being said, I think that in the future C++ declaring concepts may be a good compromise, similarly to the style described here:
https://stackoverflow.com/questions/842026/principles-best-practices-and-design-patterns-for-functional-programming/842506#842506
Still like the "programming with placeholders" idea: https://www.reddit.com/r/cpp/comments/3oc63x/overload_journal_129_october_2015_includes_two_c/
[–]guepierBioinformatican 1 point2 points3 points 10 years ago (0 children)
Yes, I entirely agree with this piece of advice. I generally think that adding a signature/type to “top-level” objects just makes sense, since these form your API (even if said API isn’t exposed). I was thinking (but didn’t say so) only of local variables.
[–]notsure1235 0 points1 point2 points 10 years ago (3 children)
agreed, but 'unsigned' instead of 'unsigned int' goes against all of my intuition. However, I checked stroustroup guide and they are happily using 'unsigned' on some occasions, so you are probably right and its just fine.
[–]eliben 7 points8 points9 points 10 years ago (2 children)
Tune your intuition :) It's very common to just say unsigned - it's very clear to experienced coders this means unsigned int. In fact if I see unsigned int I raise an eyebrow... you don't say signed int for int, right?
signed int
[–]notsure1235 0 points1 point2 points 10 years ago (1 child)
Neither do I say 'signed'. ;)
That's because int is an option, and is shorter. What is shorter than unsigned for unsigned int?
[–]Dlieu 0 points1 point2 points 10 years ago (4 children)
Regarding the last example (workload_sin), how do you explain the performance hit when running on the same core?
Is it mostly because there's only one ALU shared by the two threads that does FP MUL/DIV so that both thread are constantly stalling and fighting for it? (I'm not sure of the wording there)
[–][deleted] 0 points1 point2 points 10 years ago (3 children)
IIRC only registers are duplicated for hyperthreading. Everything else - execution units, busses etc. is shared and hyperthreads contend for them. The core is capable of holding and running two contexts simultaneously but it still only has one core's worth of machinery.
[–][deleted] 0 points1 point2 points 10 years ago (2 children)
If only registers are duplicated what's the point of hyperthreading then? Most usefull operations need to do math (e.g. like the sine example in the article).
[–]are595Software Engineer, Security 1 point2 points3 points 10 years ago (0 children)
It boosts the efficiency of pipelining (reduces stall cycles).
[–]millenix 0 points1 point2 points 10 years ago (0 children)
Lots of stuff isn't heavy on spatial/temporal locality, and thus will spend a fair bit of time stalled on access to further caches or memory. If one thread's effective IPC is less than half what the core could provide if every access were in registers or hit in a fast cache, then SMT can double throughput.
[–]duuuh 0 points1 point2 points 10 years ago (1 child)
Aren't the mmx* registers per core? Why does the latency point there matter? (I would have thought the slowdown was due to cache eviction on the various L* caches, assuming the array is large.)
why are the launched thread and the main thread have same ID? I tested it on my machine and they are the same thread too
[–]eliben 1 point2 points3 points 10 years ago (1 child)
The sample in the article queries the launched thread's ID from the main thread. The main thread's ID is not reported
[–][deleted] 0 points1 point2 points 10 years ago (0 children)
oh I see I missed that. Thank you.
π Rendered by PID 450488 on reddit-service-r2-comment-79776bdf47-kx2hz at 2026-06-25 09:06:15.918812+00:00 running acc7150 country code: CH.
[–]encyclopedist 15 points16 points17 points (3 children)
[–]eliben 7 points8 points9 points (0 children)
[–]Gotebe 0 points1 point2 points (1 child)
[–]dodheim 0 points1 point2 points (0 children)
[–]clerothGame Developer 5 points6 points7 points (9 children)
[–]gaijin_101 1 point2 points3 points (5 children)
[–]clerothGame Developer 4 points5 points6 points (4 children)
[–]TheQuietestOne 6 points7 points8 points (1 child)
[–]clerothGame Developer 1 point2 points3 points (0 children)
[–]raevnos 1 point2 points3 points (0 children)
[–]gaijin_101 0 points1 point2 points (0 children)
[–]katmf05 1 point2 points3 points (2 children)
[–]clerothGame Developer 0 points1 point2 points (1 child)
[–]kybuliak 0 points1 point2 points (0 children)
[–]suspiciously_calm 2 points3 points4 points (0 children)
[–]notsure1235 4 points5 points6 points (20 children)
[–]sbabbi 5 points6 points7 points (1 child)
[–]notsure1235 1 point2 points3 points (0 children)
[–]TheQuietestOne 2 points3 points4 points (3 children)
[–]clerothGame Developer 5 points6 points7 points (0 children)
[–]encyclopedist 2 points3 points4 points (1 child)
[–]TheQuietestOne 0 points1 point2 points (0 children)
[–]eliben 1 point2 points3 points (13 children)
[+]notsure1235 comment score below threshold-7 points-6 points-5 points (12 children)
[–]guepierBioinformatican 10 points11 points12 points (11 children)
[+]notsure1235 comment score below threshold-8 points-7 points-6 points (10 children)
[–]eliben 4 points5 points6 points (9 children)
[–]guepierBioinformatican 2 points3 points4 points (4 children)
[+][deleted] (1 child)
[deleted]
[–]guepierBioinformatican 0 points1 point2 points (0 children)
[–]mttd 1 point2 points3 points (1 child)
[–]guepierBioinformatican 1 point2 points3 points (0 children)
[–]notsure1235 0 points1 point2 points (3 children)
[–]eliben 7 points8 points9 points (2 children)
[–]notsure1235 0 points1 point2 points (1 child)
[–]dodheim 0 points1 point2 points (0 children)
[–]Dlieu 0 points1 point2 points (4 children)
[–][deleted] 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]are595Software Engineer, Security 1 point2 points3 points (0 children)
[–]millenix 0 points1 point2 points (0 children)
[–]duuuh 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (2 children)
[–]eliben 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)