Monday, August 25, 2014

Synchronous and Asynchronous sockets.

Whenever we perform some operation on a socket, it may not be able to complete immediately and return control back to your program.
For example:
                  A read on a socket cannot complete until some data has been sent by the remote host.If there is no data waiting to be read, one of the two things can happen,
  • The function can wait until some data has been written on the socket, OR  
  •  It can return immediately with an error that indicates that there is no data to be read.
The first case is called "Synchronous or blocking socket".
The program is "blocked" until the request for the data has been satisfied. When the remote system does write some data on the socket, the read operation will complete and execution of the program will resume. 

The second case is called an "Asynchronous or non-blocking socket", and requires that the application recognize the error condition and handle the situation properly.

Wednesday, August 20, 2014

Copy Constructor and Assignment operator

The assignment operator is used to copy the values from one object to another "already existing object".

Eg:    classA objectone(5); //Calls classA constructor.
         classA objectTwo;    //Calls classA default constructor.
         objectTwo = objectone; // Calls classA assignment operator.

  • In this case,objectTwo has already created by the time assignment is executed.consequently the classA assignment operator is called.
If the object is being copied into does not exist then "copy constructor" comes into existence.

Eg:
       ClassA object1(5) ; //calls classA constructor,
       ClassA object2 = object1;//Calls classA's copy constructor.

A "Copy constructor " is a special constructor that initializes a new object from an existing object.

  • The assignment operator copies to existing objects,and the copy constructor copies to newly created objects..
  • If a new object has to be created before copying can occur,the copy constructor is used.
Where we need "Copy constructor" : 
  • When instantiating one object and initializing it with values from another object.
  • When passing an object by value.
  • when an object is returned from a function by value. 

Thursday, August 14, 2014

MFC Threads.


MFC distinguishes between two types of threads.
  • User interface(UI) Threads and 
  • Worked Threads.
The difference between the two is that UI threads have message loops and worker threads do not.UI threads can create windows and process messages sent to those windows.

Worker threads perform background tasks that receive no direct input from the user and therefore don't need windows and message loops.

Creating worker thread: 
The best way to launch a thread in an MFC Application is to call AfxBeginThread.
MFC defines two different versions of AfxBeginThread. One that starts a UI thread and another starts a worked thread.
AfxBeginThread creates a new CWinThread object launches a thread and ttachesa it to the CWinThread object and returns CWinThread pointer.

Syntax:
CWinthread * pThread = AfxBeginThread(Threadfunc,&threadInfo)

Above statement starts a worker thread and passes it to the application-defined data structure(&threadInfo) that contains input to the thread. Threadfunc  is the thread function the function that gets executed when the thread itself begin to execute.

Simple Thread function:
                                UINT Threadfunc(LPVOID pParam)
                                {
                                     UINT nIterations = (UINT)pParam;
                                     for(UINT i=0; i < nIterations ; i++)
                                        return 0;
                                 }

AfxBeginThread accepts as many as four additional parameters that specify the threads priority,stack-size,
Creation flags and security attributes.The complete function prototype is as follows.

   CWinThread * AfxBeginThread(AFX_THREADPROC pfnThreadProc,
                                                     LPVOID pParam,
                                                     int nPriority = THREAD_PRIORITY_NORMAL,
                                                     UINT nStackSize = 0,
                                                     DWORD dwCreateFlags = 0,
                                                     LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL)

  • nPriority specifies the threads execution priority.High-priority threads are scheduled for CPU time before low-priority threads. The default is THREAD_PRIORITY_NORMAL which assigns the same priority as the process that owns it.We can change thread's priority level at any time with CWinThread::SetThreadPriority.

  • The nstackSize parameter passed to AfxBeginThread specifies the threads maximum stack size. the 0 default nStackSize value allows the stack to grow as large as 1MB.
  • dwCreateFlags can be one of two values.The default value 0 tells the system to start executing the thread immediately.If CREATE_SUSPENDED is specified instead,the thread starts out in a suspended state and doesn't begin running until another thread calls CWinthread::ResumeThread on the suspended thread.

  • lpSecurityAttrs, is a pointer to a SECURITY_ATTRIBUTES structure that specifies the new threads security attributes and also tells the system whether child process should inherit the thread handle.The NULL default value assigns the new thread the same properties the thread that created it.

The Thread Function:

A thread function is a callback function,so it must be either a static class member function or a global function declared outside a class.It is prototyped this way " UINT ThreadFunc(LPVOID pParam)".

pParam is a 32-bit value whose value equals the pParam passed to AfxBeginThread.

String Table

A string table is a Windows resource that contains a list of IDs, values, and captions for all the strings of your application. For exa...