Thread: 2 loops at once?

  1. #1
    Arggggh DeepFyre's Avatar
    Join Date
    Sep 2004
    Posts
    227

    Question 2 loops at once?

    is there any way to have 2 loops running at the same time?
    thanks

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Not unless you have multiple processors, but you can fake it a number of ways.
    My best code is written with the delete key.

  3. #3
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Posts
    120
    Can you not do it with Multi-Threads in C++ (I assume so as I know that you can with C).
    "The most important thing about acting is honesty. If you can fake that you've got it made" - George Burns

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Can you not do it with Multi-Threads in C++
    Yes, that would be one of the ways to fake it.
    My best code is written with the delete key.

  5. #5
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Posts
    120
    Cool, so lookup Threads and multithreading DeepFyre. This looks good:

    http://www.powerbasic.com/support/te...threading1.asp

    dt
    "The most important thing about acting is honesty. If you can fake that you've got it made" - George Burns

  6. #6
    Arggggh DeepFyre's Avatar
    Join Date
    Sep 2004
    Posts
    227
    It doesnt tell you how to do multi threading, is it somewhere else on the site?
    Also, is multi-threading like hyper threading?

  7. #7
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Posts
    120
    I've no idea, just used Google and that looked pretty good. Does anyone else have any more light on the matter?

    dt
    "The most important thing about acting is honesty. If you can fake that you've got it made" - George Burns

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by DeepFyre
    It doesnt tell you how to do multi threading, is it somewhere else on the site?
    Also, is multi-threading like hyper threading?
    Hyper threading isnt a programming term, it's a hardware term. Multi-threading means that you have two different threads running concurrently, where the processer switches off between them very quickly.

    On windows, take a look at the CreateThread() function.

  9. #9
    Arggggh DeepFyre's Avatar
    Join Date
    Sep 2004
    Posts
    227
    Where can I find the CreateThread() function?
    I'll google it but i dont know if i will find it.

  10. #10
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268

  11. #11
    Arggggh DeepFyre's Avatar
    Join Date
    Sep 2004
    Posts
    227
    Yea ... I saw that but didnt quite undertand it
    But thanks anyway guys, I'll try to figure out the rest on my own sometime.
    Keyboard Not Found! Press any key to continue. . .

  12. #12
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>Also, is multi-threading like hyper threading?
    hyper threading is Intel's technology that, when you have several threads running side by side, makes them perform better (in a nutshell anyway).

    >>didnt quite undertand it
    Don't worry about it, CreateThread() is Windows stuff, which you probably don't know yet.

    I've heard of a function called _beginthread(), and although I've never used it and have no idea how portable it is or whatnot, it seems much more straightforward to use than CreateThread(). You might want to look that up, if you're still wanting to use multiple threads.

    On the other hand, if you want 2 loops running 'at the same time', you can often merge the two loops so that one loop somehow does the job for both, and thus you won't need them running 'at the same time'.

    Hope this helps!
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  13. #13
    Arggggh DeepFyre's Avatar
    Join Date
    Sep 2004
    Posts
    227
    Thanks Hunter, I'll look that up.
    Keyboard Not Found! Press any key to continue. . .

  14. #14
    Arggggh DeepFyre's Avatar
    Join Date
    Sep 2004
    Posts
    227
    Well i found found this:

    Code:
        _beginthreadex(void);
        do{
            
                time();
    
           }while(x != 1);
             
        _endthreadex(void);
    ...The problem is that I get these errors:
    syntax error before `)' token (on that first line)
    syntax error before `)' token (on that last line)

    I dont understand what the problem is and these are the only two errors i get.


    (Am I doing it right?)
    Keyboard Not Found! Press any key to continue. . .

  15. #15
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    No, beginthread() and beginthreadex() work by passing a function pointer to the function.

    The function documentation can be found here:
    http://msdn.microsoft.com/library/de...inthreadex.asp

    Judging from the argument, the definition of the function you need should look like this:
    Code:
    #include <process.h>
     
    void myfunction(void* parameter)
    {
       int param = *(int*)parameter; //or whatever type of parameter you wanted to pass
       //put your loop in here
    }
     
    int main()
    {
       int parameterToPass = 10;
       _beginthread(myfunction, 0, (void*)&parameterToPass);
       //Now your thread is running the loop
     
       //Do whatever you want to do in main() while the loop is running
     
       return 0;
    }
    Note that your program will continue to run as long as the thread is still running; _endthread() does not terminate the thread from the main(), but can be used within the thread function (myfunction()) to quit the thread, although it gets called automatically anyways when the function returns. So you need to find some way of ensuring that the thread ends when the program does.

    You also have to make sure that the thread doesn't access any data that main() accesses, to prevent memory corruption. That's what the windows synchronization objects are for (mutex, critical section, etc.).
    Last edited by Hunter2; 09-08-2004 at 10:24 AM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loops Trouble
    By rlframpton in forum C Programming
    Replies: 2
    Last Post: 04-17-2009, 01:08 AM
  2. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 01:18 AM
  3. recoursion or loops?
    By Mecnels in forum C++ Programming
    Replies: 2
    Last Post: 01-14-2002, 12:09 PM
  4. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM
  5. for loops - newbie q's
    By Narciss in forum C Programming
    Replies: 8
    Last Post: 09-26-2001, 02:44 AM