0

I know that there are C++ concurrent hashmap library, such as folly. However, There ain't no such thing as a free lunch. If I only use the native STL containers such as unordered_map.

The example code has three versions:

The first version:

#include "boost/asio/thread_pool.hpp"
#include "boost/thread.hpp"
#include "boost/asio.hpp"
#include <thread>
#include <unordered_map>
#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>

int main() {
    // boost::asio::thread_pool pool(std::thread::hardware_concurrency());
    boost::asio::thread_pool pool(1);
    std::unordered_map<int, int> students;
    srand((unsigned)time(NULL));
    // std::mutex m;
    for(int i = 0; i < 10000; i++) {
        // boost::asio::post(pool, [&students, &m] () {
        //     std::lock_guard<std::mutex> lock(m);
        boost::asio::post(pool, [&students] () {
            students.emplace(std::make_pair(rand(), rand()));
        });        
    }
    pool.join();
    for(auto item : students) {
        std::cout << item.first << " " << item.second << std::endl;
    }
    std::cout << students.size() << std::endl;

    return 0;
}

In the first version, there is only one thread. So there are no any questions.

The second version:

#include "boost/asio/thread_pool.hpp"
#include "boost/thread.hpp"
#include "boost/asio.hpp"
#include <thread>
#include <unordered_map>
#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>

int main() {
    boost::asio::thread_pool pool(std::thread::hardware_concurrency());
    // boost::asio::thread_pool pool(1);
    std::unordered_map<int, int> students;
    srand((unsigned)time(NULL));
    std::mutex m;
    for(int i = 0; i < 10000; i++) {
        boost::asio::post(pool, [&students, &m] () {
            std::lock_guard<std::mutex> lock(m);
        // boost::asio::post(pool, [&students] () {
            students.emplace(std::make_pair(rand(), rand()));
        });        
    }
    pool.join();
    for(auto item : students) {
        std::cout << item.first << " " << item.second << std::endl;
    }
    std::cout << students.size() << std::endl;

    return 0;
}

In the second version, there may be multiple threads that insert data into one unordered_map, the number of threads depends on the machine. Before inserting data into the unordered_map, there is one lock guaranting that only one thread can insert into the unordered_map at a time. There are also no any questions in the second version.

The third version:

#include "boost/asio/thread_pool.hpp"
#include "boost/thread.hpp"
#include "boost/asio.hpp"
#include <thread>
#include <unordered_map>
#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>

int main() {
    boost::asio::thread_pool pool(std::thread::hardware_concurrency());
    // boost::asio::thread_pool pool(1);
    std::unordered_map<int, int> students;
    srand((unsigned)time(NULL));
    // std::mutex m;
    for(int i = 0; i < 10000; i++) {
        // boost::asio::post(pool, [&students, &m] () {
        //     std::lock_guard<std::mutex> lock(m);
        boost::asio::post(pool, [&students] () {
            students.emplace(std::make_pair(rand(), rand()));
        });        
    }
    pool.join();
    for(auto item : students) {
        std::cout << item.first << " " << item.second << std::endl;
    }
    std::cout << students.size() << std::endl;

    return 0;
}

In the third version, multiple threads insert data into one unordered_map. The code can be compiled successfully. However, when running the corresponding executable program, the console prints "Segmentation fault (core dumped)"

Whether it’s safe to modify an unordered_map(STL containers) from multiple threads concurrently in C++11?

If it is unsafe, is there any unexpected unsafe behaviors or expected unsafe behaviors and what is the cause of unsafe behaviors?

In addition to the opensource concurrent hash map library and the first version code and the second version code, are there any other solutions to safely modify an unordered_map(STL containers) from multiple threads concurrently in C++11?

shu
  • 1
  • 1

0 Answers0