多线程基础

首先查看一下MSDN几个和多线程有关的API函数:

This function creates a thread to execute within the address space of the calling process.

HANDLE CreateThread(
  LPSECURITY_ATTRIBUTES lpsa, 
  DWORD cbStack, 
  LPTHREAD_START_ROUTINE lpStartAddr, 
  LPVOID lpvThreadParam, 
  DWORD fdwCreate, 
  LPDWORD lpIDThread
); 

Parameters

lpsa
[in] Ignored. Must be NULL.
如果忽略,该参数必须设置为NULL
cbStack
[in] Ignored unless the STACK_SIZE_PARAM_IS_A_RESERVATION flag is used; in that case the cbStack parameter specifies the virtual memory reserved for the new thread.

When ignored, the default stack size for a thread is determined by the linker setting /STACK.

如果忽略,线程需要的堆栈虚拟内存大小分配由连接器决定,可以人为在连接器中设置;

如果忽略,该参数设置为0;

lpStartAddr
[in] Long pointer to the application-defined function of type LPTHREAD_START_ROUTINE to be executed by the thread; represents the starting address of the thread. For more information on the thread function, see ThreadProc.
Note   It is invalid to set lpStartAddr to NULL. If this value is passed in the parameter, the function will return ERROR_INVALID_PARAMETER.
该参数是线程入口函数的地址,函数声明形式参考ThreadProc:

This function is an application-defined function that serves as the starting address for a thread.

Specify this address when calling the CreateThread function.

The LPTHREAD_START_ROUTINE type defines a pointer to this callback function.

ThreadProc is a placeholder for the application-defined function name.

DWORD ThreadProc(
  LPVOID lpParameter
);
线程入口函数的参数不能够设置为NULL,否则会收到错误信息,信息类型:ERROR_INVALID_PARAMETER
lpvThreadParam
[in] Long pointer to a single 32-bit parameter value passed to the thread.
可以向线程函数传递一个32位的参数给线程.
 
fdwCreate
[in] Specifies flags that control the creation of the thread.

The following table shows the values for this parameter.

预设值线程悬挂或是创建后就开始启动运行,如果设置为0,创建后线程立刻执行.

ValueDescription
CREATE_SUSPENDED The thread is created in a suspended state and does not run until the ResumeThread function is called.

The thread can be run immediately after creation if the flag is not specified.

STACK_SIZE_PARAM_IS_A_RESERVATION The cbStack parameter specified the maximum stack size instead of being ignored.

This is a Windows CE only flag for the CreateThread function and is used to reserve the stack size for the thread created.

By default, a thread is created with 64 KB stack reserved. You can use this flag to increase the stack size for the new thread that was created.

lpIDThread
[out] Long pointer to a 32-bit variable that receives the thread identifier.

If this parameter is NULL, the thread identifier is not returned.

接受线程ID,现在该参数可以设置为NULL

This function closes an open object handle. A remote application programming interface (RAPI) version of this function exists, named CeCloseHandle (RAPI).

BOOL CloseHandle( 
  HANDLE hObject
); 

 关闭主线程不感兴趣的线程句柄,但是线程仍然可以运行.

This function suspends the execution of the current thread for a specified interval.

void Sleep(
  DWORD dwMilliseconds
); 

释放时间片段,参数以ms为单位.
根据上面几个就可以做一个简单程序:

#include<windows.h>
#include<iostream.h>
DWORD WINAPI FuncThreadone(LPVOID lpParameter);
DWORD WINAPI FuncThreadTwo(LPVOID lpParameter);

void main(){
 HANDLE hThread1,hThread2;
 hThread1=CreateThread(NULL,0,FuncThreadone,NULL,0,NULL);
 hThread2=CreateThread(NULL,0,FuncThreadTwo,NULL,0,NULL);
 CloseHandle(hThread1);
 CloseHandle(hThread2);
 cout<<"main thread is running"<<endl;
 Sleep(3);
}

DWORD WINAPI FuncThreadone(LPVOID lpParameter){

 cout<<"thread one is running !"<<endl;
 return 0;

}

DWORD WINAPI FuncThreadTwo(LPVOID lpParameter){

 cout<<"thread two is running !"<<endl;
 return 0;

}

运行就有结果了

 
posted @ 2013-05-02 15:32  MMLoveMeMM  阅读(202)  评论(0)    收藏  举报