CreateThread

1.线程创建API

  1. CreateThread,需要调用到CRT库时,不要用这个;
  2.  BeginThread,RTL库里的System单元中定义;
  3. 在MFC程序中,应该调用AfxBeginThread函数;
  4. 在Visual C++程序中应调用_beginthreadex函数;需要调用到CRT库时。

demo

// DemoFunc.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <windows.h>
#include <strsafe.h>//win2003SDK必须安装 要不无此头文件。此文件是为了实现StringCchPrintf,StringCchLength。

#define MAX_THREADS    3
#define BUF_SIZE    255

typedef struct _MyData{
    int val1;
    int val2;
}MYDATA,*PMYDATA;

DWORD WINAPI ThreadProc(LPVOID lpParam)
{
    HANDLE hStdout;
    PMYDATA pData;
    TCHAR msgBuf[BUF_SIZE];
    size_t cchStringSize;
    DWORD dwChars;
    hStdout=GetStdHandle(STD_OUTPUT_HANDLE);
    if(hStdout==INVALID_HANDLE_VALUE)
        return 1;
    //Cast the parameter to the correct data type.
    pData=(PMYDATA)lpParam;
    //Print the parameter values using thread-safe functions.
    StringCchPrintf(msgBuf,BUF_SIZE,TEXT("Parameters=%d,%d\n"), 
        pData->val1, pData->val2);
    StringCchLength(msgBuf, BUF_SIZE, &cchStringSize);
    WriteConsole(hStdout,msgBuf,cchStringSize,&dwChars,NULL);
    //Free the memory allocated by the caller for the thread data structure.
    HeapFree(GetProcessHeap(),0,pData);
    return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
    PMYDATA pData;
    DWORD dwThreadId[MAX_THREADS];
    HANDLE hThread[MAX_THREADS];
    int i;
    //Create MAX_THREADS worker threads.
    for(i = 0; i < MAX_THREADS; i++)
    {
        //Allocate memory for thread data.
        pData=(PMYDATA)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
            sizeof(MYDATA));
        if(pData==NULL)
            ExitProcess(2);

        //Generate unique data for each thread.
        pData->val1 = i;
        pData->val2 = i + 100;

        hThread[i]=CreateThread(
            NULL,//default security attributes
            0,//use default stack size
            ThreadProc,//thread function
            pData,//argument to thread function
            0,//use default creation flags
            &dwThreadId[i]);//returns the thread identifier

        //Check there turn value for success.
        if(hThread[i] == NULL)
        {
            ExitProcess(i);
        }
    }
    //Wait until all threads have terminated.
    WaitForMultipleObjects(MAX_THREADS, hThread, TRUE, INFINITE);
    //Close all thread handles upon completion.
    for(i = 0; i < MAX_THREADS; i++)
    {
        CloseHandle(hThread[i]);
    }  
    getchar();
    return 0;
}

函数

  1. WaitForMultipleObjects
  2. WaitForSingleObject

多线程同步方法:

  • Console input buffers
  • Events
  • Mutexes
  • Processes
  • Semaphores
  • Threads
  • Timers

参考

  https://blog.csdn.net/believe_s/article/details/82623531(参数说明)

  https://blog.csdn.net/believe_s/article/details/82624315(demo和相关的注意)

  https://docs.microsoft.com/zh-cn/windows/desktop/Sync/using-synchronization(多线程同步)

  http://www.cnblogs.com/LouMengzhao/p/6076368.html(event和WaitForSingleObject配合使用)

posted @ 2019-05-19 11:23  N_zero  阅读(274)  评论(0)    收藏  举报