本文共 970 字,大约阅读时间需要 3 分钟。
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
Windows操作系统,C语言实现多线程:
#include
#include
DWORD APIENTRY ThreadOne ( LPVOID threadArg )
{
printf ( "线程开始啦,参数是:%s\n" , (char *)threadArg );
return 0;
}
int main ( void )
{
HANDLE hThread; /* 记录线程句柄 */
DWORD ThreadID; /* 记录线程ID号 */
DWORD waitingResult; /* 等待线程退出的等待结果 */
DWORD threadExitCode; /* 记录线程的返回值 */
char * aMessage = "这是线程的参数" ;
/* 创建并启动线程ThreadOne,返回值为线程句柄,赋值给hThread */
hThread = CreateThread ( NULL, 0L, ThreadOne, (LPVOID)aMessage, 0L, &ThreadID );
if ( hThread == NULL )
{
printf ("线程ThreadOne创建失败。错误代码:%lu\n", GetLastError() );
return EXIT_FAILURE ;
}
/* 等待线程句柄为的hThread线程结束 */
waitingResult = WaitForSingleObject ( hThread, INFINITE );
if ( waitingResult == WAIT_FAILED )
{
printf ( "等待线程退出等待失败。错误代码:%lu\n" , GetLastError() ) ;
return EXIT_FAILURE ;
}
if ( GetExitCodeThread ( hThread , &threadExitCode ) )
printf ( "线程的返回值是%lu\n", threadExitCode ) ;
else
printf ( "获取线程的返回值获取失败。错误代码:%lu\n" , GetLastError() ) ;
return EXIT_SUCCESS ;
}
转载地址:http://ycnzx.baihongyu.com/