Wednesday, April 14, 2010

Multithreading programming in Win32

Here is the simple application that creates 100 threads and each thread executes the StartThraed() function


DWORD WINAPI StartThread(LPVOID threadID)
{
int tid = atoi((char *)threadID);
cout< return 0;
}

int main()
{
HANDLE hThread1;
DWORD dwGenericThread;
char threadID[10];

for (int x = 0; x < 10; ++x)
{
for (int y = 0; y < 10; ++y)
{
sprintf(threadID, "%d", y*10+x);
hThread1 = CreateThread(NULL,0,StartThread,&threadID,0,&dwGenericThread);
if(hThread1 == NULL)
{
DWORD dwError = GetLastError();
cout<<"SCM:Error in Creating thread"< return 0;
}
WaitForSingleObject(hThread1,INFINITE);
CloseHandle(hThread1);
}
}
}

Thursday, April 8, 2010

Singleton class, a different way

We all knew that, the below class will give us only one instance of the class.
class Singleton
{
private:
static Singleton* singleton;
private:
Singleton();
public:
static Singleton* getIntsance();
};

Just think that is there any other way to do the same. Yes, we have onother solution for it. See below

class Singleton
{
private:
static Singleton* singleton;
private:
~Singleton();
public:
static Singleton* getIntsance();
};

Destructor is in private scope now.