LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-13-2008, 11:38 AM   #1
emp1953
LQ Newbie
 
Registered: Nov 2007
Posts: 18

Rep: Reputation: 0
sleep( ) and usleep( ) problem


I have the following code that fails to compile. Sleep() just doesn't work and this version with usleep() gives a gcc personality error when compiling.
compile line is gcc -lpthread code.cpp

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>

void * thread_function (void * args) {
while (1) {
printf ("This is the thread speaking\n");
usleep (2*10000);
}
return NULL;
}

int main () {
pthread_t thread;
pthread_create (&thread, NULL, thread_function, NULL);
while (1) {
printf ("This is main talking\n");
usleep (2*10000);
}
return 0;
}
 
Old 03-13-2008, 02:04 PM   #2
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
Sleep() is a MS function (ok, maybe it's a *nix function too, but I'm not aware of it). sleep() is a *nix function. it operates on seconds, so sleep(1) sleeps for one second.

I can compile the above code with g++ rather than gcc. is there a reason to use gcc? for the record, I tried the above code with both sleep and usleep - both worked when compiling with g++

Last edited by BrianK; 03-13-2008 at 02:07 PM.
 
Old 03-13-2008, 03:50 PM   #3
emp1953
LQ Newbie
 
Registered: Nov 2007
Posts: 18

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by BrianK View Post
Sleep() is a MS function (ok, maybe it's a *nix function too, but I'm not aware of it). sleep() is a *nix function. it operates on seconds, so sleep(1) sleeps for one second.

I can compile the above code with g++ rather than gcc. is there a reason to use gcc? for the record, I tried the above code with both sleep and usleep - both worked when compiling with g++


My bad, without knowing it you solved my problem. My makefile used gcc instead of g++

Thanks
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
usleep reduce CPU spikes ? anjanesh Programming 5 06-26-2007 12:02 PM
usleep? kalleanka Programming 5 08-22-2006 08:01 AM
stopping usleep function allomeen Programming 4 12-19-2005 08:23 PM
Searching for Server (BOOTP/DHCP) <sleep> <sleep> .. .. Eileen Linux - Networking 12 10-21-2005 02:14 AM
Perl - problem with sleep command rose_bud4201 Programming 2 05-16-2005 12:44 PM


All times are GMT -5. The time now is 12:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration