busy3.c

来自「侯捷先生翻译的书籍《Win32多线程程序设计》的源代码。」· C语言 代码 · 共 62 行

C
62
字号
/*
 * Busy3.c
 *
 * Sample code for "Multithreading Applications in Win32"
 * This sample is mentioned in the text but there is
 * no associated listing in the book.
 *
 * Use a busy loop to wait on a thread
 * that is sleeping.  This sample should
 * be used in conjunction with the
 * performance meter in Windows NT.
 *
 * Build command: cl /MD busy3.c
 */

#define WIN32_LEAN_AND_MEAN
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "MtVerify.h"

DWORD WINAPI ThreadFunc(LPVOID);

int main()
{
    HANDLE hThrd;
    DWORD exitCode = 0;
    DWORD threadId;

    puts("Waiting...");

    MTVERIFY( hThrd = CreateThread(NULL,
        0,
        ThreadFunc,
        (LPVOID)1,
        0,
        &threadId )
    );
    /* This busy loop chews up lots of CPU time */
    for (;;)
    {
        GetExitCodeThread(hThrd, &exitCode);
        if ( exitCode != STILL_ACTIVE )
            break;
    }

    printf("Finished.\n");

    MTVERIFY( CloseHandle(hThrd) );

    return EXIT_SUCCESS;
}


DWORD WINAPI ThreadFunc(LPVOID n)
{
    UNREFERENCED_PARAMETER(n);

    Sleep(60);
    return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?