How cool is this blog?

Did you know that you can drag and drop the boxes in the left or right and order them as you like?

Just move your mouse over the grey box until the cursor changes, and then drag the box to the new position as you like.

Give it a try and post a comment if you like it!

Twitter Button from twitbuttons.com

Blogroll

BlogCatalog



This is just a simple console application written in c++ that shows how to use multiple threads. Remember, it's just a SIMPLE application, don't expect too much from it. Use it as a demo or starting point.

[code]

#include <windows.h> //for thread
#include <stdio.h> // for print/read to/from console

typedef struct ThreadData {
int threadId;
} ThreadData, *PTHREADDATA;

void ListenForThreadEvent( LPVOID lpParam );

int main()
{
const int MAX_THREADS = 5;
HANDLE hThreadArray[MAX_THREADS];
PTHREADDATA pDataArray[MAX_THREADS];
DWORD dwThreadIdArray[MAX_THREADS];

for(int i=0; i<MAX_THREADS; i++)
{
pDataArray[i] = (PTHREADDATA) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
sizeof(ThreadData));
if( pDataArray[i] == NULL )
{
ExitProcess(2);
}
pDataArray[i]->threadId = i;

hThreadArray[i] = CreateThread(
NULL, // default security attributes
0, // use default stack size
(LPTHREAD_START_ROUTINE) ListenForThreadEvent, // thread function name
pDataArray[i], // argument to thread function
0, // use default creation flags
&dwThreadIdArray[i]); // returns the thread identifier

if( hThreadArray[i] == NULL )
{
printf("CreateThread error: %d\n", GetLastError());
return 0;
}
}

WaitForMultipleObjects(MAX_THREADS, hThreadArray, TRUE, INFINITE);

for(int i=0; i<MAX_THREADS; i++)
{
CloseHandle(hThreadArray[i]);
if(pDataArray[i] != NULL)
{
HeapFree(GetProcessHeap(), 0, pDataArray[i]);
pDataArray[i] = NULL; // Ensure address is not reused.
}
}

// repeat until keypressed
char wait[5];
scanf(wait);

return 0;
}

void ListenForThreadEvent( LPVOID lpParam )
{
PTHREADDATA pDataArray;
pDataArray = (PTHREADDATA)lpParam;
int i = 0;
while(i < 10)
{
printf("thread %d step %d\n", pDataArray->threadId, i++);
}
}

[end code]

The result of running this application is:


NOTES:

  • If you start a new C++ empty project, you will probably have debugging disabled. To set it up and running, follow these steps:
    1) Goto Project->YourProjectName Properties (or right click on the project name in Solution Explorer and choose Properties)
    2) On the left expand "Configuration Properties"
    3) Expand "C/C++"
    4) On the left, Select "General"
    5) On the right, change "Debug Information Format" to "Program Database For Edit And Continue (/ZI)"
    5) On the left, Select "Optimization"
    6) On the right, change "Optimization" to "Disabled (/Od)"
    7) On the left, expand "Linker"
    8) On the left, select "Debugging"
    9) On the right, change "Generate Debug Info" to "Yes"
    10) Click ok
    11) Set your breakpoints
    12) Rebuild your application

  • This simple example is taken from a more documented but more complicated demo provided by Microsoft. I don't know why they need to usually complicate everything, but YOU MUST remember: Keep It Simple, Stupid! Anyway, the demo can be found here.

0 comments

Post a Comment



Subscribe to: Post Comments (Atom)

Feedjit

Bloglinker list