2012年8月20日 星期一

[C++] Threading Sample

This is a simple sample for understanding of thread create in C++.

#include "stdafx.h"
#include <windows.h>
#include <process.h>
#include <stdio.h>

void myfunc(int n);

int _tmain(int argc, _TCHAR* argv[])
{
    HANDLE thd[2];
    DWORD tid;

    printf("Thead Start.\n");

    for (int i=0; i<2; i++)
    {
        thd[i] = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)myfunc, (void*)i, 0, &tid);
        printf("Thread %d is start \n",tid);
    }


    Sleep(1000);

    printf("main is end \n");

    system("pause");
}

void myfunc(int n)
{
    int i;
    for(int i=0;i<5;i++){
        printf("Thread %d, index=%d\n", n, i);
        Sleep(1000);
    }
    printf("Thread %d is over \n", getpid());
}

Reference:
http://wuminfajoy.blogspot.tw/2010/10/c.html

沒有留言:

張貼留言