- Prefer auto to explicit type declarations - remember that auto + {expr} => std::initializer_list
- Prefer nullptr to 0 and Null
- Prefer scoped enums to unscoped enums
- Distinguish () and {} when creating objects - the latter disallows narrowing, that might be good
- Declare functions noexcept whenever possible - esp. swap move
- Make const member functions threadsafe - make them bitwise const or internally synchronised
- Distinguish "universal references" from r-value references - "universal references" is his phrase, just note if you see type&&& type might not be an r-value
- Avoid overloading on && - typically r-value ref versus l-value ref is ok, but just r-value ref might be trouble cos it's greedy
- Pass and return r-value refs via std::move, universal refs by std::forward - and allow RVO to happens as before in C++98/03
- Understand reference collapsing See SO
- Assume that move operations are not present, not cheap and not used
- Make std::thread unjoinable on all paths - even if there's an exception
- Use std::launch::async with std::async if asynchronicity is essential - but is it really essential?
- Be aware of varying thread handle destructive behaviour
- Create tasks not threads
- Consider void functions for one-shot event communication
- Pass parameterless functions to std::async, atd::thread}} and {{{std::call_once - the arguments are unconditionally copied, as with std::bind. Use a lambda instead
- Use native handles to transcend the C++11/14 API - if you need to configure your thread, but don't use a thread, so you won't need too
- Prefer lambdas to std::bind - inlining is possible
- Beware default captures in member functions - [=] captures the this pointer, and so member variables via this->variable, which could dangle and are "by ref" i.e. will match [&]. C++14 will add stuff to help
- Use std::make_shared and std::make_unique whenever possible
- Keep abreast of standardisation
Tuesday, 25 June 2013
Effective C++11/14
Work sent me on Scott Meyers' latest course at Developer Focus. He gave us these guidelines:
Wednesday, 22 May 2013
Hello concurrent world
From Anthony William's "C++ concurrency in action - practical multithreading", section 1.4.1 gives a simple "Hello world" program using C++11's thread.
#include <iostream>
#include <thread>
void hello()
{
std::cout <<"Hello concurrent world!\n";
}
int main()
{
std::thread t(hello);
t.join();
}
After Matthew Wilson re-starting his series in Overload, "Quality Matters #7 Exceptions: the story so far" http://accu.org/var/ uploads/journals/Overload114. pdf page 10ff, I had a nagging feeling I should put some exception handling round this.
First question, what happens if we make the hello throw an exception? For example, what would this do?
std::thread t_trouble( []{ throw std::exception("Oops");} );
It calls abort. The thread function mustn't let exceptions escape. Also, main should probably catch some exceptions; for example, maybe there aren't enough resources to start the thread yet.
#include <iostream>
#include <thread>
void hello()
{
try
{
std::cout <<"Hello concurrent world!\n";
}
catch(const std::exception& e)
{
//erm... what to do with it?
}
}
int main()
{
try
{
std::thread t(hello);//can I pass parameters? Nico says I can to async (page 964)
t.join(); //Nico says we can do a t.detach and when main exits it will get stopped
}
catch(const std::system_error& e) //pulled in by thread I presume
{
if(e.code() == std::errc::resource_ unavailable_try_again)
{
std::cout << "Try again\n";
}
}
catch(const std::exception& e)
{
std::cout << e.what() << '\n';
}
}
Right, so now we are ignoring any exceptions that get thrown.
What should I do with any exceptions I get in a function that's sent to a thread? I could use std::exception_ptr, and std::rethrow_exception when a client tries to get the result. It might be better if I read all of Anthony's book (esp Chapter 8) and use std::packaged_task instead.
accu-general (http://accu.org/index.php/mailinglists) helpfully told me to read all the chapters in the book concurrently.
Friday, 4 May 2012
Flashmob daily scrum
I think our team is too big to hold a daily scrum meeting, so I turned to a couple of people near me on Wednesday and asked "What did you do yesterday? What are you doing today? What's holding you up?"
I answered as well.
The next day, I did the same again with a different group of people, announcing "Flash-mob scrum" as we started.
Today I rounded up a couple of people from previous days and we "flash-mob scrummed" by two new people. I'm hoping it might just work.
This was done in a spirit of TCC, larking about, but based on previous practice, which is vital for TCC. The team seem to be talking to each other bit more too.
I answered as well.
The next day, I did the same again with a different group of people, announcing "Flash-mob scrum" as we started.
Today I rounded up a couple of people from previous days and we "flash-mob scrummed" by two new people. I'm hoping it might just work.
This was done in a spirit of TCC, larking about, but based on previous practice, which is vital for TCC. The team seem to be talking to each other bit more too.
Saturday, 31 December 2011
Mocking in C++
ACCU London's July 2011 talk was about mocking in C++, given by Ed Sykes and hosted by 7 City.
Ed talked about MockItNow and Hippomocks. He pointed out, as has been said many times before Mocks aren't Stubs.
I can no longer remember all the details so will have to try these out for myself to see how they work.
Many thanks to Ed for a great talk, though.
Ed talked about MockItNow and Hippomocks. He pointed out, as has been said many times before Mocks aren't Stubs.
I can no longer remember all the details so will have to try these out for myself to see how they work.
Many thanks to Ed for a great talk, though.
Sunday, 15 May 2011
Power series for PCA
The book says estimate the value of the eigenvector, then iterate.
But my vector cycled as transpose(1,1), transpose(-1, 1), transpose(1, 1) which is a bit of a problem.
Oh for precise instructions.
I'll report back when I find a suitable estimate for the starting value.
But my vector cycled as transpose(1,1), transpose(-1, 1), transpose(1, 1) which is a bit of a problem.
Oh for precise instructions.
I'll report back when I find a suitable estimate for the starting value.
Subscribe to:
Posts (Atom)