##common c++concurrency libraries

Java has language-level support for concurrency + some special concurrency classes in the JDK library.

C/C++ use a library-only approach. C libraries include pthreads and winthreads. I think these are provided by the OS, because these threads are kernel threads.

Many threaded c++ projects use nothing but these C libraries.

There are also c++ thread libraries, (probably always) based on the underlying C libraries.
* c++11 thread library
* boost
* Microsoft Foundation Classes
* RogueWave

While the lucky java developer can afford to focus on a single concurrency tool set, the unlucky c++ developer often have to juggle 2 or more.

Therefore, it’s harder to get the “halo” in c++ concurrency interviews


See other posts about volatile, in both c++ and java.

[[c++succinctly]] also firmly denounced volatile keyword’s usage for concurrency.

[[effectiveModernC++]] echos in Item 40: Use std::atomic for concurrency, volatile for special memory.

For a “volatile” variable, c++ (unlike java) compiler is not required to introduce memory fence i.e. updates by one thread is not guaranteed to be visible globally. See Eric Lippert on http://stackoverflow.com/questions/26307071/does-the-c-volatile-keyword-introduce-a-memory-fence

c++ threading support – historically

[[c++concurrencyInAction]] has a concise historical review…

* POSIX is a platform-specific C api, just like the Windows API. 
I feel this is an OS-level API. Every OS-level API tends to be in C (not c++), so various languages can all use it. OS-level API tend to “present” the hardware as is. The hardware has no concepts of object orientation or exceptions. The C language is the natural language for the hardware.
* Threading is a low-level, OS-level feature. (Kernel support? Not sure about the jargon). Naturally, threading is implemented in C and exposed first as C api. Many c++ threading developers simply use C api without any motivation to use c++.
* There's no surprise that the early c++ concurrency libraries are wrappers over the existing C api. Examples include MFC and Boost.
* Note low-level C api is usually platform-dependent. The c++ wrapper api has an opportunity to smooth out the platform differences.

sample code showing boost scoped_lock#not c++14

You need to view the post to see the attached source code!

<![CDATA[ #include #include #include #include #include using namespace std; boost::posix_time::milliseconds sleepTime(1); templateclass MyQueue { public: void enqueue(const T& x) { cout < enqueuing … ” << x <mutex_); cout <> just got lock … ” << x <mutex_); if (list_.empty()) { throw 0; // unlock } T tmp = list_.front(); list_.pop_front(); cout << "< dequeu " << tmp << "\n"; return (tmp); } private: std::list list_; boost::mutex mutex_; }; MyQueue queueOfStrings; int reps = 5; void sendSomething() { for (int i = 0; i < reps; ++i) { stringstream st; st << i; queueOfStrings.enqueue("item_" + st.str()); boost::this_thread::sleep(sleepTime); } } void recvSomething() { for (int i = 0; i < reps*3; ++i) { try { queueOfStrings.dequeue(); } catch (int ex) { cout << "<- – ( ) after releasing lock, catching " << ex <