⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 thread.h

📁 Amis - A maximum entropy estimator 一个最大熵模型统计工具
💻 H
字号:
////////////////////////////////////////////////////////////////////////////  Copyright (c) 2000, Yusuke Miyao///  You may distribute under the terms of the Artistic License.//////  <id>$Id: Thread.h,v 1.3 2003/05/16 10:00:08 yusuke Exp $</id>///  <collection>Maximum Entropy Estimator</collection>///  <name>Thread.h</name>///  <overview>Interface for threads</overview>/////////////////////////////////////////////////////////////////////////#ifndef Amis_Thread_h_#define Amis_Thread_h_#ifdef AMIS_PARALLEL#include <amis/configure.h>#ifdef AMIS_SOLARIS_THREAD// sysconf to get the number of processors#include <unistd.h>#endif AMIS_NAMESPACE_BEGIN/// <classdef>/// <name>Thread</name>/// <overview>Interface for threads</overview>/// <desc>/// This class implements an interface for threads.  If you want to/// use a thread, implement a class with "run( arg )" function, and/// make a Thread object with the class as an initializer./// </desc>/// <body>//#ifdef SOLARIS_THREAD#if 0// Use solaris thread#include <thread.h>template < class Class, class Arg, class Ret >class Thread {private:  thread_t thread;  /// A structure for "pthread"  Class* object;  /// A class that implements "run" function  Arg argument;  /// An argument passed to the "run" function  Ret return_value;protected:  static void* thread_body( void* t ) {    static_cast< Thread* >( t )->run_internal();    return NULL;  }  /// Stub for calling "pthread_create"  void run_internal( void ) {    return_value = object->run( argument );  }  /// Invoking a "run" functionpublic:  Thread( Class* obj ) {    object = obj;  }  /// Initialize with an object with "run" function  ~Thread() {}  /// Destructor  void run( Arg arg ) {    argument = arg;    thr_create( NULL, NULL, thread_body, static_cast< void* >( this ), THR_BOUND|THR_NEW_LWP,&thread );  }  /// Run a thread with an argument  Ret wait( void ) {    thr_join( thread, NULL, NULL );    return return_value;  }};/// </body>/// </classdef>/// <classdef>/// <name>MutexLock</name>/// <overview>A lock for mutual exclusion</overview>/// <desc>/// This class implements a lock for mutual exclusion/// </desc>/// <body>class MutexLock {private:  mutex_t mutex;public:  MutexLock( void ) {    //mutex = PTHREAD_MUTEX_INITIALIZER;    mutex_init( &mutex, NULL, NULL );  }  ~MutexLock() {}  void acquire( void ) {    if ( mutex_lock( &mutex ) != 0 ) {      AMIS_ABORT( "pthread_mutex_lock failed" );    }  }  void release( void ) {    if ( mutex_unlock( &mutex ) != 0 ) {      AMIS_ABORT( "pthread_mutex_unlock failed" );    }  }};#else// Use pthread#include <pthread.h>class ThreadControl{public:  static void incrementConcurrency( int n ) {    static bool once = false;		    if( !once ) {#ifdef AMIS_SOLARIS_THREAD      int num_cpu = sysconf(_SC_NPROCESSORS_ONLN);      std::cerr << "Solaris Machine with " << num_cpu << " CPUs" << std::endl << std::flush;#endif      pthread_setconcurrency( pthread_getconcurrency() + n );      once = true;    }  }};template < class Class, class Arg, class Ret >class Thread {private:  pthread_t thread;  /// A structure for "pthread"  Class* object;  /// A class that implements "run" function  Arg argument;  /// An argument passed to the "run" function  Ret return_value;protected:  static void* thread_body( void* t ) {    static_cast< Thread* >( t )->run_internal();    return NULL;  }  /// Stub for calling "pthread_create"  void run_internal( void ) {    return_value = object->run( argument );  }  /// Invoking a "run" functionpublic:      Thread( Class* obj ) {    object = obj;  }  /// Initialize with an object with "run" function  ~Thread() {}  /// Destructor  void run( Arg arg ) {    argument = arg;	// increase the concurrency of the system. 	// This does not guarantee that the created thread is attached to a new CPU.	// See SOLARIS_THREAD 	pthread_attr_t attr;	pthread_attr_init( &attr );	pthread_attr_setscope( &attr, PTHREAD_SCOPE_SYSTEM ); // make a bounded thread    pthread_create( &thread, &attr, thread_body, static_cast< void* >( this ) );  }  /// Run a thread with an argument  Ret wait( void ) {    pthread_join( thread, NULL );    return return_value;  }};/// </body>/// </classdef>/// <classdef>/// <name>MutexLock</name>/// <overview>A lock for mutual exclusion</overview>/// <desc>/// This class implements a lock for mutual exclusion/// </desc>/// <body>class MutexLock {private:  pthread_mutex_t mutex;public:  MutexLock( void ) {    //mutex = PTHREAD_MUTEX_INITIALIZER;    pthread_mutex_init( &mutex, NULL );  }  ~MutexLock() {}  void acquire( void ) {    if ( pthread_mutex_lock( &mutex ) != 0 ) {      AMIS_ABORT( "pthread_mutex_lock failed" );    }  }  void release( void ) {    if ( pthread_mutex_unlock( &mutex ) != 0 ) {      AMIS_ABORT( "pthread_mutex_unlock failed" );    }  }};#endif/// </body>/// </classdef>AMIS_NAMESPACE_END#endif // AMIS_PARALLEL#endif // Thread_h_// end of Thread.h

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -