Vc++多线程编程
vc++中的多线程编程:
HANDLE hMutex; // Create a mutex with no initial owner. 必须要创建一个句柄
hMutex = CreateMutex( NULL, // no security attributes
FALSE, // initially not owned
"
MutexToProtectDatabase " ); // name of mutex
//
这个句柄只想信号量,第一个参数一半为NULL,第二个(初始化时是否被获得)false为现在可以被任何人使用
//
第三个参数是为信号量起名,方便以后查找该信号量
if
(hMutex == NULL)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBloc
kStart.gif) ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/
ContractedBlock.gif) … {
// Check for error.一般不必这样用。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBloc
kEnd.gif) }
BOOL FunctionToWriteToDatabase(HANDLE hMutex)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBloc
kStart.gif) ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/
ContractedBlock.gif) … {
DWORD dwWaitResult; // Request ownership of mutex.
dwWaitResult = WaitForSingleObject(
hMutex, // handle to mutex
5000L ); // five-second time-out interval
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubB
lockStart.gif) ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicato
rs/ContractedSubBlock.gif) switch (dwWaitResult) … {
// The thread got mutex ownership.
case WAIT_OBJECT_0:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubB
lockStart.gif) ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicato
rs/ContractedSubBlock.gif) __try … {
// Write to the database.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubB
lockEnd.gif) }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubB
lockStart.gif) ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicato
rs/ContractedSubBlock.gif) __finally … {
// Release ownership of the mutex object.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubB
lockStart.gif) ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicato
rs/ContractedSubBlock.gif) if ( ! ReleaseMutex(hMutex)) … {
// Deal with error.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubB
lockEnd.gif) }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubB
lockEnd.gif) break ; }
// Cannot get mutex ownership due to time-out.
case WAIT_TIMEOUT:
return FALSE;
// Got ownership of the abandoned mutex object.
case WAIT_ABANDONED:
return FALSE;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubB
lockEnd.gif) }
return TRUE;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBloc
kEnd.gif) }
DWORD dwWaitResult 只是为了判断信号量的归谁所有。 WaitForSingleObject() 相当与P()操作,参数有两个:第一个是
信号量,第二个是等待时间。INFINITE代表无限等待。5000L代表等待5秒,如果5秒内获得信号量,dwWaitResult==
WAIT_OBJECT_0,如果5秒没有得到信号量,则dwWaitResult== WAIT_TIMEOUT,而
WAIT_ABANDONED代表操作失败。 ReleaseMutex(hMutex)相当于V()操作。参数为信号量。
如果希望在一个函数中启动线程并调用该函数,应该这样写代码
void reading(void* name);这个函数一定要是void的返回类型。然后可以在调用函数中这样写:
_beginthread(&reading,0,(void*)english); 其中第一参数是要调用函数的函数指针。第二个函数是线程优先级一般为0。第三个
参数是个void指针,一般用来传函数的参数。如果想传多个参数只要把那个指针指向一个结构体就可以。到函数那里再转换为相应的结构体指针就可以了。
Sleep(5000L);这个函数一般是用来让线程睡眠的。参数是unsigned long,现在是睡5秒。
这只是多线程的编程常用到的几个函数。对于多线程编程还要注意同步和死锁,在这篇文字就不讨论了。