當前位置:編程學習大全網 - 源碼下載 - c語言中怎樣創建多線程?

c語言中怎樣創建多線程?

/*這是我寫的最簡單的多線程程序,看懂不?*/

#include <windows.h>

#include <stdio.h>

//#include <strsafe.h>

DWORD WINAPI ThreadProc1( LPVOID lpParam )

{

int i=0,j=0;

while(1)

{

printf("hello,this thread 1 ...\n");

//延時

for(i=0;i<200000000;i++)

{

;

}

}

}

DWORD WINAPI ThreadProc2( LPVOID lpParam )

{

int i=0,j=0;

while(1)

{

printf("hello,this thread 2 ...\n");

//延時

for(i=0;i<200000000;i++)

{

;

}

}

}

void main()

{

int i=0;

//創建線程1

CreateThread(

NULL, // default security attributes

0, // use default stack size

ThreadProc1, // thread function

NULL, // argument to thread function

0, // use default creation flags

NULL); // returns the thread identifier

//創建線程2

CreateThread(

NULL, // default security attributes

0, // use default stack size

ThreadProc2, // thread function

NULL, // argument to thread function

0, // use default creation flags

NULL); // returns the thread identifier

//讓主線程進入循環,主線程若退出,子線程1,2會被系統“殺死”

while(1)

{

printf("hello,this thread 0 ...\n");

//延時

for(i=0;i<200000000;i++)

{;}

}

}

  • 上一篇:硬盤主軸驅動原理
  • 下一篇:問幾個關於PSP比較小白的問題....
  • copyright 2024編程學習大全網