I need to lock private class member for other threads: if i do not unlock variable, other threads can not use that variable, and wait before it unlocks. (something like mutex).
For example:
using namespace std;
class a
{
public:
a() {
//here i need to lock my variable
thread th([this] {
Sleep(10000);
variable = 100;});
//here i need to unlock my variable});
}
int getVariable() { return this->variable; }
private:
int variable;
};
int main() {
a var;
thread th([&var] {cout << var.getVariable() << endl; });
//getVariable waits before variable is unlocked
}