I am trying to delay the program using sleep command. But when I print the results, I don't see the delay. Am I missing something .
#include <iostream>
#include <stdlib.h>
#include <ctime>
#include <string.h>
#include <sys/time.h> // for time
#include <unistd.h> // for microsecond sleep
using namespace std;
const std::string currentDateTime()
{
time_t now = time(0);
struct tm tstruct;
char buf[80];
tstruct = *localtime(&now);
strftime(buf, sizeof(buf), "%Y-%m-%d.%X", &tstruct);
return buf;
}
int main(){
unsigned int microseconds = 10000000 ;
int min = 1;
int max = 10;
int randNum =0;
time_t t;
for(int i=0;i<5;i++)
{
randNum = rand()%(max-min + 1) + min;
std::cout << "At time "<< currentDateTime() << " Random portnumber is : " << randNum <<"\n";
int usleep(useconds_t microseconds);
}
return 0;
}
Here is my Output (I dont see any differnce in the delay):
$ g++ times.cc -lrt -o times
$ ./times
At time 2015-01-25.16:34:15 Random portnumber is : 4
At time 2015-01-25.16:34:15 Random portnumber is : 7
At time 2015-01-25.16:34:15 Random portnumber is : 8
At time 2015-01-25.16:34:15 Random portnumber is : 6
At time 2015-01-25.16:34:15 Random portnumber is : 4