Remove Ads

Share on Facebook Share on Twitter

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multithreading
#1
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.
Reply
#2
Excellent! I was just about to go "ask" google on something like this in the near future.
Reply
#3
Nice little explanation of multi-threading. I wish threading in assembly was as easy.

However, this threading won't really increase efficiency much will it? A case that would be much more efficient would be to have a few different messageboxes that were changing the text on them (for example, counting up) and having them all count at the same time. This way you can do it simultaneously instead of having a slight wait time between them each.
Reply
#4
(01-15-201110:33 PM)Hidden Dragon Wrote: Nice little explanation of multi-threading. I wish threading in assembly was as easy.

However, this threading won't really increase efficiency much will it? A case that would be much more efficient would be to have a few different messageboxes that were changing the text on them (for example, counting up) and having them all count at the same time. This way you can do it simultaneously instead of having a slight wait time between them each.

It would increase efficiency if you were, say, trying to manage multiple processess at once(several programs? An OS perhaps?)

Or am I completely wrong?
Reply
#5
Well I didn't mean threading doesn't increase efficiency. I meant that it wouldn't do much in this case.
Reply
#6
(01-15-201110:38 PM)Hidden Dragon Wrote: Well I didn't mean threading doesn't increase efficiency. I meant that it wouldn't do much in this case.

I agree with you. This was just show show a basic example and not to be taken as the end all.
Reply
#7
I totally understand. I've only seen an example of multi-threading in assembly. It looks quite difficult. The C++ equivalent is much easier.
Reply
#8
Everything is Assembly is harderCool, but maybe that's because of my 64 bit OSSad.
Reply
#9
Haha I agree but that's only when you're relatively new. Once you know what you're doing it gets a lot easier.

64 bit assembly sucks Tongue However, 32 bit assembly will run on 64. It's just 64 that doesn't run on 32.
Reply
#10
(01-16-201112:20 AM)Scorch Wrote: Everything is Assembly is harderCool, but maybe that's because of my 64 bit OSSad.

I have 64 bit too, and you can still code x86.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)