01-15-201110:25 PM
Here's a rather basic example of multithreading.
My basic description of multithreading is using your proccessor as efficiently as possible.
This is a very simple example of multithreading
[code]
//Multithreading by Scorch
#include <windows.h>
//These 3 functions are going to run simultaneously on 3 different threads
int ThreadOne()
{
MessageBox(NULL, "Message from Thread One", "Thread One", MB_OK);
return 0;
}
int ThreadTwo()
{
MessageBox(NULL, "Message from Thread Two", "Thread Two", MB_OK);
return 0;
}
int ThreadThree()
{
MessageBox(NULL, "Message from Thread Three", "Thread Two", MB_OK);
return 0;
}
int main()
{
HANDLE threads[2];//3 handles for thread creation
DWORD wait = 0;
DWORD par[2];//For parameters in the CreateThread() line
MessageBox(NULL, "This will appear before thread creation", "Intro", MB_OK);
threads[0] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadOne, NULL, 0, &par[0]);//Creation of the Threads
threads[1] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadTwo, NULL, 0, &par[1]);
threads[2] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadThree, NULL, 0, &par[2]);
wait = WaitForMultipleObjects(3, threads, TRUE, INFINITE);
MessageBox(NULL, "This will appear after thread creation", "End", MB_OK);//Threads have now ended
return 0;
}
[/code]
I'm not a great teacher, so if you have any good descriptions then let me know to better explain this to newcomers.
My basic description of multithreading is using your proccessor as efficiently as possible.
This is a very simple example of multithreading
[code]
//Multithreading by Scorch
#include <windows.h>
//These 3 functions are going to run simultaneously on 3 different threads
int ThreadOne()
{
MessageBox(NULL, "Message from Thread One", "Thread One", MB_OK);
return 0;
}
int ThreadTwo()
{
MessageBox(NULL, "Message from Thread Two", "Thread Two", MB_OK);
return 0;
}
int ThreadThree()
{
MessageBox(NULL, "Message from Thread Three", "Thread Two", MB_OK);
return 0;
}
int main()
{
HANDLE threads[2];//3 handles for thread creation
DWORD wait = 0;
DWORD par[2];//For parameters in the CreateThread() line
MessageBox(NULL, "This will appear before thread creation", "Intro", MB_OK);
threads[0] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadOne, NULL, 0, &par[0]);//Creation of the Threads
threads[1] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadTwo, NULL, 0, &par[1]);
threads[2] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadThree, NULL, 0, &par[2]);
wait = WaitForMultipleObjects(3, threads, TRUE, INFINITE);
MessageBox(NULL, "This will appear after thread creation", "End", MB_OK);//Threads have now ended
return 0;
}
[/code]
I'm not a great teacher, so if you have any good descriptions then let me know to better explain this to newcomers.