Skip to content
Snippets Groups Projects
Commit fd934fe8 authored by Saif Eddine Askri's avatar Saif Eddine Askri
Browse files

Implement shared mutex for concurrent read/write operations in request processing

parent 20d5e1b7
No related branches found
No related tags found
No related merge requests found
......@@ -7,6 +7,7 @@
#include <mutex>
#include <map>
#include <atomic>
#include <shared_mutex>
#define PORT 2525
#define BUFFER_SIZE 1024
......@@ -15,6 +16,7 @@
#define DELETE_OPERATION 'd'
std::mutex mtx;
std::shared_mutex shared_mtx;
std::map<std::string, std::string> DB;
std::atomic<int> request_count(0);
......@@ -111,9 +113,9 @@ std::string processRequest_slow(const std::string& request) {
free(parsedRequest);
return response;
}
//=============================================
// we assume that this function is fast
// gelichzeitig lesen oder schreiben ist erlaubt
std::string processRequest_fast(const std::string& request) {
std::string response;
......@@ -164,6 +166,64 @@ std::string processRequest_fast(const std::string& request) {
return response;
}
//=============================================
// mehrer Thread Lesen gleichzeitig aber ein Thread schreibt.
std::string processRequest_shared_mtx(const std::string& request) {
std::string response;
size_t first = request.find(",");
size_t second = request.find(",", first + 1);
size_t end = request.find(";");
if (first == std::string::npos || end == std::string::npos) {
return "Bad request!";
}
char operation = request[0];
std::string key;
std::string value;
if (second != std::string::npos) {
key = request.substr(first + 1, second - first - 1);
value = request.substr(second + 1, end - second - 1);
} else {
key = request.substr(first + 1, end - first - 1);
}
switch (operation) {
case STORE_OPERATION: {
std::unique_lock<std::shared_mutex> lock(shared_mtx);
DB[key] = value;
response = "Stored [" + key + "] = " + value;
break;
}
case GET_OPERATION: {
std::shared_lock<std::shared_mutex> lock(shared_mtx);
if (DB.find(key) != DB.end()) {
response = "Value of [" + key + "] is " + DB[key];
} else {
response = "Key [" + key + "] not found";
}
break;
}
case DELETE_OPERATION: {
std::unique_lock<std::shared_mutex> lock(shared_mtx);
if (DB.erase(key)) {
response = "Deleted key [" + key + "]";
} else {
response = "Key [" + key + "] not found for deletion";
}
break;
}
default:
response = "Invalid operation!";
break;
}
return response;
}
void handle_client(int client_socket) {
char buffer[BUFFER_SIZE] = {0};
......@@ -175,7 +235,7 @@ void handle_client(int client_socket) {
}
buffer[bytes_received] = '\0';
std::string response = processRequest_slow(buffer);
std::string response = processRequest_shared_mtx(buffer);
// Anfragezähler inkrementieren
request_count.fetch_add(1, std::memory_order_relaxed);
......@@ -217,7 +277,8 @@ int main() {
std::thread monitor_thread(throughput_monitor);
monitor_thread.detach();
while (true) {
while (true)
{
client_socket = accept(server_fd, (struct sockaddr*)&address, &addrlen);
if (client_socket < 0) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment