Ælex
Ælex

Reputation: 14839

Word-Net Thread safety

I am using Word-Net in a C++ project (although the library is in C). In specific, I am calling only two functions:

findtheinfo_ds
traceptrs_ds

Now, if I understand correctly the underlying structure (its quite old as it was written in the late nineties I think), the library uses files as the database from where it retrieves the buffer results I get.

However, I am not sure about the thread safety of the library.

My current algorithm is:

SynsetPtr syn = findtheinfo_ds( query , NOUN, HYPERPTR, ALLSENSES );

if ( syn )
{
    // Iterate all senses
    while ( syn )
    {
      for ( int i = 0; i < syn->wcount; i++ )
        std::cout << "synonym: " << syn->words[i] << std::endl;

      int i = 0;

      SynsetPtr ptr = traceptrs_ds( syn, HYPERPTR, NOUN, 1 );

      while ( ptr )
      {
        for ( int x = 0; x <= i; x++ )
          std::cout << "\t";

        for ( int i = 0; i < ptr->wcount; i++ )
          std::cout << ptr->words[i] << ", ";

        std::cout << std::endl;

        i++;

        auto old_ptr = ptr;
        ptr = traceptrs_ds( ptr, HYPERPTR, NOUN, 1 );
        free_syns( old_ptr );
      }

      free_syns( ptr );
      syn = syn->nextss;
    }
    free_syns( syn );
  }
}

However, I want to run parallel threads, searching for different words at the same time. I understand that most UNIX/Linux distributions of today have thread-safe file system calls.

Furthermore, I intend to access to the above loop, per one thread only.

What I am worried about, is that before this loop above, a

wninit();

call has to take place, which makes me assume that in the library, a singleton is somewhere initialized. I cannot take a peek at the code as it is closed-source, and I do not have access to that singleton, as winit() only returns an int for success.

Is there any way to either:

  1. Ensure thread-safety in this scenario, or
  2. Find out (through any possible way), if the library is thread safe?

It is loaded dynamically, from a Debian package called wordnet-base, which installs libwordnet-3.0.so

Many thanks to anyone who can help!

Upvotes: 0

Views: 137

Answers (1)

user3159253
user3159253

Reputation: 17455

Well, the only way to ensure that a library is really thread-safe, is to analyze its code. Or simply ask its author and then trust hisr/her answer:). Usually data stored on disk isn't the cause of thread unsafety but there's a lot of places where code may break in a multi-threaded environment. One has to check for global variables, existance of variables declared static inside library functions etc.

There's however a solution which could be used if you don't have time and/or intent to study the code. You may use a multiprocess technique when parallel tasks are performed in worker processes, not worker threads, and there's a director process which prepares job units for workers and collects results. Depending on the task such workers may be implemented as FastCGI, or communicate with parent using Boost.Interprocess

Upvotes: 1

Related Questions