C++编程语言中支持多线程运行。那么如何才能正确的实现这一功能呢?今天我们就在这里先通过一个带来范例来详细解读C++多线程的应用方式,希望初学者们可以根据我们介绍的内容从中学到一些知识。
创新互联是网站建设技术企业,为成都企业提供专业的网站建设、成都做网站,网站设计,网站制作,网站改版等技术服务。拥有10多年丰富建站经验和众多成功案例,为您定制适合企业的网站。10多年品质,值得信赖!
C++多线程应用示例:
主线程创建2个线程t1和t2,创建时2个线程就被挂起,后来调用ResumeThread恢复2个线程,是其开始执行,调用WaitForSingleObject等待2个线程执行完,然后推出主线程即结束进程。
- #include
- #include
// for STL string class - #include
// for HANDLE - #include
// for _beginthread() - using namespace std;
- class ThreadX
- {
- private:
- int loopStart;
- int loopEnd;
- int dispFrequency;
- public:
- string threadName;
- ThreadX( int startValue, int endValue, int frequency )
- {
- loopStart = startValue;
- loopEnd = endValue;
- dispFrequency = frequency;
- }
- static unsigned __stdcall ThreadStaticEntryPoint(void * pThis)
- {
- ThreadX * pthX = (ThreadX*)pThis; // the tricky cast
- pthX->ThreadEntryPoint(); // now call the true entry-point-function
- return 1; // the thread exit code
- }
- void ThreadEntryPoint()
- {
- for (int i = loopStart; i <= loopEnd; ++i)
- {
- if (i % dispFrequency == 0)
- {
- printf( "%s: i = %d\n", threadName.c_str(), i );
- }
- }
- printf( "%s thread terminating\n", threadName.c_str() );
- }
- };
- int main()
- {
- ThreadX * o1 = new ThreadX( 0, 1, 2000 );
- HANDLE hth1;
- unsigned uiThread1ID;
- hth1 = (HANDLE)_beginthreadex( NULL, // security
- 0, // stack size
- ThreadX::ThreadStaticEntryPoint,
- o1, // arg list
- CREATE_SUSPENDED, // so we can later call ResumeThread()
- &uiThread1ID );
- if ( hth1 == 0 )
- printf("Failed to create thread 1\n");
- DWORD dwExitCode;
- GetExitCodeThread( hth1, &dwExitCode ); // should be STILL_ACTIVE = 0x00000103 = 259
- printf( "initial thread 1 exit code = %u\n", dwExitCode );
- o1->threadName = "t1";
- ThreadX * o2 = new ThreadX( -1000000, 0, 2000 );
- HANDLE hth2;
- unsigned uiThread2ID;
- hth2 = (HANDLE)_beginthreadex( NULL, // security
- 0, // stack size
- ThreadX::ThreadStaticEntryPoint,
- o2, // arg list
- CREATE_SUSPENDED, // so we can later call ResumeThread()
- &uiThread2ID );
- if ( hth2 == 0 )
- printf("Failed to create thread 2\n");
- GetExitCodeThread( hth2, &dwExitCode ); // should be STILL_ACTIVE = 0x00000103 = 259
- printf( "initial thread 2 exit code = %u\n", dwExitCode );
- o2->threadName = "t2";
- ResumeThread( hth1 ); // serves the purpose of Jaeschke's t1->Start()
- ResumeThread( hth2 );
- WaitForSingleObject( hth1, INFINITE );
- WaitForSingleObject( hth2, INFINITE );
- GetExitCodeThread( hth1, &dwExitCode );
- printf( "thread 1 exited with code %u\n", dwExitCode );
- GetExitCodeThread( hth2, &dwExitCode );
- printf( "thread 2 exited with code %u\n", dwExitCode );
- CloseHandle( hth1 );
- CloseHandle( hth2 );
- delete o1;
- o1 = NULL;
- delete o2;
- o2 = NULL;
- printf("Primary thread terminating.\n");
- }
以上就是对C++多线程的相关介绍。
【编辑推荐】
当前题目:C++多线程代码范例剖析
分享路径:http://www.shufengxianlan.com/qtweb/news7/263157.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联