Post Reply 
Social Buttons
 
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multithreading
Author Message
Scorch Offline
Intermediate C++
********

Trade Count: (0)
Posts: 83
Joined: Jan 2011
Reputation: 15
Post: #1
Multithreading
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;
}
I'm not a great teacher, so if you have any good descriptions then let me know to better explain this to newcomers.
01-15-2011 10:25 PM
Find all posts by this user Quote this message in a reply
Ironside Offline
Site Owner
**********

Trade Count: (0)
Posts: 170
Joined: Nov 2010
Reputation: 7
Post: #2
RE: Multithreading
Excellent! I was just about to go "ask" google on something like this in the near future.
01-15-2011 10:26 PM
Find all posts by this user Quote this message in a reply
Hidden Dragon Offline
Amazing
*******

Trade Count: (0)
Posts: 63
Joined: Jan 2011
Reputation: 4
Post: #3
RE: Multithreading
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.
01-15-2011 10:33 PM
Find all posts by this user Quote this message in a reply
Ethereal Offline
Moderator
*******

Trade Count: (0)
Posts: 39
Joined: Jan 2011
Reputation: 5
Post: #4
RE: Multithreading
(01-15-2011 10: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?
01-15-2011 10:35 PM
Find all posts by this user Quote this message in a reply
Hidden Dragon Offline
Amazing
*******

Trade Count: (0)
Posts: 63
Joined: Jan 2011
Reputation: 4
Post: #5
RE: Multithreading
Well I didn't mean threading doesn't increase efficiency. I meant that it wouldn't do much in this case.
01-15-2011 10:38 PM
Find all posts by this user Quote this message in a reply
Scorch Offline
Intermediate C++
********

Trade Count: (0)
Posts: 83
Joined: Jan 2011
Reputation: 15
Post: #6
RE: Multithreading
(01-15-2011 10: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.
01-15-2011 10:43 PM
Find all posts by this user Quote this message in a reply
Hidden Dragon Offline
Amazing
*******

Trade Count: (0)
Posts: 63
Joined: Jan 2011
Reputation: 4
Post: #7
RE: Multithreading
I totally understand. I've only seen an example of multi-threading in assembly. It looks quite difficult. The C++ equivalent is much easier.
01-16-2011 12:17 AM
Find all posts by this user Quote this message in a reply
Scorch Offline
Intermediate C++
********

Trade Count: (0)
Posts: 83
Joined: Jan 2011
Reputation: 15
Post: #8
RE: Multithreading
Everything is Assembly is harderCool, but maybe that's because of my 64 bit OSSad.
01-16-2011 12:20 AM
Find all posts by this user Quote this message in a reply
Hidden Dragon Offline
Amazing
*******

Trade Count: (0)
Posts: 63
Joined: Jan 2011
Reputation: 4
Post: #9
RE: Multithreading
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.
01-16-2011 12:26 AM
Find all posts by this user Quote this message in a reply
Ethereal Offline
Moderator
*******

Trade Count: (0)
Posts: 39
Joined: Jan 2011
Reputation: 5
Post: #10
RE: Multithreading
(01-16-2011 12: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.
01-16-2011 10:14 AM
Find all posts by this user Quote this message in a reply
Post Reply 


Forum Jump:


User(s) browsing this thread: