ConcurrentHashMap for C ++

Is there a ConcurrentHashMap for implementing C ++ or something similar anywhere?

I can't understand why multithreading in C ++ is so complicated than Java!

+7
source share
4 answers

Threads arguments are indeed supported in C ++, so there are no thread-protected containers in the standard. People obviously made them before.

I think this thing from Intel can help http://www.threadingbuildingblocks.org/

I have not used it myself yet, so no guarantors.

You can also simply wrap any container in your class using a semaphore to make it thread safe.

Good luck.

+3
source
+6
source

How you do multithreading depends on your OS. For windows, you can use MFC (Visual Studio makes it pretty simple) or use #include "windows.h" and use methods such as CreateMutex and WaitForSingleObject. Here is a quick example:

 #include windows.h class myList { public: myList() { my_mtx_hndl = CreateMutex(NULL, FALSE, "some_cross_proc_name"); } add(<some_type> obj) { WaitForSingleObject(my_mtx_hndl, INFINITE); //Add the obj } private: HANDLE my_mtx_hndl; }; 

I wrote some thread-safe containers. I find that in C ++ it is often so fast to understand the concept of thread safety well and then implement it as needed. Thus, you really understand the full value and benefits of what JAVA does for you.

By the way, the reason this material is more complicated in C ++ is that C ++ is a more powerful language that allows you to do whatever you need, which is useful and dangerous.

I am sure that if you look at CodeProject or in the boost library, you will find some good examples of thread-safe containers.

+1
source

There is a new open source library called junction that contains several new parallel maps.

https://github.com/preshing/junction

Its BSD license, so you can freely use the source code in any project for any purpose.

Read more ... this blogpost.

Thanks to author Jeff .

0
source

All Articles