![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
CreateThread
1
HANDLE CreateThread(
2
LPSECURITY_ATTRIBUTES lpThreadAttributes, // pointer to security attributes
3
DWORD dwStackSize, // initial thread stack size
4
LPTHREAD_START_ROUTINE lpStartAddress, // pointer to thread function
5
LPVOID lpParameter, // argument for new thread
6
DWORD dwCreationFlags, // creation flags
7
LPDWORD lpThreadId // pointer to receive thread ID
8
);
9![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
参数信息:lpThreadAttributes 安全属性结构体指针
dwStackSize 分配给新线程的堆大小
lpStartAddress 只想新线程的入口函数 格式如下:
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
线程函数入口
1
DWORD WINAPI FunName(
2
LPVOID lpParameter
3
)
补充注意:入口函数不一定必须是上述格式(上述格式是msdn中提及的标准格式)
可以自定义函数,但是创建线程时必须函数类型转换,示例如下:
1
void fun(int &n)
2![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
3
cout<<"sdf"<<endl;
4
cout<<n<<endl;
5
}
6
int n=10;
7
HANDLE hThread3;
8
hThread3=CreateThread(NULL,0,(unsigned long (__stdcall *)(void *))fun,&n,0,NULL);
lpParameter 线程函数的参数
dwCreationFlags 创建标志(0 or CREATE_SUSPENDED)
lpThreadId 新线程ID的接收变量
返回:成功返回句柄对象(HANDLE对象) 否则返回NULL GetLastError()返回错误信息
实例:
hThread1=CreateThread(NULL,0,ThreadFun1,NULL,0,NULL);
DWORD WINAPI ThreadFun1(
LPVOID lpParameter
)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
cout<<"df"<<endl;
return 0;
}
1
BOOL CloseHandle(
2
HANDLE hObject // handle to object to close
3
);
4![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
关闭HANDLE句柄
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
CreateMutex
1
HANDLE CreateMutex(
2
LPSECURITY_ATTRIBUTES lpMutexAttributes,
3
// pointer to security attributes
4
BOOL bInitialOwner, // flag for initial ownership
5
LPCTSTR lpName // pointer to mutex-object name
6
);
7![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
参数信息:lpMutexAttributes 安全属性结构体指针 NULL是为默认的安全描叙符
bInitialOwner 为真时调用线程创建互斥对象,调用线程拥有互斥对象 为假时调用线程不拥有互斥对象
lpName 互斥对象的名称
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
WaitForSingleObject
1
DWORD WaitForSingleObject(
2
HANDLE hHandle, // handle to object to wait for
3
DWORD dwMilliseconds // time-out interval in milliseconds
4
);
5![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
参数信息:hHandle Mutex对象(临界区)
dwMiliseconds 时间间隔(毫秒),超过时间间隔函数就返回 为0时马上返回 为INFINITE时用不返回
返回:失败返回WAIT_FAILED 否则返回一个引发这个函数的事件
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
ReleaseMutex
1
BOOL ReleaseMutex(
2
HANDLE hMutex // handle to mutex object
3
);
4![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
返回:成功返回非零数 否则返回0
posted on 2009-08-30 14:18
人生在于攀登! 阅读(232)
评论(0) 编辑 收藏 引用