Microsoft Visual C++ .NET Professional Projects - Premier Press
.pdf150Part I INTRODUCING VC++.NET
MutexName. Specifies the name of the mutex you want to use.
Security. Specifies the security attributes for the mutex object.
To release the mutex object, you need to call the CSyncObject::Unlock function. On releasing the mutex object, another thread can use the same object to access the resource.
CCriticalSection |
Y |
|
L |
CCriticalSection is another CSyncObject derived class that you can use to syn- |
|
chronize threads by creating critical sections. A critical section is a synchronization |
|
object that enables only one thread to gain exclusive access to a resource. The crit- |
|
ical sections are preferred over mutexes when speed is critical in an application and |
|
A |
|
the resources are not used across processes.FNow, if you consider the same airline |
|
E |
|
reservation example, you might recommend a faster method of access to data in |
|
T |
|
an application. Because speed isMa critical factor in such an application, it would be ideal if you were to create critical sections in the application instead of mutexes and also avoid any invalid information by gaining exclusive access to data.
CSemaphore
In addition to the CMutex and CCriticalSection classes, CSemaphore is yet
another CSyncObject derived class. Converse to the earlier two classes discussed, CSemaphore allows multiple threads to access a resource. This class enables you to create synchronized threads by creating semaphores. A semaphore is a synchronization object that enables you to limit the number of threads that can gain access to a shared resource.
The number of threads that can access a resource is limited by specifying the number of users at the time of the creation of the CSemaphore object. The syntax for the CSemaphore constructor is as follows:
CSemaphore(
LONG InitialNo,
LONG MaxNo,
LPCTSTR SemaphoreName,
LPSECURITY_ATTRIBUTES Security
);
Following is a description of the parameters of the CSemaphore constructor:
Team-Fly®
THREADS |
Chapter 6 |
|
151 |
|
|||
|
|
|
|
InitialNo. Specifies the initial number of threads that can access a resource. It must be greater than zero and less that the maximum number you specify.
MaxNo. Specifies the maximum number of threads that can access a resource.
SemaphoreName. Specifies the name of the semaphore.
Security. Specifies the security attributes for the semaphore.
TIP
The number of users accessing a resource is inversely proportional to the speed of that application. The lesser the number of users accessing a resource, the faster is the application’s response.
Revisiting the earlier example of the airline reservation application, it is recommended that you use a CSemaphore object to handle the task of reading and displaying the flight status, because the CSemaphore object enables you to create a thread to read the data while another thread can simultaneously display the data, thus speeding up the entire process.
Taking further the example of the airline reservation application, it is advisable to use a combination of the aforementioned objects to handle different tasks in this application. For instance, you can use the CSemaphore object to access data that is not being updated, whereas, while updating data, you can use either CMutex or CSemaphore objects as per the requirements of the application.
CEvent
CEvent is a CSyncObject derived class that enables event handling in synchronized threads by creating events. An event is a synchronized object that enables a thread to notify an event to another thread. Consider a spreadsheet application in which you constantly change data. You have created a multithreaded application that executes threads in the background of the application. For example, you have created threads that recalculate data, draw graphs based on the data, and save the updated spreadsheet automatically. Because these threads are executing in the background of the application, they do not have any user interaction. Then how
152 |
Part I |
INTRODUCING VC++.NET |
|
|
|
can you ensure that the threads are informed about any changes in the application? Well, the CEvent object comes to your rescue. When you create a CEvent type of object in a class, you can use it to notify the required threads about the occurrence of an event, such as data being updated, and the threads perform their respective tasks.
While the synchronization classes aid you in synchronizing threads, the synchronization access objects of MFC control access to the resources in a multithreaded application. The types of synchronization access objects include CMultiLock and CSingleLock. These objects are discussed in detail next.
CMultiLock
To access a resource in a synchronized multithreaded application, you need to lock the resource. To lock single or multiple resources for a thread, you can use an object of the CMultiLock class. Unlike the classes discussed earlier, the CMultiLock class does not have a base class.
To use a CMultiLock object, you need to create an array of the synchronized objects that you need to use in a thread. After creating an object of the CMultiLock type, you need to call the Lock function for each member of the array. To release the lock, you need to call the Unlock function. Both, Lock and Unlock are the member functions of the CSyncObject class.
CSingleLock
The CSingleLock class is similar to the CMultiLock class in all respects except that you use a CSingleLock object in a thread that needs to lock only one resource. You do not require any array of synchronized objects to implement CSingleLock.
TIP
The IsLocked member function of the CMultiLock and CSingleLock classes enables you to determine the availability of a resource.
THREADS |
Chapter 6 |
|
153 |
|
|||
|
|
|
|
Summary
You’ve just gone through yet another important concept of MFC: threads. Threads enable optimum utilization of resources and handle different tasks efficiently in an application. This chapter also discussed the types of threads and the various classes that can be used to synchronize threads. Using this knowledge judiciously, you can create highly efficient applications.
This page intentionally left blank
PART IIProfessional
Project – 1
This page intentionally left blank
Project 1
Database
Programming
Using VC++.NET
Project 1 Overview
This project illustrates the use of MFC in interacting with a back end, such as
SQL Server and MS Access. In this project, you will create a banking application that uses SQL Server 2000 as its back end. This will be a Windows application, designed using the MFC ODBC classes, that enables back-end interaction. The features of this application are as follows:
A Logon form that pops up when the application is executed
A main form that allows the user to handle all day-to-day banking transactions, such as creating a customer account and handling deposits, withdrawals, and transfers
The concepts covered are as follows:
Creating a database and tables in SQL Server 2000
Creating an SDI application based on MFC ODBC classes, such as CRecordSet and CRecordView classes
Chapter 7
Database
Management
Using ODBC –
An Overview