使用具有共享内存的线程
我有一个将数据写入共享内存的 exe,而不是从共享内存读取数据,当将共享内存复制到不同的内存时,我想使用 std::thread ,因为编写器 exe 不会停止写入。
当我使用 std::thread 复制共享内存时,我将内存结构作为参数传递,而不是线程函数中的内存结构将为空,但是当正常使用该函数时,函数内存结构将如我所料。我是否错过了某些内容或 std::thread
无法用于共享内存?
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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,您编写的代码存在一些问题。首先,main 几乎肯定会在线程有机会运行之前退出。但即使这不是 main 而是其他东西(因此你的程序不会尝试退出),那么 bufferList 在你的线程运行之前就会超出范围。
此时,Francois 关于使用 thread->join() 的观点可能是正确的,尽管我通常不同意他的观点。有时需要分离,有时需要使用连接。
您确定错误与共享内存有关,而不是与 bufferList 有关吗?
我会这样做。首先,我将分离更改为连接,然后将调试语句粘贴到您正在调用的函数中。只要 main 返回得不太快,代码就应该可以工作。
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.