pupose of code
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: pupose of code

  1. #1
    Join Date
    Sep 2009
    Posts
    23

    pupose of code

    What's the purpose of the code:

    void wait ( int *ptr, int k) { while ((1 << k) & (*ptr) == 0) {} }

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,885

    Re: pupose of code

    It seems like a rather awkward attempt at making a function which causes one thread to busy-wait until anther thread writes a different value to a particular bit. It would have to be used in a multi-threaded environment unless ptr points to a memory location set by an interrupt; otherwise, it would be an infinite loop.

    In a multithreaded environment, conditions and signals are a much better way to solve this problem.

  3. #3
    Join Date
    Sep 2009
    Posts
    23

    Re: pupose of code

    The location pointed by "ptr" may be getting written by another thread. But why left-shift "k"?

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,885

    Re: pupose of code

    Quote Originally Posted by abeginner View Post
    The location pointed by "ptr" may be getting written by another thread. But why left-shift "k"?
    k is not being left-shifted; instead, 1 is being left-shifted by k bits. Therefore, the result will be an integer for which the binary representation contains 1 in only one bit, and 0 elsewhere.

    When this is bitwise-anded with (*ptr), the result will be 0 or 1, and this will only depend upon the kth bit of (*ptr).

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center