多线程用法-1

初步实例介绍CreateThread的用法:

带有参数的用法:

#include "windows.h"
#include "stdio.h"
void Scan(char* str)
{
 printf("%s",str);
}
void Start()
{
  char* s="NBA YAO YEAR!/n";
 HANDLE hThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)Scan,s,0,NULL);
 if(CloseHandle(hThread))
 {
  printf("/nClose Thread Success!/n");
 }
 else
 {
  printf("/nClose Thread Failed!");
 }
}
void main()
{
 int i=0;
 for(;i<10;i++)
 {
  Start();
 }
 
/*参数说明: 
HANDLE CreateThread( 
LPSECURITY_ATTRIBUTES lpThreadAttributes,//必须为NULL  
DWORD dwStackSize, //一般为0 ,表示堆栈与外部大小相同 
LPTHREAD_START_ROUTINE lpStartAddress, //线程函数名称 
LPVOID lpParameter, //传递给线程函数的参数,如果为多个,自定义结构体 
DWORD dwCreationFlags,//0表示创建线程后立即启动线程,如果不是立即启动需要调用ResumeThread函数 
LPDWORD lpThreadId); //用来标记该线程的名称 
*/

}

//不带有参数的用法

// suanfa.cpp : Defines the entry point for the console application.
//

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

#define THREAD_NUM 1

DWORD WINAPI PrintThreads (void *);

int main () 

  HANDLE hThread[THREAD_NUM]; 
  DWORD dwThreadID[THREAD_NUM]; 
  char *s="guochuntao";
  for (int i=0; i<THREAD_NUM; ++i) 
  { 
  int isStartImmediate = 0;

  if(3 == i)
  isStartImmediate = CREATE_SUSPENDED;

  hThread[i]=CreateThread(NULL, // security attributes that should be applied to the new thread, 
  // this is for NT. Use NULL to get the default security attributes. Use NULL for win95 
  0, // default size of 1MB can be passed by passing zero. 
  (LPTHREAD_START_ROUTINE)PrintThreads, // function name:address of the function where the new thread starts.
  NULL, // parameter(void pointer): pointer to the 32 bit parameter that will be passed into the thread
  isStartImmediate, // flags to control the creation of the thread. Passing zero starts the thread immediately. 
  // Passing CREATE_SUSPENDED suspends the thread until the ResumeThread( ) function is called.
  &dwThreadID[i] // pointer to a 32-bit variable that receives the thread identifier.
  ); 
  if (hThread[i])
  { 
  printf ("Thread launched successfully/n");  
  }  
  } 

  printf("Start sleep 100, and let other thread excute/n");
  Sleep (100);  

  printf("Start sleep 100, and thread 3 excute/n");
  ResumeThread(hThread[3]);
   
  Sleep(100);

  for(i = 0; i<THREAD_NUM; ++i)
  {
  if (hThread[i])
  {  
  CloseHandle(hThread[i]); // You need to use this to release kernel objects when you are done using them. 
  // If a process exits without closing the thread handle, 
  // the operating system drops the reference counts for those objects. 
  // But if a process frequently creates threads without closing the handles, 
  // there could be hundreds of thread kernel objects lying around and these resource leaks can have a big hit on performance.
  } 
  }
  return (0); 


//function PrintThreads 
DWORD WINAPI PrintThreads (void* s)
{
 int num=0;
  for (int i=0; i<1; i++) 
  // printf ("Thread Number is %d%d%d/n", num,num,num); 
  return 0;
}
二 其他基本API的说明
CreateThread() 调用成功返回句柄和一个id。
CloseHandle() 关闭一个打开的对象句柄,该对象句柄可以是线程句柄,也可以是进程、信号量等其他内核对象的句柄.

SuspendThread(HANDLE) 允许开发人员将HANDLE指定的线程挂起,如果要挂起的线程占有共享资源,则可能导致死锁。
ResumeThread(HANDLE) 恢复指定的线程。

TerminateThread() 立即终止线程的工作,不做任何清理工作。
ExitThread() 线程函数返回时回调用次函数,所以一般我们不去显示的调用。

ExitThread是推荐使用的结束一个线程的方法,当调用该函数时,当前线程的栈被释放,然后线程终止,相对于TerminateThread函数来说,这样做能够更好地完成附加在该线程上的DLL的清除工作. 但是ExitThread()会导致线程在清处构造器/自动变量之前就终止,所以我们最好不要显示的调用ExitThread()。

//支持多个参数的线程

// testth.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include "windows.h"
#include "stdio.h"

struct Student
{
 int age;
 int number;
}
 data;

void Scan(Student* tempdata)
{
 printf("%d,%d",tempdata->age,tempdata->number);
  char * str="guochuntao";
 printf("%s",str);
}
void Start()
{
  data.age=100+1;
  data.number=2000+1;
 
 char* s="NBA YAO YEAR!/n";
 
 HANDLE hThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)Scan,&data,0,NULL);
 
 if(CloseHandle(hThread))
 {
  printf("/nClose Thread Success!/n");
 }
 else
 {
  printf("/nClose Thread Failed!");
 }
}
void main()
{
 int i=0;
 for(;i<6;i++)
 {
  Start();
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值