Writer/Reader where a reader has to read 2 times in a row










1















So I have this problem that is kind of a classic writer/reader problem only with a twist.



Let's say, for example that you have one writer and multiple readers. The writer puts multiple messages in the shared data structure (a message contains of two small messages) so the data structure will look something like this: [m11 m12 m21 m22 m31 m32..] ,



where first number is message number and the second number is the part number. How can i make a reader receive one of the messages without beeing interrupted by another reader?



For example:



If reader 1 takes m11, I want that the shared resource to be blocked until he also reads m12.



The solution must be done only with semaphores or locks and a reader can only read one message at a time.










share|improve this question



















  • 1





    What have you tried so far? What's the issue?

    – Mark
    Nov 15 '18 at 11:39











  • What i have tried so far is implementing the classic solution, but only allow one reader at a time. The problem that i am facing is that after a reader gets the first part or the message sometimes a different rather takes the shared resource and takes the second part. I would want that the resource stays blocked untill the first reader also gets the second part.

    – daniel
    Nov 15 '18 at 11:46












  • Readers should not read from the data structure you described. Here's what I would imagine: Every 2 messages written, the writer locks a separate data structure and move the pair of messages there, afterwards releasing the lock. Then, when a reader wants to read, it reads both messages at once by locking in order to prevent the writer from writing, releasing the lock after finishing the read.

    – luci88filter
    Nov 15 '18 at 12:21












  • I understand your solution. The problem I see is that a reader can t read two messages at once. Imagine that the reader communicates with the data structure with a function that can only return one message at a time. Or maybe I don t understand correctly, how could the reader read 2 messages at the same time with this restriction?

    – daniel
    Nov 15 '18 at 13:00












  • I found a solution in java: ReentrantLock

    – daniel
    Nov 25 '18 at 19:32















1















So I have this problem that is kind of a classic writer/reader problem only with a twist.



Let's say, for example that you have one writer and multiple readers. The writer puts multiple messages in the shared data structure (a message contains of two small messages) so the data structure will look something like this: [m11 m12 m21 m22 m31 m32..] ,



where first number is message number and the second number is the part number. How can i make a reader receive one of the messages without beeing interrupted by another reader?



For example:



If reader 1 takes m11, I want that the shared resource to be blocked until he also reads m12.



The solution must be done only with semaphores or locks and a reader can only read one message at a time.










share|improve this question



















  • 1





    What have you tried so far? What's the issue?

    – Mark
    Nov 15 '18 at 11:39











  • What i have tried so far is implementing the classic solution, but only allow one reader at a time. The problem that i am facing is that after a reader gets the first part or the message sometimes a different rather takes the shared resource and takes the second part. I would want that the resource stays blocked untill the first reader also gets the second part.

    – daniel
    Nov 15 '18 at 11:46












  • Readers should not read from the data structure you described. Here's what I would imagine: Every 2 messages written, the writer locks a separate data structure and move the pair of messages there, afterwards releasing the lock. Then, when a reader wants to read, it reads both messages at once by locking in order to prevent the writer from writing, releasing the lock after finishing the read.

    – luci88filter
    Nov 15 '18 at 12:21












  • I understand your solution. The problem I see is that a reader can t read two messages at once. Imagine that the reader communicates with the data structure with a function that can only return one message at a time. Or maybe I don t understand correctly, how could the reader read 2 messages at the same time with this restriction?

    – daniel
    Nov 15 '18 at 13:00












  • I found a solution in java: ReentrantLock

    – daniel
    Nov 25 '18 at 19:32













1












1








1








So I have this problem that is kind of a classic writer/reader problem only with a twist.



Let's say, for example that you have one writer and multiple readers. The writer puts multiple messages in the shared data structure (a message contains of two small messages) so the data structure will look something like this: [m11 m12 m21 m22 m31 m32..] ,



where first number is message number and the second number is the part number. How can i make a reader receive one of the messages without beeing interrupted by another reader?



For example:



If reader 1 takes m11, I want that the shared resource to be blocked until he also reads m12.



The solution must be done only with semaphores or locks and a reader can only read one message at a time.










share|improve this question
















So I have this problem that is kind of a classic writer/reader problem only with a twist.



Let's say, for example that you have one writer and multiple readers. The writer puts multiple messages in the shared data structure (a message contains of two small messages) so the data structure will look something like this: [m11 m12 m21 m22 m31 m32..] ,



where first number is message number and the second number is the part number. How can i make a reader receive one of the messages without beeing interrupted by another reader?



For example:



If reader 1 takes m11, I want that the shared resource to be blocked until he also reads m12.



The solution must be done only with semaphores or locks and a reader can only read one message at a time.







java c algorithm parallel-processing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 11:33







daniel

















asked Nov 15 '18 at 11:23









danieldaniel

63




63







  • 1





    What have you tried so far? What's the issue?

    – Mark
    Nov 15 '18 at 11:39











  • What i have tried so far is implementing the classic solution, but only allow one reader at a time. The problem that i am facing is that after a reader gets the first part or the message sometimes a different rather takes the shared resource and takes the second part. I would want that the resource stays blocked untill the first reader also gets the second part.

    – daniel
    Nov 15 '18 at 11:46












  • Readers should not read from the data structure you described. Here's what I would imagine: Every 2 messages written, the writer locks a separate data structure and move the pair of messages there, afterwards releasing the lock. Then, when a reader wants to read, it reads both messages at once by locking in order to prevent the writer from writing, releasing the lock after finishing the read.

    – luci88filter
    Nov 15 '18 at 12:21












  • I understand your solution. The problem I see is that a reader can t read two messages at once. Imagine that the reader communicates with the data structure with a function that can only return one message at a time. Or maybe I don t understand correctly, how could the reader read 2 messages at the same time with this restriction?

    – daniel
    Nov 15 '18 at 13:00












  • I found a solution in java: ReentrantLock

    – daniel
    Nov 25 '18 at 19:32












  • 1





    What have you tried so far? What's the issue?

    – Mark
    Nov 15 '18 at 11:39











  • What i have tried so far is implementing the classic solution, but only allow one reader at a time. The problem that i am facing is that after a reader gets the first part or the message sometimes a different rather takes the shared resource and takes the second part. I would want that the resource stays blocked untill the first reader also gets the second part.

    – daniel
    Nov 15 '18 at 11:46












  • Readers should not read from the data structure you described. Here's what I would imagine: Every 2 messages written, the writer locks a separate data structure and move the pair of messages there, afterwards releasing the lock. Then, when a reader wants to read, it reads both messages at once by locking in order to prevent the writer from writing, releasing the lock after finishing the read.

    – luci88filter
    Nov 15 '18 at 12:21












  • I understand your solution. The problem I see is that a reader can t read two messages at once. Imagine that the reader communicates with the data structure with a function that can only return one message at a time. Or maybe I don t understand correctly, how could the reader read 2 messages at the same time with this restriction?

    – daniel
    Nov 15 '18 at 13:00












  • I found a solution in java: ReentrantLock

    – daniel
    Nov 25 '18 at 19:32







1




1





What have you tried so far? What's the issue?

– Mark
Nov 15 '18 at 11:39





What have you tried so far? What's the issue?

– Mark
Nov 15 '18 at 11:39













What i have tried so far is implementing the classic solution, but only allow one reader at a time. The problem that i am facing is that after a reader gets the first part or the message sometimes a different rather takes the shared resource and takes the second part. I would want that the resource stays blocked untill the first reader also gets the second part.

– daniel
Nov 15 '18 at 11:46






What i have tried so far is implementing the classic solution, but only allow one reader at a time. The problem that i am facing is that after a reader gets the first part or the message sometimes a different rather takes the shared resource and takes the second part. I would want that the resource stays blocked untill the first reader also gets the second part.

– daniel
Nov 15 '18 at 11:46














Readers should not read from the data structure you described. Here's what I would imagine: Every 2 messages written, the writer locks a separate data structure and move the pair of messages there, afterwards releasing the lock. Then, when a reader wants to read, it reads both messages at once by locking in order to prevent the writer from writing, releasing the lock after finishing the read.

– luci88filter
Nov 15 '18 at 12:21






Readers should not read from the data structure you described. Here's what I would imagine: Every 2 messages written, the writer locks a separate data structure and move the pair of messages there, afterwards releasing the lock. Then, when a reader wants to read, it reads both messages at once by locking in order to prevent the writer from writing, releasing the lock after finishing the read.

– luci88filter
Nov 15 '18 at 12:21














I understand your solution. The problem I see is that a reader can t read two messages at once. Imagine that the reader communicates with the data structure with a function that can only return one message at a time. Or maybe I don t understand correctly, how could the reader read 2 messages at the same time with this restriction?

– daniel
Nov 15 '18 at 13:00






I understand your solution. The problem I see is that a reader can t read two messages at once. Imagine that the reader communicates with the data structure with a function that can only return one message at a time. Or maybe I don t understand correctly, how could the reader read 2 messages at the same time with this restriction?

– daniel
Nov 15 '18 at 13:00














I found a solution in java: ReentrantLock

– daniel
Nov 25 '18 at 19:32





I found a solution in java: ReentrantLock

– daniel
Nov 25 '18 at 19:32












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53318391%2fwriter-reader-where-a-reader-has-to-read-2-times-in-a-row%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53318391%2fwriter-reader-where-a-reader-has-to-read-2-times-in-a-row%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3