C++ Programming/Libraries/Boost

< C++ Programming < Libraries

Boost Library

The Boost library (http://www.boost.org/) provides free peer-reviewed , open source libraries that extend the functionality of C++. Most of the libraries are licensed under the Boost Software License, designed to allow Boost to be used with both open and closed source projects.

Many of Boost's founders are on the C++ standard committee and several Boost libraries have been accepted for incorporation into the Technical Report 1 of C++0x. Although Boost was begun by members of the C++ Standards Committee Library Working Group, participation has expanded to include thousands of programmers from the C++ community at large.

The emphasis is on libraries which work well with the C++ Standard Library. The libraries are aimed at a wide range of C++ users and application domains, and are in regular use by thousands of programmers. They range from general-purpose libraries like SmartPtr, to OS Abstractions like FileSystem, to libraries primarily aimed at other library developers and advanced C++ users, like MPL.

A further goal is to establish "existing practice" and provide reference implementations so that Boost libraries are suitable for eventual standardization. Ten Boost libraries will be included in the C++ Standards Committee's upcoming C++ Standard Library Technical Report as a step toward becoming part of a future C++ Standard.

In order to ensure efficiency and flexibility, Boost makes extensive use of templates. Boost has been a source of extensive work and research into generic programming and metaprogramming in C++.

extension libraries

The current Boost release contains 87 individual libraries, including the following three:

noncopyable

The boost::noncopyable utility class that ensures that objects of a class are never copied.

class C : boost::noncopyable
{
  ...
};

Linear algebra – uBLAS

Boost includes the uBLAS linear algebra library, with BLAS support for vectors and matrices. uBlas supports a wide range of linear algebra operations, and has bindings to some widely used numerics libraries, such as ATLAS, BLAS and LAPACK.

#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>
#include <iostream>

using namespace boost::numeric::ublas;

/* "y = Ax" example */
int main () 
{
      vector<double> x(2);
      x(0) = 1; x(1) = 2;
 
      matrix<double> A(2,2);
      A(0,0) = 0; A(0,1) = 1;
      A(1,0) = 2; A(1,1) = 3;

      vector<double> y = prod(A, x);

      std::cout << y << std::endl;
      return 0;
}

Generating random numbers – Boost.Random

Boost provides distribution-independent pseudorandom number generators and PRNG-independent probability distributions, which are combined to build a concrete generator.

#include <boost/random.hpp>
#include <ctime>

using namespace boost;

double SampleNormal (double mean, double sigma)
{
    // Create a Mersenne twister random number generator
    // that is seeded once with #seconds since 1970
    static mt19937 rng(static_cast<unsigned> (std::time(0)));

    // select Gaussian probability distribution
    normal_distribution<double> norm_dist(mean, sigma);

    // bind random number generator to distribution, forming a function
    variate_generator<mt19937&, normal_distribution<double> >  normal_sampler(rng, norm_dist);

    // sample from the distribution
    return normal_sampler();
}

See Boost Random Number Library for more details.

Multi-threading – Boost.Thread

Example code that demonstrates creation of threads:

#include <boost/thread/thread.hpp>
#include <iostream>

using namespace std; 

void hello_world() 
{
  cout << "Hello world, I'm a thread!" << endl;
}

int main(int argc, char* argv[]) 
{
  // start two new threads that calls the "hello_world" function
  boost::thread my_thread1(&hello_world);
  boost::thread my_thread2(&hello_world);

  // wait for both threads to finish
  my_thread1.join();
  my_thread2.join();
  
  return 0;
}

See also Threading with Boost - Part I: Creating Threads

Thread locking

Example usage of a mutex to enforce exclusive access to a function:

#include <iostream>
#include <boost/thread.hpp>

void locked_function ()
{
    // function access mutex
    static boost::mutex m;
    // wait for mutex lock
    boost::mutex::scoped_lock lock(m);

    // critical section
    // TODO: Do something

    // auto-unlock on return
}

int main (int argc, char* argv[]) 
{
    locked_function();
    return 0;
}

Example of read/write locking of a property:

#include <iostream>
#include <boost/thread.hpp>

/** General class for thread-safe properties of any type. */
template <class T>
class lock_prop : boost::noncopyable {
public:
    lock_prop () {}

    /** Set property value. */
    void operator = (const T & v) {
        // wait for exclusive write access
        boost::unique_lock<boost::shared_mutex> lock(mutex);

        value = v;
    }

    /** Get property value. */
    T operator () () const {
        // wait for shared read access
        boost::shared_lock<boost::shared_mutex> lock(mutex);

        return value;
    }

private:
    /// Property value.
    T                           value;
    /// Mutex to restrict access
    mutable boost::shared_mutex mutex;
};

int main () {
    // read/write locking property
    lock_prop<int> p1;
    p1 = 10;
    int a = p1();

    return 0;
}


This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.