LINUX TIPS AND TRICKS --- July 14, 2000

Published by ITworld.com, the IT problem-solving network
http://www.itworld.com/newsletters

*********************************************************************

Optimizing Thread Performance 
by Danny Kalev

A reader who used a C++ Standard Template Library (STL) container in a 
multithreaded application complained about significant performance 
degradation due to the use of threads. In some parts, although each 
thread had its own container object, his multithreaded application 
suffered from a 50% loss of the runtime speed compared to the single 
threaded version of the same application. He wanted to know what could 
be the cause and how to improve STL performance when it's used in 
multithreaded environment.

Many factors affect a multithreaded application's performance. Among 
them, the kernel's built-in thread support is one of the most important. 
Some operating systems are better than others in this regard. For 
instance, BeOS is better than MacOS in its overall thread performance 
and the various Unix versions also differ in terms of thread 
performance. A second factor is the thread library used. Currently, 
Linux has several independent thread libraries (pthreads, Linux threads, 
and others) that differ in the duration of context switches between 
threads. The synchronization mechanism used (e.g., critical section, 
mutex), and the number of threads in a process also affect performance. 
Other factors include system load and the actual code that each thread 
executes. In general, the more context switches your application has, 
the higher the overhead -- unless you're using a multiprocessor machine. 
 
As for STL, it's worth noting that standard C++ doesn't specify whether 
STL containers are thread-safe. Different STL implementations use 
different thread-safety levels, and their performance varies 
accordingly. Therefore, if you have the time and patience, you can 
measure one or two more implementations of STL and see how they perform. 
But before you do that, remember that even if each thread has its own 
container object, this doesn't necessarily mean that the threads don't 
block each other frequently. Why is this? Every STL container allocates 
memory from the heap, which is still shared by all threads of the same 
process. Therefore, if your container objects reallocate storage 
frequently, your threads might block each other too often. To avoid 
this, insert all the elements at once during the container's 
construction: 

    int arr[1000];
    /*.. fill arr with values*/
    std::vector  int  v(arr,arr+1000);/*initialize s with arr*/

In this example, the vector is initialized with 1000 elements. It won't 
reallocate new storage as long as you don't call any member function 
that changes the vector's capacity. On the other hand, the following 
code is highly inefficient because the vector object reallocates storage 
frequently:

    for (int i=0 ; i 1000; ++i)
    {
      v.push_back(i);
    }

Resources

Threads libraries in Solaris 
New features for developing multi-threaded code
http://www.sunworld.com/sunworldonline/swol-06-2000/f_swol-06-insidesolaris.html

Tcl adds multithreading in bid for Enterprise 
Scripting language now ready for server-class applications, says 
Ousterhout
http://www.linuxworld.com/linuxworld/lw-1999-05/f_lw-05-tcl.html

************************************************************************

About the author
----------------
Danny Kalev is a system analyst and software engineer with more
than 10 years of experience, specializing in C++ and
object-oriented analysis and design on various platforms including
VMS, DOS, Windows, Unix, and Linux. His technical interests involve
code optimization, networking, and distributed computing. He is
also a member of the ANSI C++ standardization committee and the
author of ANSI/ISO C++ Professional Programmer's Handbook (Que,
1999). Contact him at linuxnl@excite.com.
 
*********************************************************************

CUSTOMER SERVICE

You can subscribe or unsubscribe to any of your e-mail newsletters by 
updating your form at: 
http://www.itworld.com/cgi-bin/w3-msql/newsletters/subcontent12.html?

For subscription changes that cannot be handled via the web, please send 
an email to our customer service dept: support@itworld.com

*********************************************************************

CONTACTS

* For editorial comments, write Andrew Santosusso, Associate Editor, 
Newsletters at: andrew_santosusso@itworld.com
* For advertising information, write Dan Chupka, Account Executive at:
dan_chupka@itworld.com
* For all other inquiries, write Jodie Naze, Product Manager,
Newsletters at: jodie_naze@itworld.com

*********************************************************************

Copyright 2000 ITworld.com, Inc., All Rights Reserved. 

http://www.itworld.com
