Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/thread/tutorial/counter.cpp @ 12

Last change on this file since 12 was 12, checked in by landauf, 17 years ago

added boost

File size: 976 bytes
Line 
1// Copyright (C) 2001-2003
2// William E. Kempf
3//
4// Permission to use, copy, modify, distribute and sell this software
5// and its documentation for any purpose is hereby granted without fee,
6// provided that the above copyright notice appear in all copies and
7// that both that copyright notice and this permission notice appear
8// in supporting documentation.  William E. Kempf makes no representations
9// about the suitability of this software for any purpose.
10// It is provided "as is" without express or implied warranty.
11
12#include <boost/thread/mutex.hpp>
13#include <boost/thread/thread.hpp>
14#include <iostream>
15
16boost::mutex mutex;
17int counter=0;
18
19void change_count()
20{
21    boost::mutex::scoped_lock lock(mutex);
22    int i = ++counter;
23    std::cout << "count == " << i << std::endl;
24}
25
26int main()
27{
28    const int num_threads = 4;
29    boost::thread_group thrds;
30    for (int i=0; i < num_threads; ++i)
31        thrds.create_thread(&change_count);
32    thrds.join_all();
33}
Note: See TracBrowser for help on using the repository browser.