gpt4 book ai didi

c++ - 在多线程中使用 stxxl

转载 作者:行者123 更新时间:2023-11-27 23:09:26 36 4
gpt4 key购买 nike

下面的程序,崩溃了

libc++abi.dylib: terminating with uncaught exception of type stxxl::io_error: Error in virtual void stxxl::ufs_file_base::lock() : fcntl(,F_SETLK,) path=/var/tmp/stxxl fd=5 : Resource temporarily unavailable: unspecified iostream_category error
Abort trap: 6

这看起来很像我的两个线程正在尝试使用相同的文件处理程序/文件来更新 stxxl 文件直到/var/tmp。

在 stxxl 中有多个线程使用多个文件的技巧吗?

#include <stxxl/queue>
#include <iostream>

#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>




void test() {
typedef stxxl::queue<unsigned int> queue;
queue my_queue;
for(unsigned long long i = 0; i != 1024L * 1024 * 1024; i++)
my_queue.push(10);

std::cout << "queue_size " << my_queue.size() << std::endl;

while(my_queue.size() != 0)
my_queue.pop();

std::cout << "queue_size " << my_queue.size() << std::endl;
}

int main()
{
pid_t pid;
pid_t cpid;
int status;


pid = fork();

if (pid == 0)
{
test();
exit(0);
} else
{

test();
if ((cpid=wait(&status)) == pid){
std::cout << "Child " << pid << " returned" << std::endl;
}
}

return 0;
}

最佳答案

对于 STXXL 1.4.0,您还可以在 .stxxl 配置文件中使用“###”。打开文件时,“###”将替换为当前 pid。

请注意,调用第一个 STXXL 函数时会自动打开磁盘文件。因此,必须将此类调用延迟到 fork() 之后,就像您在示例中所做的那样。

关于c++ - 在多线程中使用 stxxl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21222783/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com