【发布时间】:2014-10-11 09:45:55
【问题描述】:
未调用线程函数“get_singleton”函数。我什至没有在我的屏幕上收到任何错误。
class singleton{
private: singleton(){cout<<"constructor called";}
singleton(singleton&);
singleton& operator =(singleton &);
~singleton();
public: static singleton *s;
static singleton* get_singleton();
static pthread_mutex_t t;
};
pthread_mutex_t singleton::t=PTHREAD_MUTEX_INITIALIZER;
singleton* singleton::s=NULL;
singleton* singleton::get_singleton()
{
cout<<"get_singleton called";
if(s==NULL)
{
usleep(300);
s=new singleton();
}
return s;
}
int main(int argc, char *argv[])
{
int err;
pthread_t t1,t2;
err=pthread_create(&t1,NULL,(void *(*)(void *))singleton::get_singleton,NULL);
if(err!=0)
cout<<"unable to create thread";
err=pthread_create(&t2,NULL,(void *(*)(void *))singleton::get_singleton,NULL);
if(err!=0)
cout<<"unable to create thread";
cout<<"end of func"<<endl;
return 0;
}
调用“get_singleton”函数时,“pthread_create”api 是否有任何错误。
提前致谢。
【问题讨论】:
标签: c++ multithreading pthreads