Click here to Skip to main content
15,887,214 members
Home / Discussions / Algorithms
   

Algorithms

 
GeneralRe: Lock free algorithms Pin
Michael Gazonda31-Jul-14 19:25
professionalMichael Gazonda31-Jul-14 19:25 
AnswerRe: Lock free algorithms Pin
SledgeHammer016-Sep-14 8:58
SledgeHammer016-Sep-14 8:58 
GeneralRe: Lock free algorithms Pin
Joe Woodbury6-Sep-14 9:50
professionalJoe Woodbury6-Sep-14 9:50 
GeneralRe: Lock free algorithms Pin
SledgeHammer016-Sep-14 11:35
SledgeHammer016-Sep-14 11:35 
GeneralRe: Lock free algorithms Pin
Joe Woodbury6-Sep-14 11:49
professionalJoe Woodbury6-Sep-14 11:49 
GeneralRe: Lock free algorithms Pin
SledgeHammer016-Sep-14 12:55
SledgeHammer016-Sep-14 12:55 
GeneralRe: Lock free algorithms Pin
Joe Woodbury6-Sep-14 13:11
professionalJoe Woodbury6-Sep-14 13:11 
GeneralRe: Lock free algorithms Pin
SledgeHammer016-Sep-14 13:37
SledgeHammer016-Sep-14 13:37 
Joe Woodbury wrote:
That's the point of my question. When doing a lock-free algorithm, how do you
avoid polling (due to starvation or saturation)? In the discussions of the
well-known circular queue, this is never actually answered--it assumes that a
natural balance will always be present.
 
For the record, my
various solutions have used locks.


I'm not familiar with your requirements, but a circular buffer wouldn't be my first choice for this (unless your requirement is to discard data when the buffer is full). I would use something more like a queue. But right now your code probably looks like this:

while (true)
{
someClass theTask;

if (buffer.GetWork(out theTask))
{
DoWork(theTask);
}
}

or something of that nature. That is a *very* tight loop when no work is available and will kill the CPU. A hack in the polling design is to add a sleep to alleviate the pressure on the CPU. You'll still have some CPU usage, but it'll be much less. A drawback would be, of course, that your client couldn't do any work during the sleep time which is a huge waste of CPU time. I.e. if you slept for 1000ms, and you got a message at 10ms in, your client wouldn't do anything for another 990ms.

A push / no polling design would be something like where your buffer class exposes a waitable handle such as a mutex. Now your client code looks like:

while (true)
{
WaitForSingleObject(buffer.theMutex);
DoWork(buffer.GetWork());
}

or something like that. The WaitForSingleObject() will block with 0% CPU usage. It will only return when the mutex is set.

So...

Your buffer class would set the mutex whenever data is inserted into the queue (in the insert method). The GetWork() method would reset the mutex when there are no remaining items (idle time).

Not sure how you are sending over the network, but sockets support async push notifications, so you'd have to make sure you use that as well. In the socket async callback, you'd insert into the queue which would set the mutex.
GeneralRe: Lock free algorithms Pin
Joe Woodbury6-Sep-14 13:44
professionalJoe Woodbury6-Sep-14 13:44 
QuestionAlgorithm for comparing word with randomly distributed substring Pin
asdf2321130-Jun-14 23:06
asdf2321130-Jun-14 23:06 
AnswerRe: Algorithm for comparing word with randomly distributed substring Pin
Sanmayce3-Jul-14 7:15
Sanmayce3-Jul-14 7:15 
AnswerRe: Algorithm for comparing word with randomly distributed substring Pin
Sanmayce5-Jul-14 7:19
Sanmayce5-Jul-14 7:19 
QuestionCubesort Pin
Igor van den Hoven22-Jun-14 4:09
Igor van den Hoven22-Jun-14 4:09 
AnswerRe: Cubesort Pin
Sanmayce28-Jun-14 8:02
Sanmayce28-Jun-14 8:02 
GeneralRe: Cubesort Pin
Igor van den Hoven29-Jun-14 9:58
Igor van den Hoven29-Jun-14 9:58 
AnswerRe: Cubesort Pin
Sanmayce30-Jun-14 6:48
Sanmayce30-Jun-14 6:48 
GeneralRe: Cubesort Pin
Igor van den Hoven12-Jul-14 4:01
Igor van den Hoven12-Jul-14 4:01 
AnswerRe: Cubesort Pin
Sanmayce3-Jul-14 4:30
Sanmayce3-Jul-14 4:30 
QuestionReduce a Q2SAT formula Pin
Apurvgupta15-Jun-14 20:48
Apurvgupta15-Jun-14 20:48 
QuestionFastest textual decompression in C Pin
Sanmayce10-May-14 8:54
Sanmayce10-May-14 8:54 
AnswerRe: Fastest textual decompression in C Pin
Richard MacCutchan10-May-14 21:46
mveRichard MacCutchan10-May-14 21:46 
GeneralRe: Fastest textual decompression in C Pin
Sanmayce12-May-14 0:18
Sanmayce12-May-14 0:18 
GeneralRe: Fastest textual decompression in C Pin
Richard MacCutchan12-May-14 1:24
mveRichard MacCutchan12-May-14 1:24 
GeneralRe: Fastest textual decompression in C Pin
Chris Losinger23-May-14 3:12
professionalChris Losinger23-May-14 3:12 
GeneralRe: Fastest textual decompression in C Pin
Sanmayce24-May-14 7:17
Sanmayce24-May-14 7:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.