Fixed grammar
Source Link
cr_oag
  • 555
  • 3
  • 13

Consider what happens if writer.load() returns value A, but before the expression is evaluated, writer is changed to some other value B. This could cause empty()empty() to say that is the queue is empty when it isn't. Or to say or that it isn't when it actually is.

Consider what happens if writer.load() returns value A, but before the expression is evaluated, writer is changed to some other value B. This could cause empty() to say that is the queue is empty when it isn't. Or to say that it isn't when it actually is.

Consider what happens if writer.load() returns value A, but before the expression is evaluated, writer is changed to some other value B. This could cause empty() to say that the queue is empty when it isn't or that it isn't when it actually is.

added comment above trouble line
Source Link
cr_oag
  • 555
  • 3
  • 13

Issue #1: Unsynchronized access to writer

ConclusionIssue #2: Malfunctioning empty function

The load from reader and the load from writer followed by a comparison is not thread safe.

bool empty() const
{
    return reader.load() == writer.load();
}

Consider what happens if writer.load() returns value A, but before the expression is evaluated, writer is changed to some other value B. This could cause empty() to say that is onethe queue is empty when it isn't. Or to say that it isn't when it actually is.

Conclusion

These are two of many issues that are currently in your code. I've decided to stop here since that's a pretty bad problem. This current implementation if not safe at all. I recommend you do some more reading and learning about thread interaction. Good luck.

Issue

Conclusion

This is one of many issues that are currently in your code. I've decided to stop here since that's a pretty bad problem. This current implementation if not safe at all. I recommend you do some more reading and learning about thread interaction. Good luck.

Issue #1: Unsynchronized access to writer

Issue #2: Malfunctioning empty function

The load from reader and the load from writer followed by a comparison is not thread safe.

bool empty() const
{
    return reader.load() == writer.load();
}

Consider what happens if writer.load() returns value A, but before the expression is evaluated, writer is changed to some other value B. This could cause empty() to say that is the queue is empty when it isn't. Or to say that it isn't when it actually is.

Conclusion

These are two of many issues that are currently in your code. I've decided to stop here since that's a pretty bad problem. This current implementation if not safe at all. I recommend you do some more reading and learning about thread interaction. Good luck.

added comment above trouble line
Source Link
cr_oag
  • 555
  • 3
  • 13
if (next_writer != reader.load())
{
    if (writer.compare_exchange_weak(past_writer, next_writer))
    {
        array[past_writer]// =thread object;can be stalled here and try_pop() will access garbage memory
        // since at this point writer.store(next_writer); != reader
        array[past_writer] = object; 
        return true;
    }
}

Conclusion

if (next_writer != reader.load())
{
    if (writer.compare_exchange_weak(past_writer, next_writer))
    {
        array[past_writer] = object;
        writer.store(next_writer);
        return true;
    }
}
if (next_writer != reader.load())
{
    if (writer.compare_exchange_weak(past_writer, next_writer))
    {
        // thread can be stalled here and try_pop() will access garbage memory
        // since at this point writer != reader
        array[past_writer] = object; 
        return true;
    }
}

Conclusion

Source Link
cr_oag
  • 555
  • 3
  • 13
Loading