Is this generic implementation of cache thread safe (c++)?

Is this generic implementation of cache thread safe (c++)?

I wrote the following implementation of a generic cache, where&nbsp;func&nbsp;will provide the values (compute, read file, etc).&nbsp;<strong>Is it thread safe ?</strong>

I wrote the following implementation of a generic cache, where func will provide the values (compute, read file, etc). Is it thread safe ?

#pragma once
#include "stdafx.h"
#include <map>
#include <functional>
#include <mutex>

using namespace std;

template<class T1, class T2> class __declspec(dllexport) CacheOf { map<T1, T2> _cache; function<T2(T1)> _func; mutex CacheMtx; public: CacheOf(function<T2(T1)> func); ~CacheOf(); T2 Get(T1); void Clear(); };

template <class T1, class T2> CacheOf<T1, T2>::CacheOf(std::function<T2(T1)> func) { _func = func; }

template <class T1, class T2> CacheOf<T1, T2>::~CacheOf() { _cache.clear(); }

template <class T1, class T2> auto CacheOf<T1, T2>::Get(T1 key) -> T2 {

auto it = _cache.find(key);
T2 value;
if (it != _cache.end())
{
    value = it-&gt;second;
    return value;
}
value = _func(key);
{
    unique_lock&lt;mutex&gt; cachelock(CacheMtx);
    _cache.insert(pair&lt;T1, T2&gt;(key, value));
}
return value;

}

template <class T1, class T2> auto CacheOf<T1, T2>::Clear() -> void { _cache.clear(); }


c++

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Loops in C++ | For, While, and Do While Loops in C++

In this Video We are going to see how to use Loops in C++. We will see How to use For, While, and Do While Loops in C++.

Using isdigit() in C/C++

In this article, we'll take a look at using the isdigit() function in C/C++. This is a very simple way to check if any value is a digit or not. Let's look

C++ Tutorial for Beginners 19 - C++ Structures | Struct declaration | C++ Data Structures

Welcome to this course on C++ Tutorial for Beginners. In this video we will see How to use C++ Structures.

Object Oriented Programming in C++ | C++ OOPs Concepts | Learn Object Oriented C++

C++ is general purpose, compiled, object-oriented programming language and its concepts served as the basis for several other languages such as Java, Python, Ruby, Perl etc.

A Complete Guide to fread() in C/C++

In this article, we’ll take a look at using fread() in C/C++.