1

I have an exe that writes data to shared memory than i read data from shared memory, i want to use std::thread when copy shared memory to different memory because writer exe does not stop writing.

When i use std::thread for copying shared memory,i pass memory structure as parameter than memory structure will be null in thread function, but when use the function normally, function memory structure will be as i expect. am i missed something or std::thread not useable for shared memory ?

struct Foo {
imgHeader imgHeader;
uint8_t imgBuf[12441600];
}

void memCpyFunc(uint8_t buf[12441600], std::list<uint8_t*>& bufferList) {

    uint8_t* ImgBuffer = new uint8_t[12441600];
    memcpy(ImgBuffer, buf, 12441600);
    bufferList.push_back(ImgBuffer);

}



int main(){
    Foo* foo;
    shmid = shmget(SHM_KEY, sizeof(Foo), 0666 | IPC_CREAT);
    foo = (Foo*)shmat(shmid, NULL, 0);
    std::list<uint8_t*> bufferList;
    memCpyFunc(foo->imgBuf, bufferList); //It works
    std::thread thread(&memCpyFunc, foo->imgBuf, std::ref(bufferList)); //Segmantation Fault
    //foo->imgBuf error: Cannot access memory at address 0x7ffff03b3078
    thread.detach();
    if (shmdt(foo) == -1) {
        perror("shmdt");

    }
}


I simply want to copy data from shared memory to different memory with thread.

Serhan Erkovan
  • 481
  • 4
  • 18
  • 5
    Have you tried with `thread.join()` instead of `thread.detatch()`? `detatch()` is almost always a sign of design error. The thread may be working with a global resource that is being released by the main thread returning from `main`. – François Andrieux Mar 14 '22 at 13:25
  • 1
    What is `Haeds::ImageDs::encshmv1_t`? Should be `Foo` I think. You also declare a variable `Foo* shmp;` and use it with the name `foo`. – mch Mar 14 '22 at 13:28
  • @mch sorry, yes it is foo, i forgot to change name with foo when writing questions – Serhan Erkovan Mar 14 '22 at 13:51
  • @FrançoisAndrieux `thread.join()` is works fine, could i want to use `thread.detatch()` instead of `thread.join()` – Serhan Erkovan Mar 14 '22 at 14:08
  • 2
    @SerhanErkovan You should never use `detatch()` it inevitably leads to problems like the one you are asking about. – François Andrieux Mar 14 '22 at 14:14
  • 1
    @FrançoisAndrieux I don't like to be anal, but it's `detach`... without the 't'. You all keep spelling it wrong.... – JHBonarius Mar 14 '22 at 14:20
  • 1
    @JHBonarius Thanks, didn't realize. Too late to edit, but I'll watch out for that in the future. – François Andrieux Mar 14 '22 at 14:23
  • 1
    @SerhanErkovan reminder with shared memory you need to do synchronization with the writing process too. Otherwise your application isn't data race free and that's undefined behavior. So you'll need to wrap any writes and read in some sort of mutex shared between the processes. – Mgetz Mar 14 '22 at 15:16
  • Can you explain in more detail how why you think that creating a thread helps? There is practically never any valid reason for one thread (e.g., the main() thread in your example) to create a new thread if the parent isn't going to do anything else concurrently with the child. You claim that your program works if `main()` simply _calls_ `memCpyFunc()` instead of creating a new thread. OK, if it _works,_ then why is that not good enough? – Solomon Slow Mar 14 '22 at 15:42

1 Answers1

2

Okay, there are some problems with your code as written. First, main is almost certainly going to quit before the thread gets a chance to run. But even if this isn't main but is something else (and thus your program isn't trying to quit), then bufferList is going out of scope before your thread runs.

This is a time when Francois might be right about using thread->join(), although I normally disagree with him about that. There are times to detach and times to use join.

Are you sure the error is about the shared memory and not about bufferList?

I'd do this. First, I'd change the detach to a join, and then I'd stick debug statements in the function you're calling. The code should work as long as main doesn't return too fast.

Joseph Larson
  • 8,530
  • 1
  • 19
  • 36
  • I would be curious of an example where `detach` is correct, I've never seen one. – François Andrieux Mar 14 '22 at 14:54
  • @FrançoisAndrieux We can agree to disagree. I think, however, when you're going to tell people not to use it, you should either reference a source that backs up your opinion or state it as your personal opinion. – Joseph Larson Mar 14 '22 at 14:57
  • I'll try to do that in the the future. In this case : [When should I use std::thread::detach?](https://stackoverflow.com/questions/22803600/when-should-i-use-stdthreaddetach). It isn't just a personal opinion, c++ has a recommended design philosophy, this is part of it, like not using raw owning pointers. – François Andrieux Mar 14 '22 at 15:03
  • Note that the query for an example was genuine. If you've encountered a use case for it, I would appreciate it if you could share it. – François Andrieux Mar 14 '22 at 15:07
  • @FrançoisAndrieux I'm not sure why you feel that answer is any sort of definitive statement by the C++ gods. Out of several answers, one offers an opinion-based "rule" that detach shouldn't be used. I don't want to try to have a lengthy conversation about this in the comments thread. Not sure if there's a different channel we could use to communicate. – Joseph Larson Mar 14 '22 at 23:01
  • If you which to continue this discussion, we can do so here : https://chat.stackoverflow.com/rooms/242936/c-std-thread-detach-discussion – François Andrieux Mar 15 '22 at 00:10
  • Original program is not same with that example, but i thought about your wrote, i detach shared memory before function end. because of this program crashed. – Serhan Erkovan Mar 15 '22 at 06:31