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

标签 c++ stxxl

下面的程序,崩溃了

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/

相关文章:

c++ - 函数计时器阻止 main 继续

c++ - 使用 new int[10] 时元数据需要多少内存?

c++ - 在 C++ 中运行时获取变体中包含的类型?

c++ - 如何使用 std::string 作为 stxxl::map 中的键

c++ - 在 MakeFile 中包含 STXXL 库

C++:简单代码中的 STXXL 和 VS 运行时错误

c++ - 为什么当内存足够时 malloc() 会失败?

java - Java 中的 STXXL 等价物

c++ - STXXL多维 vector 使程序卡住

c++ - 如果不同的线程正在调用另一个不同线程的相同信号,Qt 中是否需要互斥锁?