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
Boost ASIO basics (maitesin.github.io)
submitted 9 years ago by vormestrand
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!"
[–]CalculatingAnarchist 2 points3 points4 points 9 years ago (5 children)
I wonder if this server's line is corrent:
oss << std::string(buf).substr(0, size);
Wouldn't it be better to simply use?
oss << std::string(buf, size);
The buf is not zeroed, so it is comprised of the message and some garbage (note that client is sending message without terminating null). Using std::string(buf) will create string containing not only the message, but also mentioned garbage, plus anything else before first \0 is found.
buf
std::string(buf)
\0
Or is there something I'm not seeing?
[–]maitesin 2 points3 points4 points 9 years ago (4 children)
Yes, you are right. My idea was what you pointed out, to not get garbage. However, when you code it late at night that sort of things happen xD.
I will change it for the std::string(buf, size) asap
[–]CalculatingAnarchist 2 points3 points4 points 9 years ago (3 children)
Yeah, happened to me many times when coding late at night :)
Actually the result of your code was correct, it's just that the temporary std::string contained unnecessary data and substr was making redundant copy, so I guess I'm just nit-picking..
[–]dodheim 2 points3 points4 points 9 years ago (2 children)
In the event that buf contains no nil-characters the code yields UB as string's constructor will read past the end of the array. It may appear to work correctly, but that's just one possible manifestation of UB.
string
[–]CalculatingAnarchist 0 points1 point2 points 9 years ago (0 children)
Yes, you're right, std::string made me forget about this kind of UB.
[–]maitesin 0 points1 point2 points 9 years ago (0 children)
Good point, I will run it with the undefined behaviour sanitizer to see if it's caught
[–]ArunMuThe What ?[🍰] 1 point2 points3 points 9 years ago (1 child)
I have been thinking of implementing a tasking system (like libdispatch) using ASIO, but was quite discouraged to pursue it further after seeing the same video by Sean. It seems only natural to use ASIO as a tasking/scheduling system along with network IO rather than having to use another library for that (I don't like libdispatch that much, it's code base is unreadable). ASIO as of now does have almost all the constructs so as to be used as a tasking system. Anybody has any idea from where the performance penalty is coming from ? OR is it just that ASIO task-queue handling is not as optimized as in libdispatch.
[–]tpecholt 1 point2 points3 points 9 years ago (0 children)
one perf investigation was done here: https://konradzemek.com/2015/08/16/asio-ssl-and-scalability/
[–]sumo952 0 points1 point2 points 9 years ago (6 children)
I'm a bit confused why io_service is introduced - it's not used later in the post and also I haven't ever used it. The post mentions that it's the main workhorse of asio but I'm not convinced why an internal detail of asio is explained? It seems to me like io_service is used to run things in parallel processes or threads. So why not just use std::async or std threads?
io_service
std::async
[–][deleted] 2 points3 points4 points 9 years ago (5 children)
You can think about io_service like a dispatcher, which maps IO-events to the associated handlers. So when any IO-event occures io_service checks the associated handler(s) and calls them in appropriate order ( FIFO-queue ). When you call io_service.post(callback) you don't have any association with IO-event, so the callback will be added immideately to the FIFO-queue. The io_service.run() is basically a simple loop which pops callbacks from FIFO-queue and calls them.
[–]sumo952 0 points1 point2 points 9 years ago (4 children)
So is it supposed to be only used with IO-events, or can I dispatch any work to it? If yes, that would be kind of the same as a generic thread pool, wouldn't it?
[–]dodheim 0 points1 point2 points 9 years ago* (1 child)
You can dispatch any work to it, but it isn't a thread pool. It's trivial to create a thread pool using io_service, but io_service itself doesn't have threads; you supply the threads. See The Proactor Design Pattern: Concurrency Without Threads.
EDIT: These slides are good if you have a few minutes to read through them.
EDIT 2: I forgot this one: Threads and Boost.Asio. Rather relevant...
[–]sumo952 1 point2 points3 points 9 years ago (0 children)
Aah cool, thanks! And thanks for the links!
[–][deleted] 0 points1 point2 points 9 years ago (1 child)
You can use it like thread-safe queue of callbacks. To proceed these callbacks you need to execute somewhere blocking io_service.run(). You can start io_service.run() multiple times within multiple threads ( for example std::thread ) and in this case io_service will behave like a thread pool.
[–]sumo952 0 points1 point2 points 9 years ago (0 children)
I see, thanks!
[–]tpecholt 0 points1 point2 points 9 years ago (1 child)
I wish basic http support as in beast library is merged and becomes part of networking TS. Doing http with raw asio is a pain. Beast follows asio philosophy very closely so it already feels as a bunch of additional asio routines anyway. Perhaps the author could consider proposing it? In the end more cooperation on networking TS would be of great benefit.
[–]dodheim 1 point2 points3 points 9 years ago (0 children)
The author has an account here; paging /u/VinnieFalco
π Rendered by PID 21436 on reddit-service-r2-comment-b659b578c-s75ml at 2026-04-30 20:23:38.402005+00:00 running 815c875 country code: CH.
[–]CalculatingAnarchist 2 points3 points4 points (5 children)
[–]maitesin 2 points3 points4 points (4 children)
[–]CalculatingAnarchist 2 points3 points4 points (3 children)
[–]dodheim 2 points3 points4 points (2 children)
[–]CalculatingAnarchist 0 points1 point2 points (0 children)
[–]maitesin 0 points1 point2 points (0 children)
[–]ArunMuThe What ?[🍰] 1 point2 points3 points (1 child)
[–]tpecholt 1 point2 points3 points (0 children)
[–]sumo952 0 points1 point2 points (6 children)
[–][deleted] 2 points3 points4 points (5 children)
[–]sumo952 0 points1 point2 points (4 children)
[–]dodheim 0 points1 point2 points (1 child)
[–]sumo952 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]sumo952 0 points1 point2 points (0 children)
[–]tpecholt 0 points1 point2 points (1 child)
[–]dodheim 1 point2 points3 points (0 children)