MFC一次性开多个线程的简单示例

cpp 复制代码
#include <afxwin.h>

CWinApp TheApp;

UINT myThreadFunc(LPVOID);

int main()
{
	for (int i = 0; i<10; i++)
	{
		if (AfxBeginThread(myThreadFunc, (LPVOID)i))
			printf("Thread launched %d\n", i);
	}

	// Wait for the threads to complete.
	Sleep(2000);

	return 0;
}

UINT myThreadFunc(LPVOID n)
{
	for (int i = 0; i<4; i++)
		printf("%d%d%d%d%d%d\n", n, n, n, n, n, n);
	return 0;
}

为了使用mfc,先包含afxwin.h;

afxwin.h是MFC C++类库的必需文件,其中包含如CWin,CStatic,CButton,CString,CEdit等类运行所必需的头文件;它还会调用windows.h,该头文件包含有数据类型的定义、API入口点定义和其它有用的参数信息;

Afx前缀是微软MFC一个小组的名称简写,并没有别的意义;

循环调用AfxBeginThread创建10个线程,线程都执行同一个函数,每次传入的参数不一样;

运行如下;

相关推荐
·present·5 小时前
CSAPP(lecture 4_01)
c语言·c++
FPGA小迷弟6 小时前
用 Claude Code 重构遗留系统:从评估到落地的完整实践指南
c++·python·ai·plotly·django·ai编程·claude
程序员-King.6 小时前
【ROS1】Noetic 开发工作流程与常用命令
c++·ubuntu·ros
云深麋鹿6 小时前
C++ | 基于红黑树实现mymap与myset
开发语言·c++
傲世仙尊8 小时前
string增删查改-insert-erase-replace与find加substr组合技
c++
·present·9 小时前
CSAPP(lecture 3)
c++
别动我齐刘海10 小时前
机器人 C++ / 系统联调补强2——异常处理、Protobuf
linux·c++·人工智能·学习·算法·机器学习·机器人
basketball61610 小时前
C++ 中字符串与常见类型的转换总结
开发语言·c++
等我调个Bug10 小时前
C++智能指针通俗讲解
开发语言·c++
kyle~12 小时前
时间同步 --- PTP 与 Scheduled Action Command
c++·数码相机·机器人·传感器