NO

Author Topic: Will someone post an example that sleeps 3.456us using _nanosleep?  (Read 134 times)

Offline bobgardner

  • Member
  • *
  • Posts: 4
Will someone post an example that sleeps 3.456us using _nanosleep?
« on: September 10, 2018, 12:34:39 am »
Like how to declare the struct vars and pass in the desired delay? Thanks.

Offline ika

  • Member
  • *
  • Posts: 12
Re: Will someone post an example that sleeps 3.456us using _nanosleep?
« Reply #1 on: September 10, 2018, 02:59:39 am »
This should work:

Code: [Select]
struct timespec tmp = {0};
tmp.tv_nsec = 3456;
_nanosleep(&tmp,NULL);

But, I have only use nanosleep on POSIX systems.

Offline bobgardner

  • Member
  • *
  • Posts: 4
Re: Will someone post an example that sleeps 3.456us using _nanosleep?
« Reply #2 on: September 11, 2018, 01:05:34 am »
I saw a new pellesc version and got all excited. When I saw nanosleep in the help file, I got even more excited. I want to send live raw audio (stereo 16bit littlendian samps) out the wifi on udp to a multicast ip addr. Next job is figuring how to listen  to it on an android phone, but I calculated that loading up 1400 bytes per pkt and sending every 7.9ms is about what I need. Your example help compiled right up, so thanks for the quick answer. Anyone know how to write an android app to play music?

Offline ika

  • Member
  • *
  • Posts: 12
Re: Will someone post an example that sleeps 3.456us using _nanosleep?
« Reply #3 on: September 11, 2018, 03:59:45 am »
I saw a new pellesc version and got all excited. When I saw nanosleep in the help file, I got even more excited. I want to send live raw audio (stereo 16bit littlendian samps) out the wifi on udp to a multicast ip addr. Next job is figuring how to listen  to it on an android phone, but I calculated that loading up 1400 bytes per pkt and sending every 7.9ms is about what I need. Your example help compiled right up, so thanks for the quick answer. Anyone know how to write an android app to play music?

Nanosleep is a bad idea to use for this purpose. Remember the time you pass into it is a minimum and not a maximum. You end up forfieting your time slice to the kernel and so there isn't any telling how long it will take. What you are really looking for is the high resolution timestamps API in windows: https://docs.microsoft.com/en-us/windows/desktop/SysInfo/acquiring-high-resolution-time-stamps

The kind of code for this would go as follows to wait for a specific amount of time without forfeiting the time-slice is like so:
Code: [Select]
LARGE_INTEGER f,co,cn;
uint64_t dts, ts = 0;

QueryPerformanceFrequency(&f);
QueryPerformanceCounter(&co);

dts = (f.QuadPart / 10000) * 76; //gives us the amount of QPC values for 7.6ms 

while(ts < dts){
    QueryPerformanceCounter(&cn);
    ts += cn.QuadPart - co.QuadPart;
    co = cn;
}

I didn't test any of that code, but that is the basic way that these API's work. It can solve the problem better than using any kind of sleep. The kernel can still interrupt, but that is just life... This is what you need to get it at EXACTLY 7.6ms, an optimization is to call _nanosleep in the while loop while you still have a lot of time. Since this procedure is going to eat up 100% of the CPU checking the time in a loop, it makes sense to forfeit the time-slice until you get close to the desired value, in which case you need to be greedy with your cycles to get the best precision.