6

I am writing a Server application which takes data from multiple sources at once and stores it in it's internal database (currently a std::set).

I have just been looking at Microsoft's ConcRT PPL data structures and wondering about how their efficiency compares to using a fine-grained mutex on a std::unordered_set. For example, is there much of a performance difference between the two code snippets:

void StdWithMutex( void )
{
     std::ofstream outFile( "Test.tmp" );

     std::lock_guard<std::mutex> lockGuard( m_mutex );

     // Iterate through the data and write it to a file:
     // m_setData is of type std::unordered_set<DataType>
     for( auto data : m_setData )
     {
          outFile << data;
     }
}

And:

void ConcRT( void )
{
     std::ofstream outFile( "Test.tmp" );

     // Iterate through the data and write it to a file:
     // m_setData is of type concurrency::concurrent_unordered_set
     for( auto data : m_setData )
     {
          outFile << data;
     }
}

Moveover, I often need to print the data out in order, which is why I'm currently using std::set as opposed to std::unordered_set, so if there is a gain in using concurrency::concurrent_unordered_set, would the potential performance gain come close to the cost of reordering the data each time it needs to be printed?

Thomas Russell
  • 5,480
  • 3
  • 28
  • 61
  • 5
    How is a class level mutex lock for the duration of a large file-write fine-grained? –  Jun 17 '13 at 17:25
  • It is difficult to work out what you are trying to achieve here. Your first code example locks the mutex for the entire duration of writing out the whole set. i.e. it is not concurrent with populating the set. In your second code example, the iterator in the for loop is presumably concurrent safe, so if the set was being populated while the for loop was running, presumably the for loop would only end if it obtained a lock, and moved to end before the populating thread obtained a lock to insert a new value. What are you trying to achieve? Concurrent read (from network) write (to disk)? – Ian Thompson Aug 14 '13 at 03:08

1 Answers1

1

Yes there is a huge difference. Try to run 100 threads in parallel writing and reading from this container and you will see the difference.

the PPL container does not lock -> it will be faster (it is probably wait free too, or using an improved allocator, while STL not except if you specified this allocator)

In single thread enrivronement although it is possible that the overhead of the lock is smaller than the one of the PPL container.

(in the same kind of idea the concurrent queue of coost or concurrent containers of TBB (intel) will be faster than STL containers which may all lock)

dzada
  • 4,148
  • 5
  • 23
  • 35