-
Notifications
You must be signed in to change notification settings - Fork 1
Thread
victor edited this page Aug 12, 2025
·
3 revisions
Smart_Store uses multithreading to ensure smooth and efficient handling of concurrent operations, such as importing, exporting, and accessing items. Internal mutexes are implemented to maintain thread safety, preventing race conditions when multiple threads interact with shared resources like the ItemManager. This allows Smart_Store to:
- Handle simultaneous read/write operations without data corruption.
- Improve performance during bulk imports or exports.
- Safely manage shared objects across threads. Threading ensures that Smart_Store remains responsive and reliable, even under heavy load or in multi-user environments.
#include "t_manager/ItemManager.h"
#include "err_log/Logger.hpp"
#include <iostream>
#include <string>
#include <vector>
#include <thread>
struct Item {
std::string name;
};
void simulateUser(ItemManager& manager, int userId) {
auto itemName = "Item_" + std::to_string(userId);
auto item = std::make_shared<Item>(Item{itemName});
manager.addItem(item, "user_tag_" + std::to_string(userId)); // tag is required
manager.hasItem("user_tag_" + std::to_string(userId));
}
int main() {
ItemManager manager;
// Here is the simple thread simulation.
std::vector<std::thread> threads;
/*
Simulate 3 users adding items concurrently
Each user adds an item with a unique tag
*/
for (int i = 0; i < 3; ++i) {
threads.emplace_back(simulateUser, std::ref(manager), i);
}
// Wait for all threads to finish
for (auto& t : threads) {
t.join();
}
return 0;
}// Include Smart_Store's item management system
#include "t_manager/ItemManager.h"
// Include Smart_Store's logging system
#include "err_log/Logger.hpp"
// Standard library headers
#include <iostream>
#include <vector>
#include <thread>
#include <string>
// Include JSON library for serialization
#include <nlohmann/json.hpp> // For JSON serialization
// Define a User class to represent employees in the system
class User {
public:
std::string name;
int id;
std::string email;
std::string role;
int age;
// Constructor to initialize a User with specific attributes
User(const std::string& name, int id, const std::string& email, const std::string& role, int age)
: name(name), id(id), email(email), role(role), age(age) {}
// Default constructor for deserialization and fallback
User() : name(""), id(0), email(""), role(""), age(0) {}
// Convert User object to JSON format for export or logging
nlohmann::json toJson() const {
return {
{"name", name},
{"id", id},
{"email", email},
{"role", role},
{"age", age}
};
}
// Create a User object from JSON data (used during import)
static User fromJson(const nlohmann::json& j) {
return User(
j.at("name").get<std::string>(),
j.at("id").get<int>(),
j.at("email").get<std::string>(),
j.at("role").get<std::string>(),
j.at("age").get<int>()
);
}
};
// Function to add a User to the Smart_Store system with a unique tag
void addEmployee(ItemManager& manager, int id) {
auto user = std::make_shared<User>(
"User_" + std::to_string(id), // Name
id, // ID
"user" + std::to_string(id) + "@store.com", // Email
"Staff", // Role
20 + (id % 10) // Age (varied for demo)
);
// Register the user in the system with a tag like "emp_0", "emp_1", etc.
manager.addItem(user, "emp_" + std::to_string(id));
}
int main() {
ItemManager manager; // Create an instance of Smart_Store's item manager
std::vector<std::thread> threads;
// Spawn 3 threads to simulate concurrent employee registration
for (int i = 0; i < 3; ++i) {
threads.emplace_back(addEmployee, std::ref(manager), i);
}
// Wait for all threads to finish execution
for (auto& t : threads) {
t.join();
}
std::cout << "All employees added.\n";
// Retrieve and display the employee with tag "emp_1"
auto importedUser = manager.getItem<User>("emp_1");
LOG_CONTEXT(LogLevel::DISPLAY, "\n" + importedUser->toJson().dump(4), {});
// Modify the email of the employee with tag "emp_1"
manager.modifyItem<User>("emp_1", [](User& user) {
user.email = "new_email@example.com";
});
// Retrieve and display the updated employee data
importedUser = manager.getItem<User>("emp_1");
LOG_CONTEXT(LogLevel::DISPLAY, "\n" + importedUser->toJson().dump(4), {});
return 0;
}📧 Email: ve48381@gmail.com
- Home
- Integration
- API Guide
- EX/IM-Port Files
- API Thread Access
- Schema & Versioning
- Error Logging Guide
- Global Storage Guide
If you encounter any issues and bugs, please reach out via email or GitHub issues.