资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

boostthread类应用以及资源耗尽异常-创新互联

1测试例子

创新互联专注于武侯企业网站建设,成都响应式网站建设,商城网站开发。武侯网站建设公司,为武侯等地区提供建站服务。全流程按需求定制制作,专业设计,全程项目跟踪,创新互联专业和态度为您提供的服务

#include

#include

#include

void ThreadFunc()

{

std::cout << "Welcome to thread function" << std::endl;

}

int main(int argc, char* argv[])

{

boost::thread instance(&ThreadFunc);

instance.join();

return 0;

}

2 携带参数例子

#include

#include

#include

void threadFunc(const char* pszContext)

{

std::cout << pszContext << std::endl;

}

int main(int argc, char* argv[])

{

char* pszContext = "fengyuzaitu@126.com";

boost::thread thread1(boost::bind(&threadFunc, pszContext));

thread1.join();

return 0;

}

3 类的非静态函数作为线程函数

生产环境中经常需要访问类的私有成员,如果类的静态函数作为线程函数,通过参数的方式传递极其不方便

#include

#include

#include

#include

class CThreadClass

{

public:

CThreadClass()

{

memset(m_szContext, 0x00, 1024);

}

void ThreadFunc()

{

std::cout << m_szContext << std::endl;

}

void StartThread()

{

strcpy_s(m_szContext, "Welcome to thread func\n");

boost::function0 f = boost::bind(&CThreadClass::ThreadFunc, this);

boost::thread thrd(f);

thrd.join();

}

private:

char m_szContext[1024];

};

int main(int argc, char* argv[])

{

CThreadClass instance;

instance.StartThread();

return 0;

}

4 创建线程过多,导致boost库异常抛出,耗尽资源

查看boost::system::system_error = {m_error_code={m_val=11 m_cat=0x02d848f4 {CMMS-test.exe!boost::system::`anonymous-namespace'::generic_error_category generic_category_const} {...} } ...}

boost::throw_exception(const boost::thread_resource_error & e) 行 69 C++

boost::thread::start_thread() 行 180 C++

目前通过代码测试生成1274个线程,实际上这是需要根据线程函数的实质内容决定的,在编码中必须指定上限,否则会引起程序异常崩溃

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


名称栏目:boostthread类应用以及资源耗尽异常-创新互联
URL网址:http://cdkjz.cn/article/jsggs.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220