Search code examples
c++stxxl

Working with stxxl in multiple threads


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

Solution

  • 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.