Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Performance von Anwendungen
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Saif Eddine Askri
Performance von Anwendungen
Commits
fd934fe8
Commit
fd934fe8
authored
2 months ago
by
Saif Eddine Askri
Browse files
Options
Downloads
Patches
Plain Diff
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
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
prak3/src/server.cpp
+64
-3
64 additions, 3 deletions
prak3/src/server.cpp
with
64 additions
and
3 deletions
prak3/src/server.cpp
+
64
−
3
View file @
fd934fe8
...
...
@@ -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_s
low
(
buffer
);
std
::
string
response
=
processRequest_s
hared_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
)
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment