#include <iostream>
#include <string>
#include <mutex>
#include <thread>
#include <chrono>
#include <unordered_map>

std::unordered_map<std::string, std::string> map1 ( {{"apple","red"},{"lemon","yellow"}} );
std::mutex mtx1;

std::unordered_map<std::string, std::string> map2 ( {{"orange","orange"},{"strawberry","red"}} );
std::mutex mtx2;

void func() {
	std::lock_guard<std::mutex> lock1(mtx1);
	std::lock_guard<std::mutex> lock2(mtx2);
	
	std::cout << "map1: ";
	for (auto& x: map1) std::cout << " " << x.first << " => " << x.second << ", ";
	std::cout << std::endl << "map2: ";
	for (auto& x: map2) std::cout << " " << x.first << " => " << x.second << ", ";
	std::cout << std::endl << std::endl;
	
	auto it1 = map1.find("apple");
	if(it1 != map1.end()) {
		auto val = *it1;
		map1.erase(it1);
		std::this_thread::sleep_for(std::chrono::duration<double, std::milli>(1000));
		map2[val.first] = val.second;
	}
}

int main ()
{
	std::thread t1(func);
	std::this_thread::sleep_for(std::chrono::duration<double, std::milli>(500));
	std::thread t2(func);
	t1.join();
	t2.join();

	return 0;
}