这个问题看起来很简单,却还是花了我很多时间来解决。开始时候不能在.cc文件中实现.h文件中的私有方法,后来不知道怎么又不报错了,很困惑。开始老想在调用构造函数的时候判断threadnum<THREAD_MAX,再进行thread的创建,后来发现这个方法根本不好用,在构造函数里做判断,本来就不合理。
最后在小新的指导下,在thread中创建一个静态方法 getInstance,然后在其中判断师傅符合条件,如果符合则返回一个new thread,否则返回NULL。然后将构造方法声明为private,禁止外部调用,只能通过getInstance获得thread的实例。
1.在system.h和system.cc中声明和初始化threadnum ,用来记录当前线程数。
system.h:
extern int threadnum;
system.cc
threadnum = 0;
2.修改thread.h文件,将构造函数声明为私有,并声明一个新的静态方法getInstance.
private:
Thread(char* debugName);
public:
static Thread* getInstance(char* debugName);
3.在thread.cc中实现getInstance方法,修改构造函数和析构函数
在构造函数中添加一行 ++threadnum;
在析构函数中添加一行 --threadnum;
//Thread::getInstance
//created to control the num of threads
Thread*
最后在小新的指导下,在thread中创建一个静态方法 getInstance,然后在其中判断师傅符合条件,如果符合则返回一个new thread,否则返回NULL。然后将构造方法声明为private,禁止外部调用,只能通过getInstance获得thread的实例。
1.在system.h和system.cc中声明和初始化threadnum ,用来记录当前线程数。
system.h:
extern int threadnum;
system.cc
threadnum = 0;
2.修改thread.h文件,将构造函数声明为私有,并声明一个新的静态方法getInstance.
private:
Thread(char* debugName);
public:
static Thread* getInstance(char* debugName);
3.在thread.cc中实现getInstance方法,修改构造函数和析构函数
在构造函数中添加一行 ++threadnum;
在析构函数中添加一行 --threadnum;
//Thread::getInstance
//created to control the num of threads
Thread*