Word from the Author

"One mans mistake is another mans break"
Most of the article(s) which I have posted here are based on my own personal experience. Please do not get aggravated if you disagree with what I wrote.This is just my opinion and it might not be worth much. In case you find some errors or mistakes , have any other useful information which others including myself would benefit from or if you like an article you can always post your messages and comments here.

Using Macros to decrease the amount of code written in C++

There are always pros and cons of using macros in your project.The advantage of using a macro is that it reduces the amount of coding you need to do , and the disadvantages of using a macro other than the fact that the size of the executable code is increased its also difficult to debug.

I personally try to stay away from macros but hey , you should always be familiar with things you never know when you might come across them , just like i did at work  :(

#define IMPLEMENT_SET_GET_METHOD( type , name ) \
IMPLEMENT_SET_METHOD( type , name );         \
IMPLEMENT_GET_METHOD( type , name );

#define IMPLEMENT_SET_METHOD( type , name )          \
private:                type name;         \
public:                 void set_##name( const type& value ) {  name = value; }



#define IMPLEMENT_GET_METHOD( type , name )         \
public:          const type& get_##name() const             { return name; }


class myclass
{
IMPLEMENT_SET_GET_METHOD( int  , val)
}  ;

void main()
{
 myclass obj;
 obj.set_val(34);
 cout << obj.get_val();
 cin.get();
}




Interthread Communication using PostThreadMessage in C++

In order to communicate between various running thread we could use the  PostThreadMessage

HANDLE Threadhandle;
DWORD  Threadid;

#define UWM_SEND_MY_STRING (WM_APP + 3) 

DWORD WINAPI Threadproc(LPVOID lpParameter )
{
 MSG msg;
 char *p = new char;

 while(GetMessage( &msg, NULL, 0, 0 ))
 {
 p = (char*)(msg.wParam);
 cout << p;
 }
 return 0;  //Exit the thread
}

int main()
{
MSG msg;
Threadhandle = CreateThread(NULL,0,Threadproc,NULL,0,&Threadid);
char *p=new char;
p="Send Message";       
Sleep( 1000 );
while
(TRUE)
 {
  if(PostThreadMessage(Threadid,UWM_SEND_MY_STRING,(WPARAM)p,0)==0)
    {
    printf("failure");
    }
   Sleep(1000);
 }
}
 



Windows Threading Techniques in C++


Threading can be created by the following 3 methods 
1- _beginthread
2- _beginthreadex
3- createthread

The following examples  were written using Borland C++ 5.02

_beginthread(Function Name , Stack Size , Parameter To Pass)

#include <process.h>

void ThreadProc(void *param);
int main()
{
  int n, i;
  int val = 0;
  HANDLE handle;
  printf("Enter the number of threads : ");
  scanf("%d",&n);

  for(i=0;i<n;i++)
  {
  val = i+1;
  handle = (HANDLE) _beginthread( ThreadProc,0,&val);
  WaitForSingleObject(handle,INFINITE);
  }
  cin.get();
}

void ThreadProc(void *param)
{
  int h=*((int*)param);
  printf("%d Thread is Running!\n",h);
  _endthread();
} 
 

Capturing the Error Code of A Thread GetExitCodeThread
#include <windows.h>
#include <stdio.h>
#include <process.h>

void Thread1(void *p);

int main()
{
    HANDLE  Thread;
    LPDWORD lpExitCode; 
    int code;
    Thread = (HANDLE) _beginthread(Thread1,0,0);
    Sleep(4000);
    GetExitCodeThread(Thread,lpExitCode);
    cout <<  "Start---"<<  lpExitCode <<"----End" ;
    code = int(*lpExitCode);
   
     if (code == 259)
     {
     cout << "Thread has endded";
     }
    cout << "This should show up" ;
    CloseHandle(Thread);
}


void Thread1(void *p)
{
      int i = 0;
      while(1)
      {
               printf("%u ", i);
               i = rand();
               Sleep(500);
      }
      cout << "Finished" << "\n";
}

C++ Linker Error : One or more Multiply defined symbols found

One of the most frustrating messages which you can get while building your project is a linker error.When I first came across this error , I was told that a bit of code re factoring in my project was required . Later on I realized that this error was being produced due to my unfamiliarity to the C++ Build Process. I have provided a little overview on how the build process works and then a very short example demonstrating how we could tackle such a problem. The solution would be worth the read for both firmware and software developers in the long run. Why ? I'll get to that later , but for now lets get started with the Build Process

Understanding C++ Build Process:
Although the C++ Build Process is fairly complex . Incase you are still interested have a look here . However the following diagram shows the major steps which the IDE takes here in order to build our application from meager .cpp files to a fully functional .exe/dll file .


Creating A Simple Linker Error Scenario :
The following diagram displays a very simple scenario and also includes a method on overcoming the linker error.
 















Summary:
Only .cpp files are preprocessed and then converted to individual .obj files.Hence In order to solve the problem of “Multiply defined files”. The functions in header files are only prototyped , and the definition of those functions are placed in individual .cpp files. The compiler while preprocessing the CPP file inserts the entire header into that space of the cpp file and since multiple prototype declarations is possible hence the link time error is resolved and since all the functions from the header files are in a single cpp they get registered only once..so no link time errors.