孔子的师德观包括:C++ 多线程问题

来源:百度文库 编辑:杭州交通信息网 时间:2024/04/25 14:00:38
如何在C++中实现多线程?比如说在一个线程在屏幕上打印1到100的每个数字,第2个线程在屏幕上打印从200到300的每个数字,最好有完整的C++程序,谢谢啦:)

#define WIN32_LEAN_AND_MEAN included correctly

#include <windows.h>
#include <windowsx.h>
#include <conio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <math.h>
#include <io.h>
#include <fcntl.h>

// DEFINES ////////////////////////////////////////////////////////////////////////////////

// PROTOTYPES /////////////////////////////////////////////////////////////////////////////

DWORD WINAPI Printer_Thread(LPVOID data);

// GLOBALS ////////////////////////////////////////////////////////////////////////////////

// FUNCTIONS //////////////////////////////////////////////////////////////////////////////

DWORD WINAPI Printer_Thread(LPVOID data)
{
// this thread function simply prints out data 25 times with a slight delay

for (int index=0; index<25; index++)
{
printf("%d ",data);
Sleep(100);
}

return((DWORD)data);

} // end Printer_Thread

// MAIN //////////////////////////////////////////////////////////////////////////////////

void main(void)
{

HANDLE thread_handle;
DWORD thread_id;

printf("\nStarting threads...\n");

thread_handle = CreateThread(NULL,0,Printer_Thread,(LPVOID)1,0,&thread_id);

for (int index=0; index<50; index++)
{
printf("2 ");
Sleep(100);

}

CloseHandle(thread_handle);

printf("\nAll threads terminated.\n");

} // end main