zoukankan      html  css  js  c++  java
  • win32多线程程序设计

    标题是一本书名,写得挺有意思的,是今天早上同事带过来的,我借过来看了一会儿.

    然后按照书里面前面几章的内容敲了一些代码,跑了几个例子看了一下.

    创建线程的函数:

    HANDLE CreateThread(

    LPSECURITY_ATTRIBUTES lpThreadAttributes,

    SIZE_T dwStackSize,       

    LPTHREAD_START_ROUTINE lpStartAddress,       

    LPVOID lpParameter,       

    DWORD dwCreationFlags,       

    LPDWORD lpThreadId );

    lpThreadAttributes 描述施行于这一新线程的security属性.NULL表示使用缺省值.

    dwStackSize 新线程拥有自己的堆栈,0表示使用缺省值:1MB

    lpStartAddress 新线程将开始的地址.这是一个函数指针.

    lpParameter 此值将被传送到上述所指定之新线程函数去,作为参数.

    dwCreationFlags 允许你产生一个暂时挂起的线程,默认情况是立即开始执行.

    lpThreadId 新线程的ID会被传回到这里,他是一个全局变量,可以独一无二地表示系统任一进程中的某个线程.

    如果创建成功,传回一个handle,代表新进程,否则传回一个false

    例子代码如下:

    #define WIN32_LEAN_AND_MEAN
    #include <stdio.h>
    #include <stdlib.h>
    #include <Windows.h>
    #include <conio.h>
    
    DWORD WINAPI ThreadFunc(LPVOID);
    
    #define Test2
    
    #ifdef Test1
    int main()
    {
        HANDLE hThrd;
        DWORD threadId;
        int i;
        for (i=0;i<5;i++)
        {
            hThrd = CreateThread(NULL,0,ThreadFunc,(LPVOID)i,0,&threadId);
            if (hThrd)
            {
                printf("Thread launched %d
    ",i);
                CloseHandle(hThrd);
            }
        }
        //Sleep(2000);
        system("pause");
        return EXIT_SUCCESS;
    }
    
    DWORD WINAPI ThreadFunc(LPVOID n)
    {
        int i;
        for (i=0;i<10;i++)
        {
            printf("*************%d
    ",n);
        }
        return 0;
    }
    #endif
    
    #ifdef Test2
    int main()
    {
        HANDLE hThrd1;
        HANDLE hThrd2;
        DWORD exitCode1 = 0;
        DWORD exitCode2 = 0;
        DWORD threadId;
    
        hThrd1 = CreateThread(NULL,0,ThreadFunc,(LPVOID)1,0,&threadId);
        if (hThrd1)
        {
            printf("Thread 1 launched
    ");
        }
    
        hThrd2 = CreateThread(NULL,0,ThreadFunc,(LPVOID)2,0,&threadId);
        if (hThrd2)
        {
            printf("Thread 2 launched
    ");
        }
    
        for(;;)
        {
            printf("Press any key to exit..
    ");
            getch();
    
            GetExitCodeProcess(hThrd1,&exitCode1);
            GetExitCodeProcess(hThrd2,&exitCode2);
            if (exitCode1 == STILL_ACTIVE)
            {
                puts("Thread 1 is still running!");
            }
            if (exitCode2 == STILL_ACTIVE)
            {
                puts("Thread 2 is still running!");
            }
            if (exitCode1 != STILL_ACTIVE && exitCode2 != STILL_ACTIVE)
            {
                break;
            }
        }
        CloseHandle(hThrd1);
        CloseHandle(hThrd2);
    
        printf("Thread 1 returnd %d
    ",exitCode1);
        printf("Thread 2 returnd %d
    ",exitCode2);
    
        //system("pause");
        return EXIT_SUCCESS;
    }
    
    DWORD WINAPI ThreadFunc(LPVOID n)
    {
        Sleep((DWORD)n*1000*2);
        return (DWORD)n*10;
    }
    #endif
    View Code


    一些个人感想:同事是中南大学计算机06级的,之前一直在上海工作,已经工作6,7年了,我发现他的书真是多,隔三差五

    就带一本计算机方面的书过来,而且都是一些我之前从来没有接触过的书,真是涨了见识,而且他为人非常和善,跟他交流过

    几次,没有一点架子,很乐于跟你分享他的一些经验,跟高手待在一起就是好啊!

  • 相关阅读:
    python uuid
    linux 修改时区
    Nginx 缓存命中率
    MongoDB oplog 详解
    MongoDB 复本集搭建
    复制集简介
    解决Python2.7的UnicodeEncodeError: 'ascii' codec can't encode异常错误
    MongoDB 介绍
    python virtualenv
    Docker Compose 模板文件 V2
  • 原文地址:https://www.cnblogs.com/fxl-njfu/p/3296346.html
Copyright © 2011-2022 走看看