Changeset 44638


Ignore:
Timestamp:
Apr 20, 2008, 3:37:08 PM (10 years ago)
Author:
Peter Dimov
Message:

Factored out boost/detail/lightweight_thread.hpp.

Location:
trunk
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/libs/smart_ptr/test/shared_ptr_mt_test.cpp

    r27729 r44638  
    88#endif
    99
    10 //
    1110//  shared_ptr_mt_test.cpp - tests shared_ptr with multiple threads
    1211//
    1312//  Copyright (c) 2002 Peter Dimov and Multi Media Ltd.
     13//  Copyright (c) 2008 Peter Dimov
    1414//
    15 // Distributed under the Boost Software License, Version 1.0. (See
    16 // accompanying file LICENSE_1_0.txt or copy at
    17 // http://www.boost.org/LICENSE_1_0.txt)
    18 //
     15//  Distributed under the Boost Software License, Version 1.0.
     16//  See accompanying file LICENSE_1_0.txt or copy at
     17//  http://www.boost.org/LICENSE_1_0.txt
    1918
    2019#include <boost/shared_ptr.hpp>
     
    2221
    2322#include <vector>
    24 #include <memory>
    25 #include <stdexcept>
    2623
    2724#include <cstdio>
    2825#include <ctime>
    2926
    30 // 'portable' thread framework
    31 
    32 class abstract_thread
    33 {
    34 public:
    35 
    36     virtual ~abstract_thread() {}
    37     virtual void run() = 0;
    38 };
    39 
    40 #if !defined(BOOST_HAS_PTHREADS) && defined(BOOST_HAS_WINTHREADS)
    41 
    42 char const * title = "Using Windows threads";
    43 
    44 #include <windows.h>
    45 #include <process.h>
    46 
    47 typedef HANDLE pthread_t;
    48 
    49 unsigned __stdcall common_thread_routine(void * pv)
    50 {
    51     abstract_thread * pt = static_cast<abstract_thread *>(pv);
    52     pt->run();
    53     delete pt;
    54     return 0;
    55 }
    56 
    57 int pthread_create(pthread_t * thread, void const *, unsigned (__stdcall * start_routine) (void*), void* arg)
    58 {
    59     HANDLE h = (HANDLE)_beginthreadex(0, 0, start_routine, arg, 0, 0);
    60 
    61     if(h != 0)
    62     {
    63         *thread = h;
    64         return 0;
    65     }
    66     else
    67     {
    68         return 1; // return errno;
    69     }
    70 }
    71 
    72 int pthread_join(pthread_t thread, void ** /*value_ptr*/)
    73 {
    74     ::WaitForSingleObject(thread, INFINITE);
    75     ::CloseHandle(thread);
    76     return 0;
    77 }
    78 
    79 #else
    80 
    81 char const * title = "Using POSIX threads";
    82 
    83 #include <pthread.h>
    84 
    85 extern "C" void * common_thread_routine(void * pv)
    86 {
    87     abstract_thread * pt = static_cast<abstract_thread *>(pv);
    88     pt->run();
    89     delete pt;
    90     return 0;
    91 }
    92 
    93 #endif
    94 
    95 //
    96 
    97 template<class F> class thread: public abstract_thread
    98 {
    99 public:
    100 
    101     explicit thread(F f): f_(f)
    102     {
    103     }
    104 
    105     void run()
    106     {
    107         f_();
    108     }
    109 
    110 private:
    111 
    112     F f_;
    113 };
    114 
    115 template<class F> pthread_t createThread(F f)
    116 {
    117     std::auto_ptr<abstract_thread> p(new thread<F>(f));
    118 
    119     pthread_t r;
    120 
    121     if(pthread_create(&r, 0, common_thread_routine, p.get()) == 0)
    122     {
    123         p.release();
    124         return r;
    125     }
    126 
    127     throw std::runtime_error("createThread failed.");
    128 }
     27#include <boost/detail/lightweight_thread.hpp>
    12928
    13029//
     
    13231int const n = 1024 * 1024;
    13332
    134 void test(boost::shared_ptr<int> const & pi)
     33void test( boost::shared_ptr<int> const & pi )
    13534{
    13635    std::vector< boost::shared_ptr<int> > v;
    13736
    138     for(int i = 0; i < n; ++i)
     37    for( int i = 0; i < n; ++i )
    13938    {
    140         v.push_back(pi);
     39        v.push_back( pi );
    14140    }
    14241}
     
    14443int const m = 16; // threads
    14544
     45#if defined( BOOST_HAS_PTHREADS )
     46
     47char const * thmodel = "POSIX";
     48
     49#else
     50
     51char const * thmodel = "Windows";
     52
     53#endif
     54
    14655int main()
    14756{
    14857    using namespace std; // printf, clock_t, clock
    14958
    150     printf("%s: %d threads, %d iterations: ", title, m, n);
     59    printf( "Using %s threads: %d threads, %d iterations: ", thmodel, m, n );
    15160
    152     boost::shared_ptr<int> pi(new int(42));
     61    boost::shared_ptr<int> pi( new int(42) );
    15362
    15463    clock_t t = clock();
    15564
    156     pthread_t a[m];
     65    pthread_t a[ m ];
    15766
    158     for(int i = 0; i < m; ++i)
     67    for( int i = 0; i < m; ++i )
    15968    {
    160         a[i] = createThread( boost::bind(test, pi) );
     69        boost::detail::lw_thread_create( a[ i ], boost::bind( test, pi ) );
    16170    }
    16271
    163     for(int j = 0; j < m; ++j)
     72    for( int j = 0; j < m; ++j )
    16473    {
    165         pthread_join(a[j], 0);
     74        pthread_join( a[j], 0 );
    16675    }
    16776
    16877    t = clock() - t;
    16978
    170     printf("\n\n%.3f seconds.\n", static_cast<double>(t) / CLOCKS_PER_SEC);
     79    printf( "\n\n%.3f seconds.\n", static_cast<double>(t) / CLOCKS_PER_SEC );
    17180
    17281    return 0;
  • trunk/libs/smart_ptr/test/weak_ptr_mt_test.cpp

    r27938 r44638  
    88#endif
    99
    10 //
    1110//  weak_ptr_mt_test.cpp
    1211//
    1312//  Copyright (c) 2002 Peter Dimov and Multi Media Ltd.
    14 //  Copyright 2005 Peter Dimov
     13//  Copyright 2005, 2008 Peter Dimov
    1514//
    16 // Distributed under the Boost Software License, Version 1.0. (See
    17 // accompanying file LICENSE_1_0.txt or copy at
    18 // http://www.boost.org/LICENSE_1_0.txt)
    19 //
     15//  Distributed under the Boost Software License, Version 1.0.
     16//  See accompanying file LICENSE_1_0.txt or copy at
     17//  http://www.boost.org/LICENSE_1_0.txt
    2018
    2119#include <boost/shared_ptr.hpp>
     
    2422
    2523#include <vector>
    26 #include <memory>
    27 #include <stdexcept>
    2824
    2925#include <cstdio>
     
    3127#include <cstdlib>
    3228
    33 // 'portable' thread framework
    34 
    35 class abstract_thread
    36 {
    37 public:
    38 
    39     virtual ~abstract_thread() {}
    40     virtual void run() = 0;
    41 };
    42 
    43 #if !defined(BOOST_HAS_PTHREADS) && defined(BOOST_HAS_WINTHREADS)
    44 
    45 char const * title = "Using Windows threads";
    46 
    47 #include <windows.h>
    48 #include <process.h>
    49 
    50 typedef HANDLE pthread_t;
    51 
    52 unsigned __stdcall common_thread_routine(void * pv)
    53 {
    54     abstract_thread * pt = static_cast<abstract_thread *>(pv);
    55     pt->run();
    56     delete pt;
    57     return 0;
    58 }
    59 
    60 int pthread_create(pthread_t * thread, void const *, unsigned (__stdcall * start_routine) (void*), void* arg)
    61 {
    62     HANDLE h = (HANDLE)_beginthreadex(0, 0, start_routine, arg, 0, 0);
    63 
    64     if(h != 0)
    65     {
    66         *thread = h;
    67         return 0;
    68     }
    69     else
    70     {
    71         return 1; // return errno;
    72     }
    73 }
    74 
    75 int pthread_join(pthread_t thread, void ** /*value_ptr*/)
    76 {
    77     ::WaitForSingleObject(thread, INFINITE);
    78     ::CloseHandle(thread);
    79     return 0;
    80 }
    81 
    82 #else
    83 
    84 char const * title = "Using POSIX threads";
    85 
    86 #include <pthread.h>
    87 
    88 extern "C" void * common_thread_routine(void * pv)
    89 {
    90     abstract_thread * pt = static_cast<abstract_thread *>(pv);
    91     pt->run();
    92     delete pt;
    93     return 0;
    94 }
    95 
    96 #endif
    97 
    98 //
    99 
    100 template<class F> class thread: public abstract_thread
    101 {
    102 public:
    103 
    104     explicit thread(F f): f_(f)
    105     {
    106     }
    107 
    108     void run()
    109     {
    110         f_();
    111     }
    112 
    113 private:
    114 
    115     F f_;
    116 };
    117 
    118 template<class F> pthread_t createThread(F f)
    119 {
    120     std::auto_ptr<abstract_thread> p(new thread<F>(f));
    121 
    122     pthread_t r;
    123 
    124     if(pthread_create(&r, 0, common_thread_routine, p.get()) == 0)
    125     {
    126         p.release();
    127         return r;
    128     }
    129 
    130     throw std::runtime_error("createThread failed.");
    131 }
     29#include <boost/detail/lightweight_thread.hpp>
    13230
    13331//
     
    17876}
    17977
     78#if defined( BOOST_HAS_PTHREADS )
     79
     80char const * thmodel = "POSIX";
     81
     82#else
     83
     84char const * thmodel = "Windows";
     85
     86#endif
     87
    18088int main()
    18189{
    18290    using namespace std; // printf, clock_t, clock
    18391
    184     printf("%s: %d threads, %d * %d iterations: ", title, m, n, k );
     92    printf("Using %s threads: %d threads, %d * %d iterations: ", thmodel, m, n, k );
    18593
    18694    std::vector< boost::shared_ptr<int> > v( k );
     
    193101    clock_t t = clock();
    194102
    195     pthread_t a[m];
     103    pthread_t a[ m ];
    196104
    197     for(int i = 0; i < m; ++i)
     105    for( int i = 0; i < m; ++i )
    198106    {
    199         a[i] = createThread( boost::bind( test, v ) );
     107        boost::detail::lw_thread_create( a[ i ], boost::bind( test, v ) );
    200108    }
    201109
    202110    v.resize( 0 ); // kill original copies
    203111
    204     for(int j = 0; j < m; ++j)
     112    for( int j = 0; j < m; ++j )
    205113    {
    206114        pthread_join( a[j], 0 );
Note: See TracChangeset for help on using the changeset viewer.