~ubuntu-branches/debian/sid/boost1.49/sid

« back to all changes in this revision

Viewing changes to libs/thread/tutorial/counter.cpp

  • Committer: Package Import Robot
  • Author(s): Steve M. Robbins
  • Date: 2012-02-26 00:31:44 UTC
  • Revision ID: package-import@ubuntu.com-20120226003144-eaytp12cbf6ubpms
Tags: upstream-1.49.0
ImportĀ upstreamĀ versionĀ 1.49.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (C) 2001-2003
 
2
// William E. Kempf
 
3
//
 
4
//  Distributed under the Boost Software License, Version 1.0. (See accompanying 
 
5
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
 
6
 
 
7
#include <boost/thread/mutex.hpp>
 
8
#include <boost/thread/thread.hpp>
 
9
#include <iostream>
 
10
 
 
11
boost::mutex mutex;
 
12
int counter=0;
 
13
 
 
14
void change_count()
 
15
{
 
16
    boost::mutex::scoped_lock lock(mutex);
 
17
    int i = ++counter;
 
18
    std::cout << "count == " << i << std::endl;
 
19
}
 
20
 
 
21
int main()
 
22
{
 
23
    const int num_threads = 4;
 
24
    boost::thread_group thrds;
 
25
    for (int i=0; i < num_threads; ++i)
 
26
        thrds.create_thread(&change_count);
 
27
    thrds.join_all();
 
28
}