Threads

I'm looking for a good resource for learning to do simple threads in C++. Anyone got any good sources?

THanks,
Tony. :)


/*

Those of you who believe in
psychokenesis, raise my hand.

*/
«1

Comments

  • : I'm looking for a good resource for learning to do simple threads in C++. Anyone got any good sources?
    :
    : THanks,
    : Tony. :)
    :

    [blue]There isn't much to it. If you are using a Windows 32-bit compiler, such as VC++ 6.0 or Dev-C++ just call CreateThread(). Here is a simple example[/blue]

    [code]
    #include


    DWORD WINAPI ThreadProc(LPVOID lpParameter)
    {
    // do some stuff not shown


    return 0;
    }

    int main()
    {
    DWORD dwThreadID;
    HANDLE hThread = CreateThread(NULL,0,ThreadProc,0,0,&dwThreadID);
    return 0;
    }

    [/code]

  • What if u wanted to do threads in plain old C, would it be the same thing?


    : : I'm looking for a good resource for learning to do simple threads in C++. Anyone got any good sources?
    : :
    : : THanks,
    : : Tony. :)
    : :
    :
    : [blue]There isn't much to it. If you are using a Windows 32-bit compiler, such as VC++ 6.0 or Dev-C++ just call CreateThread(). Here is a simple example[/blue]
    :
    : [code]
    : #include
    :
    :
    : DWORD WINAPI ThreadProc(LPVOID lpParameter)
    : {
    : // do some stuff not shown
    :
    :
    : return 0;
    : }
    :
    : int main()
    : {
    : DWORD dwThreadID;
    : HANDLE hThread = CreateThread(NULL,0,ThreadProc,0,0,&dwThreadID);
    : return 0;
    : }
    :
    : [/code]
    :
    :

  • : What if u wanted to do threads in plain old C, would it be the same thing?
    :
    :

    yes, nothing different.
  • Cool! :) Thanks. :)

    We did a bit on threads in Java, and, well...

    How do I handle the posibility of deadlocks?
    Multiple threads trying to access the same shared memory?

    Also, is it unreasonable to have [b]thousands[/b] of threads in a program?
    The reason I'd like to use thousands of threads is because I'm about to start a project in college - I've to program a Virtual Ant Colony. I plan on having a few thousand ants in it, and giving each ant its own thread of execution.

    Is this a reasonable strategy? Is there a better way of doing it?

    Tony.

    /*

    Those of you who believe in
    psychokenesis, raise my hand.

    */

  • I don't know much about threads but logically I don't think u need to do that, ants are grouped by type, maybe u should just have a separate thread for each different type of ant because all worker ants behave the same way


    : Cool! :) Thanks. :)
    :
    : We did a bit on threads in Java, and, well...
    :
    : How do I handle the posibility of deadlocks?
    : Multiple threads trying to access the same shared memory?
    :
    : Also, is it unreasonable to have [b]thousands[/b] of threads in a program?
    : The reason I'd like to use thousands of threads is because I'm about to start a project in college - I've to program a Virtual Ant Colony. I plan on having a few thousand ants in it, and giving each ant its own thread of execution.
    :
    : Is this a reasonable strategy? Is there a better way of doing it?
    :
    : Tony.
    :
    : /*
    :
    : Those of you who believe in
    : psychokenesis, raise my hand.
    :
    : */
    :
    :

  • Oooh... Good point!

    My reasoning behind each ant having its own thread was that each is its own individual.

    How would I go about getting the different threads to look after all of its own ant type?


    /*

    Those of you who believe in
    psychokenesis, raise my hand.

    */

  • theoritically yes, you could create thousands of threads, but you would be insane to do so because there just isn't enough computer time to schedule them all in a reasonable amount of time. DarkLoard2003 has a much better idea.
  • Object Oriented Design is the answer to that question. Each thread would be for a [blue]class[/blue] of ant, so for each thread define a class that is specific to ants of that type. Then just create as many instances (ant objects) of the class that you want and each instances will be a different ant. Also all ant-types class should be inherited for a base Ant class.


    : theoritically yes, you could create thousands of threads, but you would be insane to do so because there just isn't enough computer time to schedule them all in a reasonable amount of time. DarkLoard2003 has a much better idea.
    :

  • What about arrays? Create a class or struct that defines an ant and then create an array of that class or struct then run through the array in a for loop and do things for each ant as you get to it. You could give each ant it's own set of properties which affect it's behaviour etc.

    Or do you need to use multiple threads?
  • : What about arrays? Create a class or struct that defines an ant and then create an array of that class or struct then run through the array in a for loop and do things for each ant as you get to it. You could give each ant it's own set of properties which affect it's behaviour etc.
    :
    : Or do you need to use multiple threads?
    :
    What tools are you using? There was an example multi threaded program with Visual Studio 6 (maybe even 5) that created a bunch of bouncing balls on the screen. You might want to look into how that was done. (Sorry I can't remember the details and don't have VS with me at the moment)
  • : : What about arrays? Create a class or struct that defines an ant and then create an array of that class or struct then run through the array in a for loop and do things for each ant as you get to it. You could give each ant it's own set of properties which affect it's behaviour etc.
    : :
    : : Or do you need to use multiple threads?
    : :
    : What tools are you using? There was an example multi threaded program with Visual Studio 6 (maybe even 5) that created a bunch of bouncing balls on the screen. You might want to look into how that was done. (Sorry I can't remember the details and don't have VS with me at the moment)
    :

    It's called "bounce" :)


    [italic][blue]To understand recursive, first you need to understand recursive[/blue][/italic]

  • : : I'm looking for a good resource for learning to do simple threads in C++. Anyone got any good sources?
    : :
    : : THanks,
    : : Tony. :)
    : :
    :
    : [blue]There isn't much to it. If you are using a Windows 32-bit compiler, such as VC++ 6.0 or Dev-C++ just call CreateThread(). Here is a simple example[/blue]
    :
    : [code]
    : #include
    :
    :
    : DWORD WINAPI ThreadProc(LPVOID lpParameter)
    : {
    : // do some stuff not shown
    :
    :
    : return 0;
    : }
    :
    : int main()
    : {
    : DWORD dwThreadID;
    : HANDLE hThread = CreateThread(NULL,0,ThreadProc,0,0,&dwThreadID);
    : return 0;
    : }
    :
    : [/code]
    :
    :

    Just thought I'd mention that if the thread uses any functions from the C run-time libraries, you should not use CreateThread(), but instead use beginthread() so you don't get any memory leaks when ExitThread() is called.

    [code]
    _beginthread(ThreadProc, 0,&dwThreadID);
    [/code]



    [italic][blue]To understand recursive, first you need to understand recursive[/blue][/italic]

  • Ok... If I understand you right, then that's pretty much the idea I had in mind, tho I may have given the wrong impression...

    Do you mean:

    Create a Thread class,
    Create a Worker_Ant class that inherits the Thread class,
    Create tons of instances of Worker_Ant,
    start them all threading.

    ?


    : Object Oriented Design is the answer to that question. Each thread would be for a [blue]class[/blue] of ant, so for each thread define a class that is specific to ants of that type. Then just create as many instances (ant objects) of the class that you want and each instances will be a different ant. Also all ant-types class should be inherited for a base Ant class.

    /*

    Those of you who believe in
    psychokenesis, raise my hand.

    */

  • [blue]What the heck is that? I don't know all that winApi stuff. Can you do it in PLAIN OLD SIMPLE C++?

    [code]
    "oh it's easy; you just insert the rod, twist it clockwise, pull it twice, untwist it, twist it again, press the button, pull the rod out a little, twist it again, pull the lever, push the rod back in, give it a few good twists in the opposite direction, press the button while pushing the lever back up while giving the rod a few good shoves, bend the rod, twist it back into it's original position, press the button 3 more times, pull the lever again, and then repeat the process 5 more times and there ya go; it's as simple as that"
    [/code]

    [red]what is a HANDLE, and what are all those parameters for? that doesn't tell me a thing!

    [green]Or IS it possible to do threads in C++ without all that winAPI / MFC?

    Also, I heard mention of inheritance; can C++ do inheritance? what kinds, and how? does that also use all kinds of winAPI, or is that how you winAPI people program everything?

    [orange](I was just playing around with those tags I saw everywhere; either this should be all colorful and stuff, or have a bunch of stupid tags everywhere)

    : [blue]There isn't much to it. If you are using a Windows 32-bit compiler, such as VC++ 6.0 or Dev-C++ just call CreateThread(). Here is a simple example[/blue]
    :
    : [code]
    : #include
    :
    :
    : DWORD WINAPI ThreadProc(LPVOID lpParameter)
    : {
    : // do some stuff not shown
    :
    :
    : return 0;
    : }
    :
    : int main()
    : {
    : DWORD dwThreadID;
    : HANDLE hThread = CreateThread(NULL,0,ThreadProc,0,0,&dwThreadID);
    : return 0;
    : }
    :
    : [/code]

  • If you're lookin for something simple, then don't go here...
    http://www.redbrick.dcu.ie/~quaoar/programming/threads/

    I found a tutorial a while back and stuck it on my site. It's a little too complicated for me tho. Maybe you'll find it ok tho, who knows? :)

    /*

    Those of you who believe in
    psychokenesis, raise my hand.

    */

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories