• CreateThread demo




    #include "stdafx.h"
    #include<windows.h>
    #include<strsafe.h>//win2003SDK必须安装 要不无此头文件。此文件是为了实现StringCchPrintf,StringCchLength。

    #define MAX_THREADS 5
    #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;
      
      pData=(PMYDATA)lpParam;

      StringCchPrintf(msgBuf,BUF_SIZE,TEXT("Parameters=%d,%d "),pData->val1,pData->val2);
      //printf("%d,%d ",pData->val1,pData->val2);
      StringCchLength(msgBuf,BUF_SIZE,&cchStringSize);
      WriteConsole(hStdout,msgBuf,cchStringSize,&dwChars,NULL);


      HeapFree(GetProcessHeap(),0,pData);
      return 0;
    }
    void main()
    {
      PMYDATA pData;
      DWORD dwThreadId[MAX_THREADS];
      HANDLE hThread[MAX_THREADS];
      int i;

      for(i=0;i<MAX_THREADS;i++)
      {

        pData=(PMYDATA)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(MYDATA));
        if(pData==NULL)
          ExitProcess(2);

        pData->val1=i;
        pData->val2=i+100;
        hThread[i]=CreateThread(
                      NULL,//defaultsecurityattributes
                      0,//usedefaultstacksize
                      ThreadProc,//threadfunction
                      pData,//argumenttothreadfunction
                      0,//usedefaultcreationflags
                      &dwThreadId[i]);//returnsthethreadidentifier

        if(hThread[i]==NULL)
        {
          ExitProcess(i);
        }
      }

      WaitForMultipleObjects(MAX_THREADS,hThread,TRUE,INFINITE);

      for(i=0;i<MAX_THREADS;i++)
      {
        CloseHandle(hThread[i]);
      }

      system("pause");
    }

  • 相关阅读:
    Row versus Set Processing, Surprise!(集合处理和单行处理数据的差异性)
    SVN导出增量包的方法
    Number of Digit One
    VPS+LNMP+WordPress搭建个人网站
    寻找最小的k个数(大顶堆方法)
    二叉搜索树转化为有序双向链表(C语言)
    LeetCode 345 Reverse Vowels of a String
    LeetCode 278 First Bad Version
    LeetCode 205 Isomorphic Strings
    LeetCode 88 Merge Sorted Array
  • 原文地址:https://www.cnblogs.com/duyy/p/3711252.html
Copyright © 2020-2023  润新知