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 8450101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:52:52+00:00 2026-06-10T10:52:52+00:00

My program has 2 threads, I use shared memory to communicate between the two.

  • 0

My program has 2 threads, I use shared memory to communicate between the two.

Each thread has an ‘init’ method. In the init method, I call ‘shmget’ and ‘shmat’ to get the shared memory segment and attach to local variable. A portion of the code is like below:

Thread 1 (which runs first):

void init() {
  this->segment_id = shmget(SAME_KEY,SAME_SIZE,IPC_CREAT|S_IRUSR|S_IWUSR|S_IROTH|S_IWOTH);
  this->data       = shmat(this->segment_id,0,0);
}

Thread 2 (which runs after thread 1):

void init() {
  this->segment_id = shmget(SAME_KEY,SAME_SIZE,0);
  this->data       = shmat(this->segment_id,0,0);
}

The program is running but it gives unexpected result. What I’m afraid is that the call to ‘shmat’ in the second thread may make the ‘data’ in thread 1 inaccessible or some sort of malfunctionality. The fact is that, in thread 1 I can access the whole shared segment, but in thread 2 I can access only the first 16 bytes, so I don’t know what’s happening.

Does this mean each shared memory segment can be attached to 1 location at a time?
Is it ok to call ‘shmat’ twice to make the shared memory accessible from different pointers?

  • 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-06-10T10:52:53+00:00Added an answer on June 10, 2026 at 10:52 am

    Each shared memory segment can be attached to multiple locations at a time. It ok to call ‘shmat’ twice to make the shared memory accessible from different pointers.

    I suspect that in the given example you can’t be sure that Thread1::init() is called before Thread2::init() so in Thread2 you are accessing not created memory – so that is the reason for observed problems.

    Use IPC_CREAT for both calls (without IPC_EXCL of course).

    See manpage

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

Sidebar

Related Questions

My program has two threads: Main execution thread that handles user input and queues
I'm writing a ruby program that has 2 threads. One that listens on an
My program has to use certain files that reside in another directory. Right now
My program has one dialog and two sockets. Both sockets are derived from CAsyncSocket,
I have a class that starts a few threads. Each thread (extends Thread) calls
My program will use threads, but some variables like a collection of links/pages, information
I have two background threads and a thread to handle minimization running on my
I have a process that must create and close threads on demand. Each thread
Simple edition: In a C++ program I'm using two different threads to work with
My program has the following class definition: public sealed class Subscriber { private subscription;

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.