正文

自己敲的一个简单Thread例子 (vc6)2006-09-24 22:37:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/embed/18786.html

分享到:

//Demo1.cpp


//#include "stdafx.h"
#include "iostream.h"
//#include"process.h"//CreateThread(),ExitThread(),CloseHandle()
#include "windows.h"
#include "stdio.h"

/////////////////////////////////////线程 例子1////////////////////////
/*
BOOL repeat=TRUE; //几个线程共享全局变量repeat
DWORD WINAPI MyThread1(LPVOID lpParameter)
{
 while(repeat)
 {
  printf("How are you? -----1-------MyThread1\n");
  Sleep(5); ////
 }
 DWORD exitCode;
 ExitThread(exitCode);
 
 return 0;
}

DWORD WINAPI MyThread2(LPVOID lpParameter)
{
 while(repeat)
 {
  printf("Very Well! ------2------MyThread2\n");
  Sleep(1);///
 }
 DWORD exitCode;
 ExitThread(exitCode);
 
 return 0;
}

DWORD WINAPI KillThread(LPVOID lpParameter)
{
 repeat=FALSE;
 return 0;
}

int main(int argc,char* argv[])
{
 HANDLE handle1,handle2,handle3;
 DWORD dw1,dw2,dw3;

 handle1=CreateThread(NULL,0,MyThread1,NULL,0,&dw1);
 if(handle1==NULL)
 {

  printf("Create Thread 1 Failed!\n");
  return -1;
 }

 handle2=CreateThread(NULL,0,MyThread2,NULL,0,&dw2);
 if(handle2==NULL)
 {

  printf("Create Thread 2 Failed!\n");
  return -1;
 }
 Sleep(6000); /////

 handle3=CreateThread(NULL,0,KillThread,NULL,0,&dw3);
 if(handle3==NULL)
 {

  printf("Create Thread 3 Failed!\n");
  return -1;
 }
 CloseHandle(handle1);
 CloseHandle(handle2);
 CloseHandle(handle3);
 return 0;
}
*/
/////////////////////////////////////////////////

/////////////////////////////////////例子2////////////////////////
DWORD WINAPI MyThread(LPVOID lpParameter)
{
 for(int i=0;i<100;i++)
 {
  printf("hello,everybody! ------------MyThread\n");
  cout<<"My Thread ID:"<<GetCurrentThreadId()<<endl;
  Sleep(100); ////
 }
// DWORD exitCode;
// ExitThread(exitCode); 
 
 return 0;
}

int main(int argc,char* argv[])
{
 HANDLE handle;
 DWORD dw;

 handle=CreateThread(NULL,0,MyThread,NULL,0,&dw);
/* 
 if(handle==NULL)
 {

  printf("Create Thread  Failed!\n");
  return -1;
 }
 */
 Sleep(100);

 for(int i=0;i<10;i++)
 {
  printf("*****Now,Suspended!****\n");
  SuspendThread(handle); /////

  for(int j=0;j<3;j++)
  {
  printf("Good Thank you !\n");
  Sleep(100);
  }
  printf(">>>>Now Resumed!>>>>\n");
  ResumeThread(handle); /////
  Sleep(300);
 }

 GetExitCodeThread(handle,&dw);
 cout<<"Thread Exit ID:"<<GetCurrentThreadId()<<endl;

 CloseHandle(handle);
 return 0;
}

阅读(9922) | 评论(1)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

loading...
您需要登录后才能评论,请 登录 或者 注册