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 am going to start implementing some unit tests for a codebase that is
What are some ideas (using .NET and SQL 2005) for implementing a service that
I'm implementing some rudimentary SQL Server monitoring to watch for excessive blocking. I have
I'm looking at implementing some form of anonymous user system in Rails. I need
What's the purpose of implementing the IDisposable interface? I've seen some classes implementing it
In some editors there exist plugins implementing a feature called hungry backspace or hungry
I've written some custom model binders (implementing IModelBinder) in our ASP.NET MVC application. I'm
I'm implementing a blog with tags with some French characters. My question has to
I am implementing an HTML form with some checkbox input elements, and I want
We are implementing an IP based geolocation service, and we need to find some

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.