I have a class with 2 methods say, Connect and Disconnect. Now when i call class B i get an address for A using that i'll connect and once it is done i have to call class C to perform disconnect operation on that same address.
I can achieve this if I make A *a as a global variable. But when it comes to multithreading and A being global uses only one address/instance and it disconnects before other threads were running causing access violation exception.
How to resolve this? Thanks in advance.
class A()
{
Connect();
Disconnect();
};
A *a = nullptr;
class B()
{
a = new A();
a->Connect();
}
class C()
{
if(!a)
a->Disconnect();
}