C++
  Home arrow C++ arrow Page 11 - Multithreading in C++
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Actuate Whitepapers 
Moblin 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

Multithreading in C++
By: McGraw-Hill/Osborne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 136
    2005-07-14

    Table of Contents:
  • Multithreading in C++
  • An Overview of the Windows Thread Functions
  • Priority Classes
  • The Windows Synchronization Objects
  • The Thread Control Panel
  • A Closer Look at the Thread Control Panel
  • Demonstrating the Control Panel
  • A Multithreaded Garbage Collector
  • Synchronizing Access to gclist
  • The Entire Multithreaded Garbage Collector
  • Using the Multithreaded Garbage Collector

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Multithreading in C++ - Using the Multithreaded Garbage Collector


    (Page 11 of 11 )

    To use the multithreaded garbage collector, include gcthrd.h in your program. Then, use GCPtr in the same way as described in Chapter 2. When you compile the program, you must remember to link in the multithreaded libraries, as explained earlier in this chapter in the section describing _beginthreadex( ) and endthreadex( ).

    To see the effects of the multithreaded garbage collector, try this version of the load test program originally shown in Chapter 2:

    // Demonstrate the multithreaded garbage collector. #include <iostream>
    #include <new>
    #include "gcthrd.h"
    using namespace std;
    // A simple class for load testing GCPtr.
    class LoadTest {
      int a, b;
    public:
      double n[100000]; // just to take-up memory
      double val;
      LoadTest() { a = b = 0; }
      LoadTest(int x, int y) {
        a = x;
        b = y;
        val = 0.0;
      }

      friend ostream &operator<(ostream &strm, LoadTest &obj);
    };
    // Create an insertor for LoadTest.
    ostream &operator<(ostream &strm, LoadTest &obj) {
      strm << "(" << obj.a << " " << obj.b << ")";
      return strm;
    }
    int main() {
      GCPtr<LoadTest> mp;
      int i;
      for(i = 1; i < 2000; i++) {
        try {
          mp = new LoadTest(i, i);
          if(!(i%100))
            cout << "gclist contains " << mp.gclistSize()
                 << " entries.\n";
        } catch(bad_alloc xa) {
          // For most users, this exception won't
          // ever occur.
          cout << "Last object: " << *mp << endl;
          cout << "Length of gclist: "
               << mp.gclistSize() << endl;
        }
      }
      return 0;
    }

    Here is a sample run. (Of course, your output may vary.) This output was produced with the display option turned on by defining DISPLAY within gcthrd.h.

    Garbage collection started.
    gclist contains 42 entries.
    gclist contains 35 entries.
    gclist contains 29 entries.
    gclist contains 22 entries.
    gclist contains 18 entries.
    gclist contains 11 entries.
    gclist contains 4 entries.
    gclist contains 51 entries.
    gclist contains 47 entries.
    gclist contains 40 entries.
    gclist contains 33 entries.
    gclist contains 26 entries.
    gclist contains 19 entries.
    gclist contains 15 entries.
    gclist contains 10 entries.
    gclist contains 3 entries.
    gclist contains 53 entries.
    gclist contains 46 entries.
    gclist contains 42 entries.
    Before collecting for shutdown() for class LoadTest
    After collecting for shutdown() for class LoadTest

    As you can see, because collect( ) is running in the background, gclist never gets very large, even though thousands of objects are being allocated and abandoned.

    Some Things to Try

    Creating successful multithreaded programs can be quite challenging. One reason for this is the fact that multithreading requires that you think of programs in parallel rather than linear terms. Furthermore, at runtime, threads interact in ways that are often difficult to anticipate. Thus, you might be surprised (or even bewildered) by the actions of a multithreaded program. The best way to get good at multithreading is to play with it. Toward this end, here are some ideas that you might want to try.

    Try adding another list box to the thread control panel that lets the user adjust the priority class of the thread in addition to its priority value. Try adding various synchronization objects to the control panel that can be turned on or off under user control. This will let you experiment with different synchronization options.

    For the multithreaded garbage collector, try collecting garbage less often, such as when gclist reaches a certain size or after free memory drops to a predetermined point. Alternatively, you could use a waitable timer to activate garbage collection on a regular basis. Finally, you might want to experiment with the garbage collector’s priority class and settings to find which level is optimal for your use.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

    Buy this book now. This article is taken from chapter three of The Art of C++, written by Herbert Schildt (McGraw-Hill/Osborne, 2004; ISBN: 0072255129). Check it out at your favorite bookstore. Buy this book now.

    C++ ARTICLES

    - Large Numbers
    - Dijkstra`s Shunting Algorithm with STL and C...
    - Brief Introduction to the STL Containers
    - The Standard Template Library
    - Templates in C++
    - C++ Programmer Alerts
    - C++ Programming Tips
    - First Steps in (C) Programming, conclusion
    - First Steps in (C) Programming, continued
    - First Steps in (C) Programming, introduction
    - C++ Preprocessor: Always Assert Your Code Is...
    - C++ Preprocessor: The Code in the Middle
    - Programming in C
    - Temporary Variables: Runtime rvalue Detection
    - Temporary Variables: Chasing Temporaries Away







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway