std::string ccy (ptr->ccy, ptr->ccy+3); //using a special string() ctor
my ptr->ccy is the address of a 3-char array, but it’s immediately followed by other chars belonging to another field, in a tightly packed struct without padding. If you simply pass ptr->ccy to string() ctor, your string will take in many extra chars until a null terminator.
Could be a tiny halo in a coding IV. Useful technique in some situations, such as virtual output operator.
Benefit: the virtual method can reuse code more easily
Within a function f, a static local int is shared mutable across threads and unsafe!
Solution — declare the variable with thread local storage, so each thread get a distinct static local object, not shared.
Statistically, 20% of my java coding questions involve threading, my halo and other candidates’ weakness. 0% in c++ except SCB.
Remember the 最强大脑 competition? I’m a specialist in mental arithmetics, so the more mental arithmetic games, the better my overall score.
map::count() is simpler and not slower 🙂 than map::find()
Even for a multimap, count() is slower but good enough in a quick coding test. Just add a comment to say “will upgrade to find()”
In terms of cost, count() is only slightly slower than find(), as the matching entries live together in the RB tree.
"map the 'v' keystroke to mean 'save file unconditionally'
map v :w!
In java, mutex is always in heap. Primitive objects can’t be host to a mutex.
In pthreads, mutex object can be allocated anywhere, but I have seen it allocated only in heap or in global area.
In C, you can also allocate mutex in shared memory, creating a cross-process mutex! Not a common practice in java.
Q: what if I allocate a mutex in stack?
A: in java,the actual object is still on heap,though the variable is on stack and invisible to other threads
A: in C, the entire mutex object can be on stack, but such a mutex is useless. Imagine a lock on a door with a single key to be shared, but other people have their own doors, so there’s no synchronization no access control at all:(
This is available in pthreads (pthread_mutex_trylock()).
I believe java and boost::thread are all based on that.
https://www.howtogeek.com/howto/16226/complete-guide-to-symbolic-links-symlinks-on-windows-or-linux/ is a 2017 article.
mklink command can create both hard links (known as “hard links” in Windows) and soft links (known as “symbolic links” in Windows).
On Windows XP, I have used “Junction.exe” for years, because mklink is not available.