首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在多线程中使用stxxl

在多线程中使用stxxl
EN

Stack Overflow用户
提问于 2014-01-19 21:16:30
回答 2查看 419关注 0票数 0

下面的程序,与

代码语言:javascript
复制
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文件til /var/tmp。

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

代码语言:javascript
复制
#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;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-01-20 16:47:12

使用STXXL1.4.0,您还可以在###配置文件中使用“.stxxl”。打开文件时,"###“被替换为当前的pid。

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

票数 3
EN

Stack Overflow用户

发布于 2014-01-20 19:19:20

我找到了答案,我自己,一个解决方案是给每个线程自己的虚拟磁盘,这个我已经这样做了:

代码语言:javascript
复制
  stxxl::config * cfg = stxxl::config::get_instance();

  std::string result; 
  std::ostringstream convert; 
  convert << id; // the id of the stream, to make th filenames different
  result = convert.str(); 
    std::string base_file = "/var/tmp/stxxl" + result;
  stxxl::disk_config disk1(base_file, 100 * 1024 * 1024, "syscall autogrow delete_on_exit");

  cfg->add_disk(disk1);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21222783

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档