Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3352792
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:04:23+00:00 2026-05-18T02:04:23+00:00

I was implementing some simple Producer/Consumer program that had some semaphores and shared memory.

  • 0

I was implementing some simple Producer/Consumer program that had some semaphores and shared memory. To keep things simple, let’s assume that there’s just a block of shared memory and a semaphore in my program.

At first, I though that I only had to consider as critical section bits of code that’d try to write to the shared memory block. But as the shared memory block consists of, let’s say, 1024bytes, I can’t read all the data at the same time (it’s not an atomic operation), so it is indeed possible that while I’m reading from it, the Producer comes and starts writing in it, so the reader will get half old data, half new data. From this, I can only think that I also have to put shared memory reading logic inside a “semaphore” block.

Now, I have lots of code that looks like this:

if (sharedMemory[0] == '0') { ... }

In this case, I am just looking for a single char in memory. I guess I don’t have to worry about puting a semaphore around this, do I?

And what if instead I have something like

if (sharedMemory[0] == '0' && sharedMemory[1] == '1') { ... }

From my perspective, I guess that as this are 2 operations, I’d have to consider this as a critical section, thus having to put a semaphore around it. Am I right?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T02:04:24+00:00Added an answer on May 18, 2026 at 2:04 am

    Technically, on a multicore or multiprocessor system the only thing that’s atomic are assembly opcodes which are specifically documented as being atomic. Even reading a single byte presents a (quite small) chance the another processor will come along and modify it before you’re doing reading it, except in some cases that deals with CPU cache and aligned chunks of memory (Fun thread: http://software.intel.com/en-us/forums/showthread.php?t=76744, Interesting read: http://www.corensic.com/CorensicBlog/tabid/101/EntryId/8/Memory-Consistency-Models.aspx)

    You must either use types which internally guarantee atomicity or specifically protect accesses on multithreaded multicore systems.

    (The answer may change slightly on IL platforms like .NET and JVMs since they make their own guarantees about what’s atomic and what isn’t).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a some simple Java code that looks similar to this in its
For some reason, I notice that I end up using a lot of finite
I have some places where implementing some sort of cache might be useful. For
I'm writing a UDP server that currently receives data from UDP wraps it up
I have some problems when I want to use implicit methods to convert a
How would I go about implementing a band-pass filter in c#? I'm using a
[Update]: Forgot to include ComponentArt... Hi, An interaction designer employed by the client I
So I have a problem. Or rather my friend has a problem, since I
I've been given a java api for connecting to and communicating over a proprietary
I have a website where users can upload their files; these are stored on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.