Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Can I use select instead Sleep?

Can I use select instead Sleep?

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
15 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hanlei0000000009
    wrote on last edited by
    #1

    I need my program sleep 1 microsecond, but Sleep() can not do this. So, can I use select() instead Sleep() to do this? timeout.tv_sec=0; timeout.tv_usec=1; FD_ZERO(&readfd); ret=select(0,&readfd,NULL,NULL,&timeout); It's have any problem or not?

    K R S D L 6 Replies Last reply
    0
    • H hanlei0000000009

      I need my program sleep 1 microsecond, but Sleep() can not do this. So, can I use select() instead Sleep() to do this? timeout.tv_sec=0; timeout.tv_usec=1; FD_ZERO(&readfd); ret=select(0,&readfd,NULL,NULL,&timeout); It's have any problem or not?

      K Offline
      K Offline
      KarstenK
      wrote on last edited by
      #2

      Why sleep? There is also the WaitForMultipleObject()-API "Carpe diem"   :-O

      Press F1 for help or google it. Greetings from Germany

      1 Reply Last reply
      0
      • H hanlei0000000009

        I need my program sleep 1 microsecond, but Sleep() can not do this. So, can I use select() instead Sleep() to do this? timeout.tv_sec=0; timeout.tv_usec=1; FD_ZERO(&readfd); ret=select(0,&readfd,NULL,NULL,&timeout); It's have any problem or not?

        R Offline
        R Offline
        Rajesh R Subramanian
        wrote on last edited by
        #3

        hanlei0000000009 wrote:

        I need my program sleep 1 microsecond, but Sleep() can not do this.

        Neither can any other API do this (under Windows). Windows was never designed to provide this kind of functionality (it is not a real-time OS). If you use something like Sleep(2), your program may sleep for 2 milliseconds, may be 3, or 4, or say even 100 ms. There's no guarantee about this. The thread scheduler tries its levels best to put your thread to an 'unschedulable' state for as close as possible, to what you've asked. But, it can almost never be exact. So, while Windows cannot even promise you the precision of milliseconds, you can forget microseconds, which is 1000 times the precision of milliseconds!

        It is a crappy thing, but it's life -^ Carlo Pallini

        1 Reply Last reply
        0
        • H hanlei0000000009

          I need my program sleep 1 microsecond, but Sleep() can not do this. So, can I use select() instead Sleep() to do this? timeout.tv_sec=0; timeout.tv_usec=1; FD_ZERO(&readfd); ret=select(0,&readfd,NULL,NULL,&timeout); It's have any problem or not?

          S Offline
          S Offline
          Stuart Dootson
          wrote on last edited by
          #4

          Yeah, that's not happening... Why 1 microsecond, FFS? What are you trying to do? BEcause there may well be a better way.

          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

          1 Reply Last reply
          0
          • H hanlei0000000009

            I need my program sleep 1 microsecond, but Sleep() can not do this. So, can I use select() instead Sleep() to do this? timeout.tv_sec=0; timeout.tv_usec=1; FD_ZERO(&readfd); ret=select(0,&readfd,NULL,NULL,&timeout); It's have any problem or not?

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            hanlei0000000009 wrote:

            I need my program sleep 1 microsecond...

            If you need this delay in order for the code to work, you've got an underlying problem that needs to be resolved. Adding arbitrary delays such as this is just asking for trouble later.

            "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            1 Reply Last reply
            0
            • H hanlei0000000009

              I need my program sleep 1 microsecond, but Sleep() can not do this. So, can I use select() instead Sleep() to do this? timeout.tv_sec=0; timeout.tv_usec=1; FD_ZERO(&readfd); ret=select(0,&readfd,NULL,NULL,&timeout); It's have any problem or not?

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              The Windows multimedia timers[^] might be useful

              R 1 Reply Last reply
              0
              • L Lost User

                The Windows multimedia timers[^] might be useful

                R Offline
                R Offline
                Rajesh R Subramanian
                wrote on last edited by
                #7

                Actually, they won't help. The OP wants resolution in terms of microseconds!

                It is a crappy thing, but it's life -^ Carlo Pallini

                L 1 Reply Last reply
                0
                • R Rajesh R Subramanian

                  Actually, they won't help. The OP wants resolution in terms of microseconds!

                  It is a crappy thing, but it's life -^ Carlo Pallini

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Well it's going to be better than a standard timer, so it's at least closer

                  R 1 Reply Last reply
                  0
                  • L Lost User

                    Well it's going to be better than a standard timer, so it's at least closer

                    R Offline
                    R Offline
                    Rajesh R Subramanian
                    wrote on last edited by
                    #9

                    I was particularly avoiding in providing any possible solution to him, because the OP's approach has a fundamental flaw (introducing 'artificial' delays in his code). And to answer you, NO, multimedia timers are neither going to provide resolutions any closer to microseconds! Not under Windows.

                    It is a crappy thing, but it's life -^ Carlo Pallini

                    L 1 Reply Last reply
                    0
                    • R Rajesh R Subramanian

                      I was particularly avoiding in providing any possible solution to him, because the OP's approach has a fundamental flaw (introducing 'artificial' delays in his code). And to answer you, NO, multimedia timers are neither going to provide resolutions any closer to microseconds! Not under Windows.

                      It is a crappy thing, but it's life -^ Carlo Pallini

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      Rajesh R Subramanian wrote:

                      And to answer you, NO

                      I do not claim that they have a resolution of microseconds. But they have a resolution which is considerably higher than the standard timer so they come closer to the goal. Sure they don't reach it, but being closer is better than .. well.. not being closer

                      R 1 Reply Last reply
                      0
                      • L Lost User

                        Rajesh R Subramanian wrote:

                        And to answer you, NO

                        I do not claim that they have a resolution of microseconds. But they have a resolution which is considerably higher than the standard timer so they come closer to the goal. Sure they don't reach it, but being closer is better than .. well.. not being closer

                        R Offline
                        R Offline
                        Rajesh R Subramanian
                        wrote on last edited by
                        #11

                        harold aptroot wrote:

                        But they have a resolution which is considerably higher than the standard timer so they come closer to the goal. Sure they don't reach it, but being closer is better than .. well.. not being closer

                        Well, why don't you understand? Do I have to say again? It is NOT closer to the goal. The goal itself, is unachievable. Windows does not support such things. Not to mention the OP's approach is flawed. For an OS to provide real-time functionality, it would require a very strong knowledge of the underlying hardware (including how fast each and every device is, how fast is the bus, the processor, how will they behave under specific conditions, etc.,). However, Windows was designed to support a wide variety of hardware, which means that the OS cannot practically have a deep knowledge of each and every hardware device that it may have to work with. So they had to compromise on this part. Is that you down-voting? Why? Because I don't agree with your comment? Well, your comment is WRONG.

                        It is a crappy thing, but it's life -^ Carlo Pallini

                        L 1 Reply Last reply
                        0
                        • R Rajesh R Subramanian

                          harold aptroot wrote:

                          But they have a resolution which is considerably higher than the standard timer so they come closer to the goal. Sure they don't reach it, but being closer is better than .. well.. not being closer

                          Well, why don't you understand? Do I have to say again? It is NOT closer to the goal. The goal itself, is unachievable. Windows does not support such things. Not to mention the OP's approach is flawed. For an OS to provide real-time functionality, it would require a very strong knowledge of the underlying hardware (including how fast each and every device is, how fast is the bus, the processor, how will they behave under specific conditions, etc.,). However, Windows was designed to support a wide variety of hardware, which means that the OS cannot practically have a deep knowledge of each and every hardware device that it may have to work with. So they had to compromise on this part. Is that you down-voting? Why? Because I don't agree with your comment? Well, your comment is WRONG.

                          It is a crappy thing, but it's life -^ Carlo Pallini

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #12

                          You think it is not closer. But it is. He's never going to reach the goal, but a better resolution takes him closer to his goal. Just because you can not get there doesn't mean you can not get closer. And yes I gave you a 3, not because you disagree but because You are the one who is partically wrong. It would be closer to his goal. It's like trying to reach 0K (zero Kelvin) really - you won't reach it, but you can get a lot closer than your kitchen freezer (which would be the normal timer in this analogy) So why won't I understand? Because it isn't true in the first place. edit: and here's an other one: suppose someone says "I want to get positive infinity using an integer! I am using uint32 now but it won't go high enough", well of course he can't reach it, but he could get closer using an uint64. It could be that he didn't really need infinity but just "something really high" and that it would help him anyway, just as in this case the OP may not know that multimedia timers exist and they may be good enough for what he's doing - who knows? At least he will be better off knowing that they exist.

                          R 1 Reply Last reply
                          0
                          • L Lost User

                            You think it is not closer. But it is. He's never going to reach the goal, but a better resolution takes him closer to his goal. Just because you can not get there doesn't mean you can not get closer. And yes I gave you a 3, not because you disagree but because You are the one who is partically wrong. It would be closer to his goal. It's like trying to reach 0K (zero Kelvin) really - you won't reach it, but you can get a lot closer than your kitchen freezer (which would be the normal timer in this analogy) So why won't I understand? Because it isn't true in the first place. edit: and here's an other one: suppose someone says "I want to get positive infinity using an integer! I am using uint32 now but it won't go high enough", well of course he can't reach it, but he could get closer using an uint64. It could be that he didn't really need infinity but just "something really high" and that it would help him anyway, just as in this case the OP may not know that multimedia timers exist and they may be good enough for what he's doing - who knows? At least he will be better off knowing that they exist.

                            R Offline
                            R Offline
                            Rajesh R Subramanian
                            wrote on last edited by
                            #13

                            harold aptroot wrote:

                            At least he will be better off knowing that they exist.

                            Fine, he'll know that such a thing exists. That's the only point I can take. But, I won't take the "it's closer to the goal" codswallop.

                            harold aptroot wrote:

                            And yes I gave you a 3, not because you disagree but because You are the one who is partically wrong. It would be closer to his goal.

                            Now, you're being ridiculous. How is it closer? He's trying to achieve something that would require an RTOS, but is doing that task on Windows. So, how is any approach going to be closer to the goal? Not only you don't know about the subject, but you're also down-voting because you think that I'm wrong. How long have you been doing C++ or programming Windows to say that using a multimedia timer can be closer to make Windows behave like an RTOS? I'd assume that you're a teenager, have never worked on any professional project, and are talking about stuff that you know superficially (or know nothing about) here. I'm not wasting another minute on you.

                            It is a crappy thing, but it's life -^ Carlo Pallini

                            L 1 Reply Last reply
                            0
                            • R Rajesh R Subramanian

                              harold aptroot wrote:

                              At least he will be better off knowing that they exist.

                              Fine, he'll know that such a thing exists. That's the only point I can take. But, I won't take the "it's closer to the goal" codswallop.

                              harold aptroot wrote:

                              And yes I gave you a 3, not because you disagree but because You are the one who is partically wrong. It would be closer to his goal.

                              Now, you're being ridiculous. How is it closer? He's trying to achieve something that would require an RTOS, but is doing that task on Windows. So, how is any approach going to be closer to the goal? Not only you don't know about the subject, but you're also down-voting because you think that I'm wrong. How long have you been doing C++ or programming Windows to say that using a multimedia timer can be closer to make Windows behave like an RTOS? I'd assume that you're a teenager, have never worked on any professional project, and are talking about stuff that you know superficially (or know nothing about) here. I'm not wasting another minute on you.

                              It is a crappy thing, but it's life -^ Carlo Pallini

                              L Offline
                              L Offline
                              Lost User
                              wrote on last edited by
                              #14

                              Rajesh R Subramanian wrote:

                              I'm not wasting another minute on you.

                              Well thanks. But in my world any improvement means you're closer. And going from normal timers to multimedia timers is definitely an improvement if your goal is a very short wait. Thus it is closer. Are you getting extremely offensive over a definition of "closer"? Come on now.

                              Rajesh R Subramanian wrote:

                              He's trying to achieve something that would require an RTOS

                              Yes, so it's impossible, but so what? You can still get a lot closer to the goal than a 18ms resolution. Honestly now, an answer that is not right does not deserve a 5, a 4 is more fitting, you do have a point, however your definition of closer does not match the one in the dictionary. This isn't even a programming matter, it's just you having odd definition of being closer to a goal. Btw including my age in the argument is argumentum ad hominem - in other words, you would be wrong by default.

                              1 Reply Last reply
                              0
                              • H hanlei0000000009

                                I need my program sleep 1 microsecond, but Sleep() can not do this. So, can I use select() instead Sleep() to do this? timeout.tv_sec=0; timeout.tv_usec=1; FD_ZERO(&readfd); ret=select(0,&readfd,NULL,NULL,&timeout); It's have any problem or not?

                                H Offline
                                H Offline
                                hanlei0000000009
                                wrote on last edited by
                                #15

                                Thanks all. It's looked like I need transplant my project From Windows to Linux or Unix. timeval's member tv_usec is a value in microseconds. It declaration and used in UNIX first. So, I think UNIX, FreeBSD for example can do this as possible.

                                1 Reply Last reply
                                0
                                Reply
                                • Reply as topic
                                Log in to reply
                                • Oldest to Newest
                                • Newest to Oldest
                                • Most Votes


                                • Login

                                • Don't have an account? Register

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • World
                                • Users
                                • Groups