dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 25 of 25

Thread: Circular Buffer POP()

  1. #16
    Join Date
    Feb 2017
    Posts
    506

    Re: Circular Buffer POP()

    Quote Originally Posted by Raj90 View Post
    With this requirement is there any idea for the above issue?
    I would talk to my boss (or whoever is in charge of requirements). The try_pop call signature is almost like an industry standard used by both Microsoft and Intel.

    But okay. If it must look like exactly like this,

    T pop()

    and you cannot raise exceptions and you cannot set aside a special value of T that indicates an empty buffer, then the only remaining option as I see it is to introduce a global flag. This flag is set in pop() and can be read when pop() returns. If the flag is true then a valid T has been returned from the buffer, and if not the buffer was empty and the returned T is invalid.

    Personally I would prefer the try_pop() signature any time. Enforce it and you will gain guru status at your company .

  2. #17
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,972

    Re: Circular Buffer POP()

    Introducing a global flag, of course, is a possibility but using global flags is totally frowned upon and should be a non-starter. That is not good practice.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.4.2)

  3. #18
    Join Date
    Jun 2019
    Posts
    30

    Re: Circular Buffer POP()

    Quote Originally Posted by 2kaud View Post
    You have already been given the possibilities.

    I understood the possibilities... but those possibilities are not matching with my POP() method.

  4. #19
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,972

    Re: Circular Buffer POP()

    You have 2 options:

    1) Keep the pop() method and use one of the given possibilities
    2) Change the pop method.

    In this circumstance, I would consider getting rid of the test in pop() and leave it up to the caller to make sure the buffer isn't empty before calling pop() as there is an isempty(). Most of the STL doesn't do bounds checking unless specified. If you have no control over the function interface - what exactly is the given description for pop()?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.4.2)

  5. #20
    Join Date
    Feb 2017
    Posts
    506

    Re: Circular Buffer POP()

    Quote Originally Posted by Raj90 View Post
    I understood the possibilities... but those possibilities are not matching with my POP() method.
    I gave you the only remaining option in my post #16 and that's to introduce a global flag. As 2kaud notes and as I indicated this is not considered a good option. Still it will work and it will get the job done. And traditionally global flags are used in C/C++. Compare for example errno.
    Last edited by wolle; June 19th, 2019 at 06:51 AM.

  6. #21
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,972

    Re: Circular Buffer POP()

    What if the user wants more than one circular buffer? and if a global is used, what would be returned for the error case - T{}??
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.4.2)

  7. #22
    Join Date
    Feb 2017
    Posts
    506

    Re: Circular Buffer POP()

    Quote Originally Posted by 2kaud View Post
    What if the user wants more than one circular buffer? and if a global is used, what would be returned for the error case - T{}??
    The idea of a global flag was more of a principle. What I really meant is that if the OP wants to keep the pop() signature then the issue of blocking/non-blocking must be solved outside of pop(). One implementation that could work is to make the isEmpty() function part of the public interface. Just like there's a pop() function per buffer there could also be an isEmpty() per buffer.

    Then it's up to the user to make sure isEmpty() is false before a call to pop() is made. What should happen if a call to pop() still is made when isEmpty() is true? Well, either the result is undefined or, more reasonably, pop() would work like a blocking pop (a wait_pop()) and wait until there's something in the buffer to return.

    So in summary: Make pop() a blocking pop and supply an additional isEmpty() function that can be used to avoid calls to pop() if blocking is undesired.
    Last edited by wolle; June 19th, 2019 at 08:11 AM.

  8. #23
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,972

    Re: Circular Buffer POP()

    Then it's up to the user to make sure isEmpty() is false before a call to pop() is made.
    Yes - that was my conclusion in post #19.

    either the result is undefined
    That would fit in with other STL behavior.

    pop() would work like a blocking pop (a wait_pop()) and wait until there's something in the buffer to return
    That would assume two threads - a producer and a consumer, with the consumer doing the wait_pop(). Nothing in the OP's supplied code suggests this.

    From the info available I would go for undefined behaviour - and put the onus onto the user of the buffer.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.4.2)

  9. #24
    Join Date
    Feb 2017
    Posts
    506

    Re: Circular Buffer POP()

    Quote Originally Posted by 2kaud View Post
    Yes - that was my conclusion in post #19.
    Sorry, I see that now. Anyway good that we arrived at the same solution independently.

  10. #25
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,972

    Re: Circular Buffer POP()

    Quote Originally Posted by wolle View Post
    Sorry, I see that now. Anyway good that we arrived at the same solution independently.
    but an interesting discussion
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.4.2)

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)