Thread: problem with function

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    42

    problem with function

    hi all, now i face with big problem. i write a program that run two functions at the same time. in my program i have one clock function which i want the clock function still run. and i still can work in my program at the same time. but i have no idea how to do that. please if anyone have a solution. thanks in advanced.

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Can post the code?
    You could use fork() or thread?

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    42
    it long code. difficult to show you. but i can explain to you more easy. example:
    i want to calculate sth. the user input number and get the result and repeat it again and again, but at the same time, i want my clock function running too.
    Last edited by hugoguan; 12-16-2010 at 04:13 AM.

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Ok what is clock() function?is it clock() function from time.h?
    What's that clock function going to do while you're doing getting input,repeating.etc?

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    42
    my clock function is the clock, which it start when the program start.
    00:00:00 start till program end.

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    You are not answering what I'm asking!
    Is it the clock() function from standard libary time.h?
    And what's the purpose the that clock function? If you just want to know how long your computation takes, it's not necessary to run them in parallel.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    42
    sorry for misunderstand your question. that clock function i create by myself. clock.h.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    To run 2 pieces of code simultaneously, you have to, as Bayint Naung suggested, use something like fork() or threads. This is a considerable undertaking, so google for some tutorials.

    But, if you simply want to know how long your program was running, you do something like:
    Code:
    int main(void)
    {
        time_t  start, end;
        start = time(NULL);
        // rest of program
        end = time(NULL);
        printf("Execution took %ld seconds\n", end - start);
        return 0;
    }

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Sounds like you could use the alarm() function. You could have it raise SIGALRM every second which could trigger your clock() function.
    If you understand what you're doing, you're not learning anything.

  10. #10
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    If you're talking about waiting for user-keyboard input while at the same time displaying a clock that's updating every second, then you have to start using non standard I/O functions such as kbhit(). This returns an indication of whether a key has been hit without waiting. You need to put that in a loop along with your clock displayer. Basically you are managing keyboard, mouse inputs yourself.

    I used to do these things years ago with old compilers. I'm not sure if these facilities are available now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tough problem with function pointers
    By Montejo in forum C Programming
    Replies: 10
    Last Post: 12-22-2009, 01:17 AM
  2. wxWidgets link problem
    By cboard_member in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2006, 02:36 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM