学习使用Depend工具,#pragma, Mutex,Event
2010-03-02 13:20
483 查看
1.
可以方便的看到哪些模块,关联到了这个dll,
/Program Files/Microsoft Visual Studio 8/Common7/Tools/Bin/depends.exe
具体分析待阅读:
http://www.vckbase.com/document/viewdoc/?id=1590
2. pragmatics 语法论
vc下的编译使用命令:
#pragma comment(lib,"dll_name.lib") 隐式链接dll_name.lib
可以在头文件中写入上边的语句,然后,
class __declspec(dllimport) CMyClassName
{
.........
}
3. 使用Mutex,防止进程或线程间的同时操作一个资源。
Copy From MSDN
shared resource from simultaneous access by multiple threads or processes. Each
thread must wait for ownership of the mutex before it can execute the code that
accesses the shared resource. For example, if several threads share access to a
database, the threads can use a mutex object to permit only one thread at a time
to write to the database.
In the following example, a process uses the CreateMutex function to create a mutex
object.
When a thread of this process writes to the database, as in the next example,
it first requests ownership of the mutex. If it gets ownership, the thread
writes to the database and then releases its ownership.
The example uses structured exception-handling syntax to ensure that the
thread properly releases the mutex object. The __finally block of code is
executed no matter how the __try block terminates (unless the
__try block includes a call to the TerminateThread function). This prevents
the mutex object from being abandoned inadvertently.
4. 学习Event
situations to notify a waiting thread of the occurrence of an event. For
example, overlapped I/O operations on files, named pipes, and communications
devices use an event object to signal their completion. For more information
about the use of event objects in overlapped I/O operations, see Synchronization and
Overlapped Input and Output.
In the following example, an application uses event objects to prevent
several threads from reading from a shared memory buffer while a master thread
is writing to that buffer. First, the master thread uses the CreateEvent function to create a manual-reset
event object. The master thread sets the event object to nonsignaled when it is
writing to the buffer and then resets the object to signaled when it has
finished writing. Then it creates several reader threads and an auto-reset event
object for each thread. Each reader thread sets its event object to signaled
when it is not reading from the buffer.
Before the master thread writes to the shared buffer, it uses the ResetEvent function to set the state of
hGlobalWriteEvent (an application-defined global variable) to nonsignaled. This
blocks the reader threads from starting a read operation. The master then uses
the WaitForMultipleObjects
function to wait for all reader threads to finish any current read operations.
When WaitForMultipleObjects returns, the master thread can safely write
to the buffer. After it has finished, it sets hGlobalWriteEvent and all the
reader-thread events to signaled, enabling the reader threads to resume their
read operations.
Before starting a read operation, each reader thread uses WaitForMultipleObjects to wait for
the application-defined global variable hGlobalWriteEvent and its own read event
to be signaled. When WaitForMultipleObjects returns, the reader thread's
auto-reset event has been reset to nonsignaled. This blocks the master thread
from writing to the buffer until the reader thread uses the SetEvent function to set the event's state back
to signaled.
可以方便的看到哪些模块,关联到了这个dll,
/Program Files/Microsoft Visual Studio 8/Common7/Tools/Bin/depends.exe
具体分析待阅读:
http://www.vckbase.com/document/viewdoc/?id=1590
2. pragmatics 语法论
vc下的编译使用命令:
#pragma comment(lib,"dll_name.lib") 隐式链接dll_name.lib
可以在头文件中写入上边的语句,然后,
class __declspec(dllimport) CMyClassName
{
.........
}
3. 使用Mutex,防止进程或线程间的同时操作一个资源。
Copy From MSDN
Using Mutex Objects
You can use a mutex object to protect ashared resource from simultaneous access by multiple threads or processes. Each
thread must wait for ownership of the mutex before it can execute the code that
accesses the shared resource. For example, if several threads share access to a
database, the threads can use a mutex object to permit only one thread at a time
to write to the database.
In the following example, a process uses the CreateMutex function to create a mutex
object.
HANDLE hMutex; // Create a mutex with no initial owner. hMutex = CreateMutex( NULL, // default security attributes FALSE, // initially not owned NULL); // unnamed mutex if (hMutex == NULL) { printf("CreateMutex error: %d/n", GetLastError()); }
When a thread of this process writes to the database, as in the next example,
it first requests ownership of the mutex. If it gets ownership, the thread
writes to the database and then releases its ownership.
The example uses structured exception-handling syntax to ensure that the
thread properly releases the mutex object. The __finally block of code is
executed no matter how the __try block terminates (unless the
__try block includes a call to the TerminateThread function). This prevents
the mutex object from being abandoned inadvertently.
BOOL FunctionToWriteToDatabase(HANDLE hMutex) { DWORD dwWaitResult; // Request ownership of mutex. dwWaitResult = WaitForSingleObject( hMutex, // handle to mutex 5000L); // five-second time-out interval switch (dwWaitResult) { // The thread got mutex ownership. case WAIT_OBJECT_0: __try { // Write to the database. } __finally { // Release ownership of the mutex object. if (! ReleaseMutex(hMutex)) { // Deal with error. } 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; } return TRUE; }
4. 学习Event
Platform SDK: DLLs, Processes, and Threads |
Using Event Objects
Applications use event objects in a number ofsituations to notify a waiting thread of the occurrence of an event. For
example, overlapped I/O operations on files, named pipes, and communications
devices use an event object to signal their completion. For more information
about the use of event objects in overlapped I/O operations, see Synchronization and
Overlapped Input and Output.
In the following example, an application uses event objects to prevent
several threads from reading from a shared memory buffer while a master thread
is writing to that buffer. First, the master thread uses the CreateEvent function to create a manual-reset
event object. The master thread sets the event object to nonsignaled when it is
writing to the buffer and then resets the object to signaled when it has
finished writing. Then it creates several reader threads and an auto-reset event
object for each thread. Each reader thread sets its event object to signaled
when it is not reading from the buffer.
#define NUMTHREADS 4 HANDLE hGlobalWriteEvent; HANDLE hReadEvents[NUMTHREADS]; void CreateEventsAndThreads(void) { HANDLE hThread; DWORD i, IDThread; // Create a manual-reset event object. The master thread sets // this to nonsignaled when it writes to the shared buffer. hGlobalWriteEvent = CreateEvent( NULL, // default security attributes TRUE, // manual-reset event TRUE, // initial state is signaled "WriteEvent" // object name ); if (hGlobalWriteEvent == NULL) { printf("CreateEvent failed (%d)/n", GetLastError()); return; } // Create multiple threads and an auto-reset event object // for each thread. Each thread sets its event object to // signaled when it is not reading from the shared buffer. for(i = 0; i < NUMTHREADS; i++) { // Create the auto-reset event. hReadEvents[i] = CreateEvent( NULL, // no security attributes FALSE, // auto-reset event TRUE, // initial state is signaled NULL); // object not named if (hReadEvents[i] == NULL) { printf("CreateEvent failed (%d)/n", GetLastError()); return; } hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) ThreadFunction, &hReadEvents[i], // pass event handle 0, &IDThread); if (hThread == NULL) { printf("CreateThread failed (%d)/n", GetLastError()); return; } } }
Before the master thread writes to the shared buffer, it uses the ResetEvent function to set the state of
hGlobalWriteEvent (an application-defined global variable) to nonsignaled. This
blocks the reader threads from starting a read operation. The master then uses
the WaitForMultipleObjects
function to wait for all reader threads to finish any current read operations.
When WaitForMultipleObjects returns, the master thread can safely write
to the buffer. After it has finished, it sets hGlobalWriteEvent and all the
reader-thread events to signaled, enabling the reader threads to resume their
read operations.
VOID WriteToBuffer(VOID) { DWORD dwWaitResult, i; // Reset hGlobalWriteEvent to nonsignaled, to block readers. if (! ResetEvent(hGlobalWriteEvent) ) { printf("ResetEvent failed (%d)/n", GetLastError()); return; } // Wait for all reading threads to finish reading. dwWaitResult = WaitForMultipleObjects( NUMTHREADS, // number of handles in array hReadEvents, // array of read-event handles TRUE, // wait until all are signaled INFINITE); // indefinite wait switch (dwWaitResult) { // All read-event objects were signaled. case WAIT_OBJECT_0: // Write to the shared buffer. break; // An error occurred. default: printf("Wait error: %d/n", GetLastError()); ExitProcess(0); } // Set hGlobalWriteEvent to signaled. if (! SetEvent(hGlobalWriteEvent) ) { printf("SetEvent failed (%d)/n", GetLastError()); return; } // Set all read events to signaled. for(i = 0; i < NUMTHREADS; i++) if (! SetEvent(hReadEvents[i]) ) { printf("SetEvent failed (%d)/n", GetLastError()); return; } }
Before starting a read operation, each reader thread uses WaitForMultipleObjects to wait for
the application-defined global variable hGlobalWriteEvent and its own read event
to be signaled. When WaitForMultipleObjects returns, the reader thread's
auto-reset event has been reset to nonsignaled. This blocks the master thread
from writing to the buffer until the reader thread uses the SetEvent function to set the event's state back
to signaled.
VOID ThreadFunction(LPVOID lpParam) { DWORD dwWaitResult; HANDLE hEvents[2]; hEvents[0] = *(HANDLE*)lpParam; // thread's read event hEvents[1] = hGlobalWriteEvent; dwWaitResult = WaitForMultipleObjects( 2, // number of handles in array hEvents, // array of event handles TRUE, // wait till all are signaled INFINITE); // indefinite wait switch (dwWaitResult) { // Both event objects were signaled. case WAIT_OBJECT_0: // Read from the shared buffer. break; // An error occurred. default: printf("Wait error: %d/n", GetLastError()); ExitThread(0); } // Set the read event to signaled. if (! SetEvent(hEvents[0]) ) { printf("SetEvent failed (%d)/n", GetLastError()); return; } }
相关文章推荐
- ROS中gazebo工具学习(使用gazebo加载机器人模型)
- 【数据库学习】MongoDB环境搭建配置及可视化工具使用(win10)
- c语言学习-工具使用
- 通过使用Chrome的开发者工具来学习JavaScript
- 基于OpenCL的深度学习工具:AMD MLP及其使用详解
- blktrace工具学习与使用
- vss团队开发工具使用(个人学习心得)
- 安卓学习笔记--内存泄漏检测工具—LeakCanary的配置和使用
- Cordova学习笔记 angular中工具方法使用示例
- 【学习笔记】KALI基本工具使用
- Unity中使用Loom工具,Unity多线程学习
- etl工具kettle学习——下载安装使用
- java开发人员,最应该学习和熟练使用的工具类。google guava.(谷歌 瓜娃)
- 大数据学习笔记(七)-Spark的图形化工具的使用
- Ubuntu LTS 系统学习使用体会和实用工具软件汇总 6.04 8.04 10.04 12.04 14.04 16.04
- java学习日记_28:面向对象之工具类中使用静态。08.01
- 线性代数学习过程中使用的一些工具及技巧
- 正则表达式学习--RegexBuddy工具的使用
- SVN的学习之路四(客户端工具的使用)
- SVN的学习之路四(客户端工具的使用)