Doing 2 things at the same time

This is a discussion on Doing 2 things at the same time within the C Programming forums, part of the General Programming Boards category; How can I make a program do two things at the same time? For example, the program would be generating ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    182

    Doing 2 things at the same time

    How can I make a program do two things at the same time?

    For example, the program would be generating prime numbers and storing them into dynamically allocated memory, but at the same time it would be printing on the console a kind of progress indicator like: | / -- \ | / -- (you get the idea, the rotating sticks thingy) independently from the generating prime numbers process.

    Thanks!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I suppose the technically correct method would be threads. Note: very hard.

    In this case, I would write my prime generating program, and at the bottom of the loop I'd print out another character in my rotating sticks thing, rather than go to all that trouble.

  3. #3
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    One "easy" way to accomplish that is by using threads. One thread is doing the calculation, and the other thread is showing the progress (let's say on stderr -- that's how I do it).

    And you should forget about the "rotating sticks thingy", it doesn't give much information and is a waste of time to program. Print every X seconds the value of the current number being test for primality, for example.

    Edit: argh -- beaten by tabstop
    I hate real numbers.

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Use threads. If you are using Linux, use pthreads.

    Generally you ll create two threads. One will do one thing, the other the other. That's the most correct way.

    But you could just:
    do_something
    print \
    do_something
    print |
    do_something
    print /
    do_something
    print /
    do_something
    etc etc

    EDIT: argh, beaten by both (my g/f talks to much)

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    tabstop:
    The problem is that with the algorithm that I am using, the bigger the number being tested for primality, the slower the loop. So, it would get slower and slower.

    That's what I meant hehe, two threads (I kinda forgot how it was called). But, I have no idea of how to do them. Could you guys point me to the right direction of how to accomplish this?

    Thanks!

    P.S.: Not using linux, I'm in WinXP, using MinGW w/ MSYS

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,788
    beginthread.
    As long as the threads do not touch the same data (that includes two threads not outputting to screen, as well), it will be a cinch.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    You have some choices. You could use Pthread but I really don't know if there's a implementation of it on Windows (that makes me wonder, why ?). Oh, well, I just found one.

    Or, there's is the threading functions your operating system has to offer, like CreateThread(). You can take a look at the documentation on MSDN website. And if you wanted a pragmatic example (since it can be long to go through all the stuff), well, you'll have to wait for someone to post an example.

    Personally, I'd go for Pthread since it's more portable and it may be easier to use.
    I hate real numbers.

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    Elysia:
    The function that beginthread will call can only have one argument?

    foxman:
    I'll leave pthread for later, cause I really don't know how to use dlls or how to link to libraries. So I'll check that later.

    Thanks!

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,788
    No, it takes 3 arguments, but you only need to concern yourself with the first.
    The second and third can be 0 and NULL respectively.
    First argument is a function where the execution of the thread will begin.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by foxman View Post
    You have some choices. You could use Pthread but I really don't know if there's a implementation of it on Windows (that makes me wonder, why ?)
    Because on windows, CreateThread() is so much easier.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    Yeah, but I was talking about the function that beginthread will call. Can't that function have more than 1 argument?

    OK, let me think of an example:

    Main starts, calls beginthread which calls a function ( lets call it "progress") that will print out the "/ -- \ | /..." So, that function will keep on until the function "progress" calls _endthread. Meanwhile the "genereate_primes" function is called by main, which will generate primes and store them to dynamically allocated memory. And when it finishes, we somehow tell "progress" that "genereate_primes" finished and then we print the numbers to the console.

    Question: How would function "progress" know when "generate_primes" finished so that "progress" can call _endthread?

  12. #12
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Well, progress can read a global variable every loop to see if it stops or not.
    If for example endProgram == 0 it would stop. Ofc, generate_primes will set endProgram = 0 when it finishes and endProgram would be set to 1 at start.

    Another way is using SIGNALS. Don't know really how to on windows. And it is kind of a mess with pthreads on linux sometimes. But it is most correct way lets say.

    So go for the global variable!

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,788
    Better make that global variable volatile.
    Ie
    Code:
    volatile BOOL g_bFinished;
    This is the easiest way. Another is using events in Windows, but it's somewhat trickier and platform dependant.
    Also, avoid _endthread. Use a global variable instead to tell the other thread that you're shutting down.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    let me do the global variable. I'll post the code when I finish.

    Thinking of making a semiprimes generating program. I made a simple to use function a long time ago so it would be easy to make a program with it.

  15. #15
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,676

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers
    By Big_0_72 in forum C Programming
    Replies: 3
    Last Post: 10-28-2008, 07:51 PM
  2. Determine the closest departure time
    By Kyeong in forum C Programming
    Replies: 9
    Last Post: 10-07-2008, 08:06 PM
  3. Execution Time - Rijandael encryption
    By gamer4life687 in forum C++ Programming
    Replies: 5
    Last Post: 09-20-2008, 09:25 PM
  4. Pthreads performance
    By C_ntua in forum C Programming
    Replies: 42
    Last Post: 06-17-2008, 11:29 AM
  5. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 09:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21