-
November 19th, 2011, 09:44 PM
#1
pupose of code
What's the purpose of the code:
void wait ( int *ptr, int k) { while ((1 << k) & (*ptr) == 0) {} }
-
November 19th, 2011, 10:07 PM
#2
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.
-
November 19th, 2011, 10:10 PM
#3
Re: pupose of code
The location pointed by "ptr" may be getting written by another thread. But why left-shift "k"?
-
November 19th, 2011, 10:32 PM
#4
Re: pupose of code
Originally Posted by abeginner
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|