Thread: How to wait some time to execute something (newbie question)

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    3

    How to wait some time to execute something (newbie question)

    Hi all,

    I'm a java programmer and new to C development on linux. I need to write a C code that wait some time to execute a function. In java it's as simple as follows:
    Code:
    //some code
    Thread.sleep(1000); //waits 1000 miliseconds and continues
    //some code
    In C on linux I know that pthread.h has thread manipulation functions, but I can't find a way to do the program execution to stop for some time and then continue.

    Thank you very much,

    Joao Paulo

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    sleep() is a standard function

    there is also nanosleep()
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MK27
    sleep() is a standard function
    To clarify, it is standard with respect to POSIX, but not C.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    3
    Thank you for the answers.

    In which header file the sleep function is in? I've looked in pthread.h, but it isn't there.


    Thanks.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by jpm View Post
    Thank you for the answers.

    In which header file the sleep function is in? I've looked in pthread.h, but it isn't there.


    Thanks.
    Sorry, laserlight is right, it is POSIX specific. The header is unistd.h

    If you are using the GNU libraries (linux et. al.), there's a function index:
    Function Index - The GNU C Library
    sleep() is there. You can download the whole manual:
    http://www.gnu.org/software/libc/manual/
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Jun 2009
    Posts
    3

    Talking

    Quote Originally Posted by MK27 View Post
    Sorry, laserlight is right, it is POSIX specific. The header is unistd.h

    If you are using the GNU libraries (linux et. al.), there's a function index:
    Function Index - The GNU C Library
    sleep() is there. You can download the whole manual:
    The GNU C Library - GNU Project - Free Software Foundation (FSF)
    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with time
    By Gong in forum C++ Programming
    Replies: 7
    Last Post: 01-11-2007, 02:43 PM
  2. newbie question on KDevelop
    By zaphodikus in forum Linux Programming
    Replies: 2
    Last Post: 06-05-2006, 12:57 AM
  3. Newbie question about registries/files
    By SirDavidGuy in forum C++ Programming
    Replies: 1
    Last Post: 02-20-2002, 09:58 PM
  4. time a function takes to execute (up to the millisecond)
    By jszielenski in forum C Programming
    Replies: 4
    Last Post: 11-23-2001, 01:34 AM
  5. Episode II Return Of the newbie : Website IO Question
    By MagiZedd in forum Windows Programming
    Replies: 1
    Last Post: 10-18-2001, 08:58 PM