Working with stxxl in multiple threads

444 views Asked by At

The following program, crashes with

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

This looks a lot like my two threads are trying to use the same file handler/file for doing updates to the stxxl file til /var/tmp.

Is there a trick for having multiple threads using multiple files in 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;
}
2

There are 2 answers

2
Timo Bingmann On BEST ANSWER

With STXXL 1.4.0 you can also use "###" in a .stxxl config file. The "###" is replaced with the current pid when files are opened.

Note that the disk files are automatically opened when the first STXXL functions are called. So one must delay such calls until after the fork(), which like you did in your example.

0
Martin Kristiansen On

I found the answer my self, a solution is to give every thread its own virtual disk, this I have done like this:

  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);