I am trying to make a program that uses shared resources, but all I get in the is std::logic_error. I think I am not using the mutex in the right way. Here is a snippet of the code.
#include <iostream>
#include <vector>
#include <thread>
#include <mutex>
struct camera {
std::string name;
std::string mac;
bool accessStatus;
};
class service {
public:
service(){};
void run();
private:
mutable std::mutex _mutex;
};
void service::run()
{
unsigned char option;
// some dummy camera object
camera camera_object;
camera_object.name = "camera_name";
camera_object.mac = "B6:24:3D:4C:00:9B";
camera_object.accessStatus = true;
// a vector of objects
std::vector<camera> cameras;
cameras.push_back(camera_object);
std::thread TT([&](){
while (true) {
// dummy condition
if (1 == 1) {
std::cout << cameras.size();
}
{
std::unique_lock<std::mutex> mlock(_mutex);
std::cout << "Choose an option:\n"
<< "\t 1. add one more camera \n"
<< "\t 2. get the theme \n"
<< std::flush;
option = getchar();
switch (option) {
case '1':
cameras.push_back(camera_object);
break;
case '2':
std::cout << "Not yet implemented\n" << std::flush;
break;
default:
std::cout << "Invalid input\n" << std::flush;
break;
}
}
// don't waste CPU resources
using namespace std::chrono_literals;
std::this_thread::sleep_for(1s);
system("clear");
}
});
TT.detach();
}
int main() {
service sv;
sv.run();
return 0;
}
Sometimes when I run it it just returns segmentation fault, but other times it let me choose an option, but after I choose it I get std::logic_error. I am trying to understand how mutex and multithreading works, but I have a hard time on this one.
Edit: the shared resource is the cameras vector. I am doing this program just to learn, it does not have a real objective. The condition 1==1 is there just to be sure that is always prints the vector size.