LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   sleep( ) and usleep( ) problem (https://www.linuxquestions.org/questions/linux-newbie-8/sleep-and-usleep-problem-627784/)

emp1953 03-13-2008 10:38 AM

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;
}

BrianK 03-13-2008 01:04 PM

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++

emp1953 03-13-2008 02:50 PM

Quote:

Originally Posted by BrianK (Post 3087572)
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


All times are GMT -5. The time now is 10:05 PM.