必须使用pthread库才能在MinGW环境下编译多线程程序?
must to use pthread library to compile multithreaded programs in MinGW environment?
在MinGW环境下编译多线程程序一定要使用pthread库吗?
我看到头文件在集成的MinGW中声明了_beginthreadex函数,在TrueStudio中。但程序运行出现异常。不知道是不是用了_beginthreadex函数
//process.h
/* Thread initiation and termination functions.
*
* NOTE: Apparently _endthread() calls CloseHandle() on the handle of the
* thread, creating a potential for race conditions, if you are not careful.
* Basically, you MUST ensure that NOTHING attempts to do ANYTHING with the
* thread handle after the thread calls _endthread(), or returns from the
* thread function.
*
* NOTE: No old names for these functions. Use the underscore.
*/
_CRTIMP __cdecl __MINGW_NOTHROW
unsigned long _beginthread (void (*)(void *), unsigned, void *);
_CRTIMP __cdecl __MINGW_NOTHROW void _endthread (void);
#ifdef __MSVCRT__
_CRTIMP __cdecl __MINGW_NOTHROW unsigned long _beginthreadex
(void *, unsigned, unsigned (__stdcall *) (void *), void *, unsigned, unsigned *);
_CRTIMP __cdecl __MINGW_NOTHROW void _endthreadex (unsigned);
#endif
更新:
以上信息是不是表明需要MSVC编译器支持_beginthreadex函数,而不是_beginthread函数?
不,不必使用 pthreads
。您可以直接使用 Win32 API。
hThreadArray[i] = CreateThread(
NULL, // default security attributes
0, // use default stack size
MyThreadFunction, // thread function name
pDataArray[i], // argument to thread function
0, // use default creation flags
&dwThreadIdArray[i]); // returns the thread identifier
如果你担心便携性,你也可以通过libuv
。
https://docs.microsoft.com/en-us/windows/win32/procthread/creating-threads
您应该使用 MinGW-w64 而不是 MinGW,它得到了更好的维护和更新,并且支持 Windows 32 位和 64 位。
至于线程,任何 MinGW(-w64) 都将提供 Windows 线程 API,但任何具有 POSIX 线程支持的 MinGW-w64 构建(如独立构建来自https://winlibs.com/) 还将允许您在 Windows.
上使用 pthreads
如果出于某种原因您无法使用经典的 MinGW,您还可以查看 https://sourceforge.net/projects/pthreads4w/ 以仍然能够使用 pthreads。
在MinGW环境下编译多线程程序一定要使用pthread库吗? 我看到头文件在集成的MinGW中声明了_beginthreadex函数,在TrueStudio中。但程序运行出现异常。不知道是不是用了_beginthreadex函数
//process.h
/* Thread initiation and termination functions.
*
* NOTE: Apparently _endthread() calls CloseHandle() on the handle of the
* thread, creating a potential for race conditions, if you are not careful.
* Basically, you MUST ensure that NOTHING attempts to do ANYTHING with the
* thread handle after the thread calls _endthread(), or returns from the
* thread function.
*
* NOTE: No old names for these functions. Use the underscore.
*/
_CRTIMP __cdecl __MINGW_NOTHROW
unsigned long _beginthread (void (*)(void *), unsigned, void *);
_CRTIMP __cdecl __MINGW_NOTHROW void _endthread (void);
#ifdef __MSVCRT__
_CRTIMP __cdecl __MINGW_NOTHROW unsigned long _beginthreadex
(void *, unsigned, unsigned (__stdcall *) (void *), void *, unsigned, unsigned *);
_CRTIMP __cdecl __MINGW_NOTHROW void _endthreadex (unsigned);
#endif
更新: 以上信息是不是表明需要MSVC编译器支持_beginthreadex函数,而不是_beginthread函数?
不,不必使用 pthreads
。您可以直接使用 Win32 API。
hThreadArray[i] = CreateThread(
NULL, // default security attributes
0, // use default stack size
MyThreadFunction, // thread function name
pDataArray[i], // argument to thread function
0, // use default creation flags
&dwThreadIdArray[i]); // returns the thread identifier
如果你担心便携性,你也可以通过libuv
。
https://docs.microsoft.com/en-us/windows/win32/procthread/creating-threads
您应该使用 MinGW-w64 而不是 MinGW,它得到了更好的维护和更新,并且支持 Windows 32 位和 64 位。
至于线程,任何 MinGW(-w64) 都将提供 Windows 线程 API,但任何具有 POSIX 线程支持的 MinGW-w64 构建(如独立构建来自https://winlibs.com/) 还将允许您在 Windows.
上使用 pthreads如果出于某种原因您无法使用经典的 MinGW,您还可以查看 https://sourceforge.net/projects/pthreads4w/ 以仍然能够使用 pthreads。