Friday, January 12, 2007

A small note about UI threads.

What is a UI thread ?? its a normal thread with a windows. thats all . But the danger comes when you are creating MFC window's objects like CFrameWnd inside a normal thread created using CreateThread API function.

For example..
CreateThread(0,0,ThreadProc);


 WINAPI ThreadProc()
{


    CFrameWnd * p  = new CFrameWnd()'
    p->Create(0,"booommm");
    p->ShowWindow(1); // normal show..


    while(GetMessage())
   {
         TranslalteMsg(); DispatchMsg();
   }
    return 1;
}

 When this code executes it will crash.. so use normal CreateWindow() function instead of CFrameWnd :: Create()..

If you want to use CFrameWnd use class CWinThread or AfxBeginThread() for creating threads.. Following code shows how it can be done with CWinThread class..

class myThread: public CWinThread
{


 public  :
    BOOL InitInstance()
    {
         CFrameWnd *ptr = new CFrameWnd();
         pt->Create("0","painter man");
         pt->ShowWindow(1);
     }
}

one more thing is you won't need to write a messge loop for this window. This will be autmatically done by this class in CWinThread::Run()..
Life is more easy... 



Wednesday, January 10, 2007

Threads Local Storage(TLS)

In a Multithreading environment sometimes it usually needs to store data specific to each threads, This storage system is called thread local storage. Note that this is also taken from the total process address space. So In this page i am going to show some way how this can be done in Microsoft Windows systems..
       Note that this storage space is very limited , and is varies according to the different implementing platforms (ie OS ).  In windows 2000 , Xp (and later) reserved total 1088 indexes per process. Windows 98 and ME reserves total 80 indexes per process and Windows 98 , NT reserved 64 indexes per process.

So what is this index ? it acts like an handle (not really ) .One thread have only one index at a time . Using this we can access the information specific to that thread. So if i am runnning a program in Xp i can create total 1088 indexes for each threads (Not tried  yet ) in that process. Well , How to get this index ? Microsoft provides APIs to get this indexes as well as storing data to TLS . Following are the APIs

 DWORD TlsAlloc(void);
BOOL TlsSetValue( DWORD dwTlsIndex, LPVOID lpTlsValue );
LPVOID TlsGetValue(  DWORD dwTlsIndex);
BOOL TlsFree(  DWORD dwTlsIndex);
Sample usage

DWORD id;
Main()
{

    id =  TlsAllc();
   CreateThread(Thread1);
   CreateThread(Thread2);
   TlsFree(id);
}
Thread1()
{
    p = ( <type *> )  TlsGetValue(id)
    // do as u like
}
Thread2()
{
    p = ( <type *> )  TlsGetValue(id)
    // do as u like
}
Microsoft also proves much simpler usage. That is __declspec(thread) keryword. This keyword can also be attached to a static or ex tern data members... We know  that usually static data is common to all threads. But if attach _declspec(thread) to that member it is allocated to that threads local storage space.
ex : static __declspec(thread) int tlsData;

Virtual Function and ZeroMemory

What caused me to write this post is a BUG. Really i am not joking.. Everone know that there is a virtual table pointer is present at the beginning of the object memory . i am also know that.. But i did a mistake long ago . Following lines shows it...

class Base
{
public :
   virtual void Render()
   {
    }
};


class Derived: public Base
{
    // i have several data members here..
public :
    Derived()
    {
        // For easily initializing all of my data members to zero , i used ZeroMemory here
       ZeroMemory(this,sizeof(Derived));
       // Danger .. This also clear the virtual table pointer..
    }
    void Render()
   {
   }
};
So when i called it like
Derived *d = new Derived()
d->Render();

it crasheddd .... because of the ZeroMemory in the constructor set the vptr to Zero.