Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C+;中线程计数的静态类变量+; 我在C++中编写了一个基于线程的应用程序。下面是显示如何检查线程计数的示例代码。我需要确保在任何时候,从我的应用程序生成的工作线程只有20个: #include<stdio.h> using namespace std; class ThreadWorkerClass { private: static int threadCount; public: void ThreadWorkerClass() { threadCount ++; } static int getThreadCount() { return threadCount; } void run() { /* The worker thread execution * logic is to be written here */ //Reduce count by 1 as worker thread would finish here threadCount --; } } int main() { while(1) { ThreadWorkerClass twObj; //Use Boost to start Worker Thread //Assume max 20 worker threads need to be spawned if(ThreadWorkerClass::getThreadCount() <= 20) boost::thread *wrkrThread = new boost::thread( &ThreadWorkerClass::run,&twObj); else break; } //Wait for the threads to join //Something like (*wrkrThread).join(); return 0; } #包括 使用名称空间std; 类threadworker类 { 私人: 静态int线程数; 公众: void threadworker类() { threadCount++; } 静态int getThreadCount() { 返回线程数; } 无效运行() { /*辅助线程执行 *逻辑要写在这里*/ //将计数减少1,因为辅助线程将在此处完成 线程数--; } } int main() { 而(1) { 螺纹加工类twObj; //使用Boost启动辅助线程 //假设最多需要生成20个工作线程 if(ThreadWorkerClass::getThreadCount()_C++_Multithreading_Boost_Static_Static Members - Fatal编程技术网

C+;中线程计数的静态类变量+; 我在C++中编写了一个基于线程的应用程序。下面是显示如何检查线程计数的示例代码。我需要确保在任何时候,从我的应用程序生成的工作线程只有20个: #include<stdio.h> using namespace std; class ThreadWorkerClass { private: static int threadCount; public: void ThreadWorkerClass() { threadCount ++; } static int getThreadCount() { return threadCount; } void run() { /* The worker thread execution * logic is to be written here */ //Reduce count by 1 as worker thread would finish here threadCount --; } } int main() { while(1) { ThreadWorkerClass twObj; //Use Boost to start Worker Thread //Assume max 20 worker threads need to be spawned if(ThreadWorkerClass::getThreadCount() <= 20) boost::thread *wrkrThread = new boost::thread( &ThreadWorkerClass::run,&twObj); else break; } //Wait for the threads to join //Something like (*wrkrThread).join(); return 0; } #包括 使用名称空间std; 类threadworker类 { 私人: 静态int线程数; 公众: void threadworker类() { threadCount++; } 静态int getThreadCount() { 返回线程数; } 无效运行() { /*辅助线程执行 *逻辑要写在这里*/ //将计数减少1,因为辅助线程将在此处完成 线程数--; } } int main() { 而(1) { 螺纹加工类twObj; //使用Boost启动辅助线程 //假设最多需要生成20个工作线程 if(ThreadWorkerClass::getThreadCount()

C+;中线程计数的静态类变量+; 我在C++中编写了一个基于线程的应用程序。下面是显示如何检查线程计数的示例代码。我需要确保在任何时候,从我的应用程序生成的工作线程只有20个: #include<stdio.h> using namespace std; class ThreadWorkerClass { private: static int threadCount; public: void ThreadWorkerClass() { threadCount ++; } static int getThreadCount() { return threadCount; } void run() { /* The worker thread execution * logic is to be written here */ //Reduce count by 1 as worker thread would finish here threadCount --; } } int main() { while(1) { ThreadWorkerClass twObj; //Use Boost to start Worker Thread //Assume max 20 worker threads need to be spawned if(ThreadWorkerClass::getThreadCount() <= 20) boost::thread *wrkrThread = new boost::thread( &ThreadWorkerClass::run,&twObj); else break; } //Wait for the threads to join //Something like (*wrkrThread).join(); return 0; } #包括 使用名称空间std; 类threadworker类 { 私人: 静态int线程数; 公众: void threadworker类() { threadCount++; } 静态int getThreadCount() { 返回线程数; } 无效运行() { /*辅助线程执行 *逻辑要写在这里*/ //将计数减少1,因为辅助线程将在此处完成 线程数--; } } int main() { 而(1) { 螺纹加工类twObj; //使用Boost启动辅助线程 //假设最多需要生成20个工作线程 if(ThreadWorkerClass::getThreadCount(),c++,multithreading,boost,static,static-members,C++,Multithreading,Boost,Static,Static Members,设计不够好。问题是您公开了构造函数,因此无论您喜欢与否,人们都可以创建任意数量的对象实例。您应该进行某种线程池。例如,您有一个类维护一组池,如果可用,它会提供线程。例如 class MyThreadClass { public: release(){ //the method obtaining that thread is reponsible for returning it } }; class ThreadPool { //create

设计不够好。问题是您公开了构造函数,因此无论您喜欢与否,人们都可以创建任意数量的对象实例。您应该进行某种线程池。例如,您有一个类维护一组池,如果可用,它会提供线程。例如

class MyThreadClass {
   public:
      release(){
        //the method obtaining that thread is reponsible for returning it
      }
};

class ThreadPool {
  //create 20 instances of your Threadclass
  public:
  //This is a blocking function
  MyThreadClass getInstance() {
     //if a thread from the pool is free give it, else wait
  }
};
因此,所有内容都由池类在内部进行维护。切勿将对该类的控制权交给其他类。您还可以向池类添加查询函数,如hasFreeThreads()、numFreeThreads()等

您还可以通过发出智能指针来增强这种设计,这样您就可以了解有多少人仍然拥有该线程。
让获得线程的人负责释放线程有时是危险的,因为进程崩溃,他们从不后退,对此有很多解决方案,最简单的一个是在每个线程上保持一个时钟,当时间用完时,线程被强行收回。

设计不够好。问题是如果你没有公开构造函数,那么不管你喜欢与否,人们都可以创建任意多的对象实例。你应该做一些线程池。例如,你有一个类维护一组池,如果可用,它会给出线程。比如

class MyThreadClass {
   public:
      release(){
        //the method obtaining that thread is reponsible for returning it
      }
};

class ThreadPool {
  //create 20 instances of your Threadclass
  public:
  //This is a blocking function
  MyThreadClass getInstance() {
     //if a thread from the pool is free give it, else wait
  }
};
因此,所有内容都由池类在内部进行维护。切勿将对该类的控制权交给其他类。您还可以向池类添加查询函数,如hasFreeThreads()、numFreeThreads()等

您还可以通过发出智能指针来增强这种设计,这样您就可以了解有多少人仍然拥有该线程。
让获取线程的人负责释放线程有时是危险的,因为进程崩溃,他们从不后退,对此有很多解决方案,最简单的方法是在每个线程上保持一个时钟,当时间用完时,线程将被强制收回。

void ThreadWorkerClass()=>这应该是构造函数吗?那么您只创建一个对象,线程数永远不会超过1。感谢@PermanentGuest编辑了代码。为什么不在
for
循环中创建20个线程,并将其保留在该循环中?@DavidSchwartz是的,除此之外,这也是我正在考虑的第二种方法签名。类似于有一个固定的线程池和一个提交任务/作业的公共队列。@AakashRoy-和David的评论/回答是最好的。如果你可以避免线程微管理,你肯定应该这样做。如果你不能避免线程微管理,请更改你的设计,直到你可以作废ThreadWorkerClass()=>这应该是构造函数吗?那么您只创建一个对象,线程数永远不会超过1。感谢@PermanentGuest编辑了代码。为什么不在
for
循环中创建20个线程,并将其保留在该循环中?@DavidSchwartz是的,除此之外,这也是我正在考虑的第二种方法签名。类似于有一个固定的线程池和一个提交任务/作业的公共队列。@AakashRoy-和David的评论/回答是最好的。如果你可以避免线程微管理,你肯定应该这样做。如果你不能避免线程微管理,请在你可以避免之前更改你的设计