Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #include #include #include #include #include #include #include #include std::mutex fileCacheMutex; //for the thread safety global mutex is taken in to consideration . //This function

#include

#include

#include

#include

#include

#include

#include

#include

#include

std::mutex fileCacheMutex;

//for the thread safety global mutex is taken in to consideration .

//This function ; reads the content of a file into a string and returns it ...

std::string read_file_to_string(std::string file_name)

{

std::ifstream file(file_name);

std::stringstream buffer;

buffer << file.rdbuf();

std::string result = buffer.str();

return result;

}

//The refactored class is here .

class FileCache {

std::unordered_map

std::future> content_map;

public:

std::string operator[](std::string file_name) { std::unique_lock lock(fileCacheMutex);

auto it = content_map.find(file_name);

if (it != content_map.end())

{

lock.unlock();

try {

return it->second.get();

}catch (const std::exception& e) {

std::cerr << "Exception caught: " << e.what() <<

return "";

{

std::future futureContent =

std::async(std::launch::async, [file_name]()

{

return read_file_to_string(file_name);

});

content_map[file_name] = std::move(futureContent); lock.unlock();

try {

return content_map[file_name].get();

} catch (const std::exception& e) {

std::endl; }}

else

std::endl; }

} };

std::cerr << "Exception 3 caught: " << e.what() <<

return "";

// desc: Generates a file name based on an index

// pre: None

// post: None, aside from description

std::string test_file_name(size_t index){

return std::string("_test_cache_")+std::to_string(index); }

// desc: Generates text in a pseudo-random way, using the input // index as a seed for determining the text size and content // pre: None

// post: None

std::string pseudo_random_text(size_t index){ std::mt19937 rng(index);

std::string result;

size_t size = rng() % 100000;

result.resize(size);

for(size_t i=0; i

// Insert a newline every 64 characters

if((i&0x3F) == 0x3F){

result[i] = ' ';

} else {

// Non-newline characters are random capital letters

result[i] = 'A' + rng() % 26;

}

}

return result;

}

void generate_test_files(size_t count) {

std::vector file_list;

for(size_t i=0; i

std::ofstream file(test_file_name(i));

file << pseudo_random_text(i);

file.close();

} }

void clean_up_test_files(size_t count) {

for(size_t i=0; i

remove(test_file_name(i).c_str());

}

}

void test_cache(size_t file_count, FileCache &cache) { size_t seed = rand();

std::mt19937 rng(seed);

std::vector canon;

for(size_t i=0; i

}

// Perform onr hundred thousand cache lookups

for(int i=0; i<100000; i++) {

size_t index = rand() % file_count; std::string file_name = test_file_name(index); std::string const& content = cache[file_name]; if(content != canon[index]){

std::cout << "Mismatch detected when loading file '" << file_name << "' ";

} }

}

int main() {

size_t thread_count = 32;

size_t file_count = 100;

FileCache cache;

// Make dummy files to test the FileCache

generate_test_files(file_count);

std::thread team[thread_count];

// Have many threads query the cache in parallel

for(size_t i=0; i

team[i] = std::thread(test_cache,file_count,std::ref(cache));

}

for(size_t i=0; i

team[i].join();

}

// Remove dummy files, so they don't clutter your directory

clean_up_test_files(file_count);

return 0; }.

Help me fix the issue ,

./we terminate called after throwing an instance of 'std::future_error' terminate called recursively

what(): std::future_error: No associated state Aborted (core dumped) . help me fix this and the expecected output is:

Mismatch detected when loading file '_test_cache_21' Mismatch detected when loading file '_test_cache_56'

$ time ./we

real 0m4.594s user 0m26.831s sys 1m43.295s

..Instruc5ons :

class called FileCache. A file cache stores the content of files the first time they are loaded, returning the stored content on any subsequent load attempts. This allows sub- tasks to use the content of files efficiently without coordinating the responsibility of reading in said content. Whichever thread first attempts to read a file through a cache will automatically perform the read.

class FileCache {

std::unordered_map< std::string,

std::string > content_map;

public:

std::string const& operator[](std::string file_name) { std::string content = read_file_to_string(file_name); content_map[file_name] = content; std::string const& result = content_map[file_name]; return result;

} };

modified this class and wrote something in my above code . FileCache is a very bad file cache, since it re-loads files that have already been loaded, and it does not store the results of those loads in a thread-safe way.objective is to refactor the FileCache class to be thread safe and to not reload files that are were already loaded or which are currently being loaded by another thread. this is expected to use

std::async as well as futures. The one method provided for the FileCache class, the indexing operator, is used to query the

content of files, with the file name provided as an index, and with the file content returned as a reference to a const string.

My code is refactored one but has the problem . please help me fix it .

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions

Question

Prove that (R 2 , U) is second countable.

Answered: 1 week ago