upd: finish writing ReadWriteLock
This commit is contained in:
@ -1,8 +1,10 @@
|
|||||||
#ifndef PROTECTOR_UTILITY_H
|
#ifndef PROTECTOR_UTILITY_H
|
||||||
#define PROTECTOR_UTILITY_H
|
#define PROTECTOR_UTILITY_H
|
||||||
|
#include <atomic>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <condition_variable>
|
#include <condition_variable>
|
||||||
#include <exception>
|
#include <exception>
|
||||||
|
#include <iostream>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <stack>
|
#include <stack>
|
||||||
@ -69,8 +71,8 @@ class ReadWriteLock {
|
|||||||
private:
|
private:
|
||||||
std::mutex mtx;
|
std::mutex mtx;
|
||||||
std::condition_variable cv;
|
std::condition_variable cv;
|
||||||
int readers;
|
std::atomic<int> readers;
|
||||||
bool is_writing;
|
std::atomic<bool> is_writing;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ReadWriteLock();
|
ReadWriteLock();
|
||||||
@ -84,4 +86,15 @@ class SessionClass {
|
|||||||
std::string SessionToken;
|
std::string SessionToken;
|
||||||
std::string OuthorizationKey;
|
std::string OuthorizationKey;
|
||||||
};
|
};
|
||||||
|
namespace BookStore_ZYM {
|
||||||
|
extern std::mutex debug_Print_Mutex;
|
||||||
|
}
|
||||||
|
void debugPrint();
|
||||||
|
template <typename... Args>
|
||||||
|
void debugPrint(Args... args) {
|
||||||
|
BookStore_ZYM::debug_Print_Mutex.lock();
|
||||||
|
((std::cerr << args), ...);
|
||||||
|
std::cerr << std::endl;
|
||||||
|
BookStore_ZYM::debug_Print_Mutex.unlock();
|
||||||
|
}
|
||||||
#endif // PROTECTOR_UTILITY_H
|
#endif // PROTECTOR_UTILITY_H
|
@ -1,6 +1,7 @@
|
|||||||
#ifndef PROTECTOR_SCHEDULE_H
|
#ifndef PROTECTOR_SCHEDULE_H
|
||||||
#define PROTECTOR_SCHEDULE_H
|
#define PROTECTOR_SCHEDULE_H
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
#include "bs-utility.h"
|
#include "bs-utility.h"
|
||||||
#include "engine.h"
|
#include "engine.h"
|
||||||
@ -9,6 +10,7 @@ class BookStoreBackEndClass {
|
|||||||
BlockingStringStream *input_ptr;
|
BlockingStringStream *input_ptr;
|
||||||
BlockingStringStream *output_ptr;
|
BlockingStringStream *output_ptr;
|
||||||
BookStoreEngineClass *engine_ptr;
|
BookStoreEngineClass *engine_ptr;
|
||||||
|
std::unordered_map<std::string, SessionClass> session_map;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
BookStoreBackEndClass() = delete;
|
BookStoreBackEndClass() = delete;
|
||||||
|
@ -24,7 +24,7 @@ void ReadWriteLock::endRead() {
|
|||||||
std::unique_lock<std::mutex> lock(mtx);
|
std::unique_lock<std::mutex> lock(mtx);
|
||||||
readers--;
|
readers--;
|
||||||
if (readers == 0) {
|
if (readers == 0) {
|
||||||
cv.notify_one(); // 唤醒一个等待的写操作
|
cv.notify_all(); // 唤醒一个等待的写操作
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,3 +41,11 @@ void ReadWriteLock::endWrite() {
|
|||||||
is_writing = false;
|
is_writing = false;
|
||||||
cv.notify_all(); // 唤醒所有等待的读操作和写操作
|
cv.notify_all(); // 唤醒所有等待的读操作和写操作
|
||||||
}
|
}
|
||||||
|
namespace BookStore_ZYM {
|
||||||
|
std::mutex debug_Print_Mutex;
|
||||||
|
}
|
||||||
|
void debugPrint() {
|
||||||
|
BookStore_ZYM::debug_Print_Mutex.lock();
|
||||||
|
std::cerr << std::endl;
|
||||||
|
BookStore_ZYM::debug_Print_Mutex.unlock();
|
||||||
|
}
|
@ -4,30 +4,57 @@
|
|||||||
#include "bs-utility.h"
|
#include "bs-utility.h"
|
||||||
#include "builtin-cli.h"
|
#include "builtin-cli.h"
|
||||||
#include "clipp/clipp.h"
|
#include "clipp/clipp.h"
|
||||||
void test() {
|
ReadWriteLock rwLock;
|
||||||
BlockingStringStream bss;
|
std::mutex mtx;
|
||||||
|
void readerFunc(int id) {
|
||||||
|
while (true) {
|
||||||
|
rwLock.startRead();
|
||||||
|
debugPrint("Reader ", id, " is reading.");
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(498));
|
||||||
|
debugPrint("Reader ", id, " end reading.");
|
||||||
|
rwLock.endRead();
|
||||||
|
|
||||||
std::thread reader([&bss]() {
|
// 模拟一些其他操作
|
||||||
std::string data;
|
std::this_thread::sleep_for(std::chrono::milliseconds(502));
|
||||||
for (int i = 1; i <= 5; ++i) {
|
|
||||||
// Use getline with string delimiter
|
|
||||||
bss.getline(data, '\n');
|
|
||||||
std::cerr << "Received: " << data << std::endl;
|
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
|
||||||
std::thread writer([&bss]() {
|
void writerFunc(int id) {
|
||||||
for (int i = 1; i <= 5; ++i) {
|
while (true) {
|
||||||
bss << "Data " << i << '\n';
|
rwLock.startWrite();
|
||||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
debugPrint("Writer ", id, " is writing.");
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(1005));
|
||||||
|
debugPrint("Writer ", id, " end writing.");
|
||||||
|
rwLock.endWrite();
|
||||||
|
|
||||||
|
// 模拟一些其他操作
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(1013));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void test() {
|
||||||
|
std::thread readers[3];
|
||||||
|
std::thread writers[2];
|
||||||
|
|
||||||
|
for (int i = 0; i < 3; ++i) {
|
||||||
|
readers[i] = std::thread(readerFunc, i + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < 2; ++i) {
|
||||||
|
writers[i] = std::thread(writerFunc, i + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 等待所有线程结束
|
||||||
|
for (int i = 0; i < 3; ++i) {
|
||||||
|
readers[i].join();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < 2; ++i) {
|
||||||
|
writers[i].join();
|
||||||
}
|
}
|
||||||
});
|
|
||||||
reader.join();
|
|
||||||
writer.join();
|
|
||||||
}
|
}
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
// test();
|
test();
|
||||||
// return 0;
|
return 0;
|
||||||
bool is_server = false;
|
bool is_server = false;
|
||||||
std::string config_dir = "";
|
std::string config_dir = "";
|
||||||
bool custom_config_dir = false;
|
bool custom_config_dir = false;
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
~~未完待续,别扣我分~~
|
~~未完待续,别扣我分~~
|
||||||
|
尚未定稿的内容见《总体设计文档》
|
||||||
**项目名称**:BookStore
|
**项目名称**:BookStore
|
||||||
|
|
||||||
**作者**:Zhuang Yumin
|
**作者**:Zhuang Yumin
|
||||||
|
Reference in New Issue
Block a user