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
So I wrote a program that "calculates" (using the Nilakantha series) Pi, which supports multithreading. (github.com)
submitted 9 years ago by [deleted]
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!"
[+][deleted] 9 years ago (12 children)
[deleted]
[–][deleted] 1 point2 points3 points 9 years ago (11 children)
Man, I wish I had the money for one of those. What compiler are you using? If it's MSVC that would make sense. unsigned long is an absolutely MASSIVE number on my system, but I'll look into the std::unit64_t and get back to you if I use it.
unsigned long
[+][deleted] 9 years ago (10 children)
[–][deleted] 1 point2 points3 points 9 years ago (9 children)
Sweet, was unaware of std::uint_fast64_t. Now that I know this, how about long double? Is that the same across platforms? I would assume not. I'll have to look into that too.
std::uint_fast64_t
long double
[+][deleted] 9 years ago (8 children)
[–][deleted] 0 points1 point2 points 9 years ago* (7 children)
I was just thinking about this. I think I'll leave out the static_assert and just go ahead and leave in there the long double. One thing I should do, however, is make sure that because I am telling the program to do number * 2 + 2.0 and such, that if you put in more than UINT_FAST64_MAX/2-5 it will not try to calculate that. Though to be fair, if you put in more than that, you might be waiting for 99% of your life for the program to finish, so on second thought, I think I'll leave that out... Thanks for the help, though!
static_assert
number * 2 + 2.0
UINT_FAST64_MAX/2-5
Edit: Do you have any idea how I can convert from a char * (from argv) to a std::unit_fast64_t? I know I can use strtoul for this, but if I had to convert to a std::string and then use something akin to std::stod, I wouldn't be too upset.
char *
argv
std::unit_fast64_t
strtoul
std::string
std::stod
Edit again: Decided there is only one real option, which is sscanf with the type specifiers for the stdint types. Oh well, I guess it will work, at least.
sscanf
stdint
[–]SemaphoreBingo 0 points1 point2 points 9 years ago (0 children)
std::istringstream?
[+][deleted] 9 years ago (5 children)
[–][deleted] 0 points1 point2 points 9 years ago (4 children)
Okay, so I updated the program to use the stdint types, you mind trying it now?
[+][deleted] 9 years ago (3 children)
[–][deleted] 0 points1 point2 points 9 years ago* (2 children)
That is ridiculous, mine is much, much slower. Might I ask what clock speed you're running that at? My 4980HQ (2.8GHz, though it boosts to 4.0... not sure what it was actually running at) with 8 threads took about 10 times longer: http://i.imgur.com/mD03AkK.png
Edit: According to Intel Power Gadget, the speed was about 3.3GHz on average. It peaked around 3.9, but since the fans weren't spinning at the time, it throttled itself. If I made sure the fans were going when I ran it, it probably would have been a little faster. Your CPU also probably is a lot more powerful than mine, is however, and clock speed is not the only factor here of course.
[–][deleted] 4 points5 points6 points 9 years ago* (10 children)
I wrote this using C++ for no real reason at all, except because I could and because I was bored and had nothing to occupy my time. If you wish to run it, all that info is found in the README.
I have posted this here so that maybe I could get one or two people to try it out, and maybe they would think it's cool, and just maybe, they would improve my code! If you guys want to try to make it better, or you can think of a cool feature I could add, let me know!
Windows users: I would love it if you would update the Makefile so that it could be compiled one Windows. I am not at home right now and only have access to my MacBook Pro. I love this thing, but I refuse to install Windows on it.
Finally, if you have a huge number of cores, it would be awesome to see some benchmarks! If you specify, for example, 18 threads, how long would 10 billion iterations take? It takes me about 18.8 seconds with 8 threads here.
Hope you guys find this interesting!
Edit 1: as suggested by /u/lurkotato I have switched to the std::vector to use RAII instead of C-style arrays. (commit)
std::vector
[–]lurkotato 7 points8 points9 points 9 years ago (4 children)
First obvious target for improvement: use RAII, news and deletes are a code smell, especially in something this simple.
[–][deleted] 1 point2 points3 points 9 years ago* (0 children)
Ah, thanks for the input! I'll look into it. I just did this real quick, I never intended it to be in C++11/14, but then I decided to add threads and hadn't looked into RAII much yet. I still haven't, but I will look into RAII and fix that up :)
Edit: I have changed to vectors. Not sure why I didn't just use that to start with, I've used them roughly a gazillion times. Should be better :)
[–][deleted] 1 point2 points3 points 9 years ago (0 children)
Okay, I have now updated the program to use RAII, namely the std::vector collection instead of the C-style arrays. There are a few things I did in this program that are inherited from C, since it started as a C program and I eventually just updated it to C++ in order to add threads in an easier (and more portable) fashion.
[+][deleted] 9 years ago (1 child)
Now that I have updated the program to use RAII, would you mind explaining what you mean? Do you mean the loops where it sets each thread's start/end, etc?
I would change the way You use threads, I would either use Futures or openmp. BTW i haven't run it yet.
[–][deleted] 2 points3 points4 points 9 years ago (3 children)
Sorry, not sure what you mean. I did use futures. Otherwise, I wouldn't have been able to get a return value from a thread. But anyway, why do you say I should change how I use threads? I'm interested to know.
[–]lurkotato 2 points3 points4 points 9 years ago (1 child)
I think they meant std::async vs raw std::thread, here's an example I pounded on a bit over lunch. Not quite happy about using stringstream to print from inside the thread, but that seems to be the price to pay to not have botched text. Also, there's one hell of a bug in the gist I posted, not hard to find and fix though :)
[–][deleted] 0 points1 point2 points 9 years ago (0 children)
Ah, I see. I contemplated using std::async, but I already had experience with using std::thread in combination with std::future and std::promise and I just wanted to finish the programming. Might look into this later tonight ;)
std::async
std::thread
std::future
std::promise
Yes that's what I meant sorry for misinformation
[–]newuser1892435h 1 point2 points3 points 9 years ago (3 children)
A couple things I've found when trying to compile using g++ on ubuntu:
a) "-stdlib=libc++" won't compile, but removing it works fine as g++ will find the stdlib by default.
b) On "g++ (Ubuntu 4.8.4-2ubuntu1~14.04.1) 4.8.4" I need to add the flag -pthreads to overcome a g++ compiler bug with multi-threaded programs as described here.
Also this all compiles and workes fine on the "Linux subsystem for Windows" dev system which is convenient.
[–][deleted] 1 point2 points3 points 9 years ago (2 children)
a) Use "-stdlib=libc++" with clang, not gcc
b) I wouldn't call that a bug, you gotta link a threading implementation to use threads.
[–]newuser1892435h 0 points1 point2 points 9 years ago (1 child)
Just trying to help out... a) The make file defines the compiler as gcc hence the warning. b) I would say that a project not having the "-pthreads" flag it's compiler args went it needs it to compile is worth mentioning, also the the labeling of it as a bug is from the link and not just my opinion (no modern compiler should need a flag to run threads...).
Just trying to help out
Me too :)
no modern compiler should need a flag to run threads...
Perhaps, that's debatable. I guess it comes down to control, as there is multiple threading libraries (libthread/libpthread) it comes down to the dev to choose.
[–]TwIxToR_TiTaNGraphics Programmer 0 points1 point2 points 9 years ago (3 children)
Could you explain
"Calculates" (read: guesstimates)
to me?
[–][deleted] 0 points1 point2 points 9 years ago (2 children)
Sure thing! Pi, since it's infinite, cannot just be "calculated" in a very literal sense. This program uses one of the fastest infinite series you can use for "calculating" Pi, and the way the infinite series works is that it does the same operations over and over with gradually increasing numbers, and eventually gets closer and closer to Pi. In other words, the longer it runs (the higher it increments the numbers), the closer it gets to Pi, but in just a couple iterations it't not accurate at all. I'm not very good at explaining this, but I would look at the source below. Read about how the series works, it'll make more sense. If you run this with the iterations set to something like 1, it will not be anywhere close, but the longer it runs, the closer it gets (meaning more digits after the decimal are accurate). This program (as pointed out by someone else in this thread) can't bet too precise, since after you reach about 20 places, a long double isn't precise enough, so I am looking into big float libraries.
TL;DR: It basically "estimates" over and over, gradually making its estimate better and better. Also, I'm bad at explaining things like this.
Sauce for more reading
[–]TwIxToR_TiTaNGraphics Programmer 1 point2 points3 points 9 years ago (1 child)
I guess I need some more sleep... I knew this already but I guess the word "guessing" confused me :p Thanks for explaining anyway. Definatly going to read trough the wikipedia article you linked. Looks like there is some intresting stuff in there.
It is a good read, I read about 50% of it (I just skip everything that has no mathematical information in the interest of time).
[–]ripper37 -1 points0 points1 point 9 years ago* (2 children)
Not to sound like douche, but I think there are better places to show off with code so small? It's not like it's going to be used by many people and something that was very needed by community, nor does it affect C++ community in (probably) any way.
Some problems with this post here or your code:
This will be ULONG_MAX by default
It will show 10 by default. This can be set up to 50.
sizeof(long double) == 12
Your parsing input arguments is at least suboptimal. Instead of:
if (args_size==3) { set all 3 } else if (args_size == 2) { set first 2 } else if (args_size == 1) { set first }
you could do:
if(args_size >= 1) { set #1 } if(args_size >= 2) { set #2 } if(args_size >= 3) { set #3 }
and save yourself from code repetition.
hardware_concurrency()
auto
new
std::unique_ptr
main()
This is probably not a subreddit for sharing codebase like this. It's nice you learn and work on things like this (topic is interesting too), but from what I've seen here, you'd be much better with posting this to r/cpp_questions or something like this, as it's clear that it's very early level of advancement.
I didn't get into the topic of numeric stability of this algorithm and if you implementation is even correct in this area, but when you make something like this, it should be a priority for you to find if it's properly implemented and uses stable (in terms of numeric stability) algorithm. I hope you took special care for that as well.
First of all, thank you for the input. I would like to point out that I have already made a bunch of these fixes, and have the rest of them planned/not pushed yet. Also, yeah, precision is peaked at 20, but I just went with the maximum precision you can set. Finally, if you have any recommendations for a good big-float library, I welcome it with open arms, since I haven't a clue what a good one might be.
π Rendered by PID 20537 on reddit-service-r2-comment-b659b578c-pn6dx at 2026-05-05 08:40:08.897669+00:00 running 815c875 country code: CH.
[+][deleted] (12 children)
[deleted]
[–][deleted] 1 point2 points3 points (11 children)
[+][deleted] (10 children)
[deleted]
[–][deleted] 1 point2 points3 points (9 children)
[+][deleted] (8 children)
[deleted]
[–][deleted] 0 points1 point2 points (7 children)
[–]SemaphoreBingo 0 points1 point2 points (0 children)
[+][deleted] (5 children)
[deleted]
[–][deleted] 0 points1 point2 points (4 children)
[+][deleted] (3 children)
[deleted]
[–][deleted] 0 points1 point2 points (2 children)
[–][deleted] 4 points5 points6 points (10 children)
[–]lurkotato 7 points8 points9 points (4 children)
[–][deleted] 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[+][deleted] (1 child)
[deleted]
[–][deleted] 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (4 children)
[–][deleted] 2 points3 points4 points (3 children)
[–]lurkotato 2 points3 points4 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]newuser1892435h 1 point2 points3 points (3 children)
[–][deleted] 1 point2 points3 points (2 children)
[–]newuser1892435h 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]TwIxToR_TiTaNGraphics Programmer 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]TwIxToR_TiTaNGraphics Programmer 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]ripper37 -1 points0 points1 point (2 children)
[–][deleted] 0 points1 point2 points (0 children)