C++多线程代码范例剖析

开发 后端
在这篇文章中,我们通过在主线程中创建两个线程的方法来为大家详细解读C++多线程的具体应用方法,以方便大家将来的实际应用。

C++编程语言中支持多线程运行。那么如何才能正确的实现这一功能呢?今天我们就在这里先通过一个带来范例来详细解读C++多线程的应用方式,希望初学者们可以根据我们介绍的内容从中学到一些知识。

C++多线程应用示例:

主线程创建2个线程t1和t2,创建时2个线程就被挂起,后来调用ResumeThread恢复2个线程,是其开始执行,调用WaitForSingleObject等待2个线程执行完,然后推出主线程即结束进程。

#include <stdio.h> 
#include <string> // for STL string class  
#include <windows.h> // for HANDLE  
#include <process.h> // 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");  
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.

以上就是对C++多线程的相关介绍。

【编辑推荐】

  1. C++获得系统时间不同方案介绍
  2. C++静态成员函数基本概念讲解
  3. C++静态数据成员定义及应用浅谈
  4. C++指针重载应用代码解读
  5. C++模板函数重载不同之处点评
责任编辑:曹凯 来源: 博客园
相关推荐

2012-05-18 10:36:20

CC++编程

2023-08-01 16:35:48

鸿蒙ArkUI应用开发

2023-08-02 09:29:40

任务池TaskPool

2011-07-21 11:12:58

iPhone 线程 多线程

2010-02-03 10:05:48

C++ enum枚举

2010-02-05 15:30:54

C++多线程测试

2010-01-18 14:09:58

C++多线程

2021-02-25 15:58:46

C++线程编程开发技术

2021-03-05 07:38:52

C++线程编程开发技术

2010-02-01 14:26:50

C++读写文本文件

2010-02-04 13:45:36

C++类模板

2024-06-24 08:10:00

C++互斥锁

2015-04-17 10:35:51

c++c++程序内存泄漏检测代码

2010-01-14 17:42:47

CC++

2010-02-03 10:17:29

C++继承方式

2024-11-05 16:29:57

2023-12-14 15:05:08

volatile代码C++

2010-01-28 16:31:54

C++类型

2024-06-24 12:57:09

多线程C++编程语言

2010-01-12 15:03:33

C++代码
点赞
收藏

51CTO技术栈公众号