环境
Window10, MinGW。
注意:
如果要使用C++11版本的编译器,需要在下载的MinGW中加入一些头文件,因为MinGW提供的编译器目前缺少有关Thread的头文件【1】。你可以在Gitee或者Github上搜索关键词mingw-std-threads,相关资源都可以使用。我使用的提取地址如下:
https://gitee.com/CreateMe/mingw-std-threads
在此感谢资源提供者!
将包中的头文件放入MInGW的如下位置,
C:\MinGW\lib\gcc\mingw32\6.3.0\include\c++
结果如下。
步骤
- 编写代码
测试代码参考此位置最下方第一条笔记,
https://www.runoob.com/cplusplus/cpp-multithreading.html
注意:
- 用我们刚刚下载的<mingw.thread.h>代替<thread.h>
问题:
- error To use the MinGW-std-threads library, you will need to define the macro _WIN32_WINNT to be 0x0501 (Windows XP) or higher.
原因是,宏_WIN32_WINNT是用于定义Windows和Internet Exporeer版本号的,,用于区分不用版本的函数和参数。在你编译程序时会根据不同的版本系统号,链接不同的函数和参数。【2】
你可以通过右键点击并查看这个宏的内容,可以有
#define _WIN32_WINNT_NT4 0x0400 /* Windows NT4 */
#define _WIN32_WINNT_NT4E 0x0401 /* Windows NT4E */
#define _WIN32_WINNT_NT4SP3 0x0403 /* Windows NT4 + ServicePack 3 */
#define _WIN32_WINDOWS_95 0x0400 /* Windows 95 */
#define _WIN32_WINDOWS_98 0x0410 /* Windows 98 */
#define _WIN32_WINDOWS_ME 0x0490 /* Windows Millenium Edition */
#define _WIN32_WINNT_WIN2K 0x0500 /* Windows 2000 */
#define _WIN32_WINNT_WINXP 0x0501 /* Windows XP */
#define _WIN32_WINNT_WS03 0x0502 /* Windows Server 2003 */
#define _WIN32_WINNT_WIN6 0x0600 /* Alias for Windows Vista */
#define _WIN32_WINNT_VISTA 0x0600 /* Windows Vista */
#define _WIN32_WINNT_WS08 0x0600 /* Windows Server 2008 */
#define _WIN32_WINNT_LONGHORN 0x0600 /* Alias for Windows Vista */
#define _WIN32_WINNT_WIN7 0x0601 /* Windows 7 */
#define _WIN32_WINNT_WIN8 0x0602 /* Windows 8 */
#define _WIN32_WINNT_WINBLUE 0x0603 /* Windows 8.1 */
这里需要对宏_WIN32_WINNT的定义大于0x0500,所以可以任选0x0500之后的Win版本。我修改为如下,
#ifndef _WIN32_WINNT
# ifdef WINVER
# define _WIN32_WINNT WINVER
# else
# ifdef _WARN_DEFAULTS
# warning "Assuming default setting of _WIN32_WINNT_WIN2K for _WIN32_WINNT"
# endif
# define _WIN32_WINNT **_WIN32_WINNT_WINBLUE**
# endif
#endif
再运行即可。