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

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++

What is GEEK

Buddha Community

Tubo Man

1548643035

This implementation is not thread-safe, for multiple reasons.

The first half of GetOf() invokes methods of a std::map’s instance. None of std::map’s methods are thread safe, and the second half of GetOf() modifies the std::map.

Since the mutex does not protect the first half of Getof(), where the aforementioned methods get invoked, this is not thread-safe.

Clear() also modifies the std::map, without any synchronization/mutex protection, either.

P.S. The invocation of std::map::clear() in the destructor is not necessary.

Generics type in C# | Generic Class | Generic Method | C# Bangla Tutorial | Advanced C#

https://youtu.be/xGjX0R_6qDg

#oop in c# #object oriented programming in c# #object oriented concept in c# #learn oop concept #advance c# #generics type in c#

Generics type example in C# | Generic Class | Generic Method | C# Tutorial | Advanced C#

https://youtu.be/xfDjyg9jKSk

#oop in c# #object oriented programming in c# #object oriented concept in c# #learn oop concept #advance c# #generics type example in c#

Action generic delegates in C# | Generic delegates in C# | C# Bangla Tutorial |Advanced C#

https://youtu.be/hXkztwCUZz4

#oop in c# #object oriented programming #object oriented concept in c# #learn oop concept #advance c# #action generic delegates in c#

Generic delegates in C# | What is generic delegates | C# Bangla Tutorial | Advanced C#

https://youtu.be/XotHYwRuhOM

#oop in c# #object oriented programming #object oriented concept in c# #learn oop concept #advance c# #generic delegates in c#

Tamale  Moses

Tamale Moses

1624240146

How to Run C/C++ in Sublime Text?

C and C++ are the most powerful programming language in the world. Most of the super fast and complex libraries and algorithms are written in C or C++. Most powerful Kernel programs are also written in C. So, there is no way to skip it.

In programming competitions, most programmers prefer to write code in C or C++. Tourist is considered the worlds top programming contestant of all ages who write code in C++.

During programming competitions, programmers prefer to use a lightweight editor to focus on coding and algorithm designing. VimSublime Text, and Notepad++ are the most common editors for us. Apart from the competition, many software developers and professionals love to use Sublime Text just because of its flexibility.

I have discussed the steps we need to complete in this blog post before running a C/C++ code in Sublime Text. We will take the inputs from an input file and print outputs to an output file without using freopen file related functions in C/C++.

#cpp #c #c-programming #sublimetext #c++ #c/c++