I am trying to run print() and print(char ch) methods on different threads with singleton instance .
Can any body help me out why I am getting errors:-
error C3867: 'Singleton::print': function call missing argument list; use '&Singleton::print' to create a pointer to member
error C3867: 'Singleton::print': function call missing argument list; use '&Singleton::print' to create a pointer to member
error C2661: 'std::thread::thread' : no overloaded function takes 2 arguments
Also help me in correcting out the following code for me.
class Singleton
{
private:
Singleton()
{}
static Singleton* singletonInstance;
public:
static Singleton* getSingletonInstance();
void print()
{
std::this_thread::sleep_for(std::chrono::milliseconds(500));
for (int i = 0; i < 100000; i++)
{
cout<<i<<endl;
}
}
void print(char ch)
{
std::this_thread::sleep_for(std::chrono::milliseconds(500));
for (int i = 0; i < 100000; i++)
{
cout<<ch<<" "<<i<<endl;
}
}
};
Singleton* Singleton::singletonInstance = nullptr;
Singleton* Singleton::getSingletonInstance()
{
if(!singletonInstance)
singletonInstance=new Singleton();
return singletonInstance;
}
int main()
{
std::thread t1(Singleton::getSingletonInstance()->print);
std::thread t2(Singleton::getSingletonInstance()->print,'T');
t1.join();
t2.join();
return 0;
}
object.printand equivalentlyp->printaren't valid C++. This has nothing to do with threads, you're just not passing a proper callable object. As the error says, you can create a pointer-to-member, though it won't have an object bound to it, you'd have to pass it separately. There's also the option of a lambda, which is probably easier given the overloading. - chris