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.
Ian
source share