想在c++中动态的生成多线程,代码如下:
类1:
.........................
ntp_client::ntp_client()
{
}
ntp_client::~ntp_client()
{
}
void ntp_client::run_ntp()
{
while (true)
{
sleep(2);
}
}
类2:动态生成线程类,代码如下:
class ntp_gather
{
private:
public:
ntp_gather();
~ntp_gather();
void ntp_gather_run();
vector<string> m_vec_WhiteList;
list<ntp_client *> m_arr_pNtpClient;
list<thread *> m_arr_pNtpThread;
};
......
void ntp_gather::ntp_gather_run()
{
m_vec_WhiteList.push_back("17.253.116.253");
m_vec_WhiteList.push_back("162.159.200.1");
ntp_client * pNtp_client = NULL;
thread * pThread = NULL;
for (vector<string>::iterator it = m_vec_WhiteList.begin(); it != m_vec_WhiteList.end(); ++it)
{
pNtp_client = new ntp_client();
pNtp_client->m_TimeDeviation = appConfig.m_MaxTimeDeviation;
pNtp_client->m_IP = *it;
thread thread_ntp_gather(&ntp_client::run_ntp, &(*pNtp_client));
pThread->detach();
m_arr_pNtpClient.push_back(pNtp_client);
m_arr_pNtpThread.push_back(pThread);
}
}
以上程序编译能通过,在运行时总会内存错误(Segmentation fault (core dumped))。请问怎样修改,要求保留以上能动态创建线程的效果。