System::Threading::ThreadPool क्लास

ThreadPool class

Thread pool API allowing it pushing jobs into queue to be read by pool of worker threads. This is a static type with no instance services. You should never create instances of it by any means.

class ThreadPool : public System::Object

विधियाँ

विधिविवरण
static GetAvailableThreads(int&, int&)उपलब्ध थ्रेड्स की संख्या प्राप्त करता है।
static GetInstance()RTTI जानकारी।
static GetMaxThreads(int&, int&)समकालिक थ्रेड्स की अधिकतम संख्या प्राप्त करता है।
static GetMinThreads(int&, int&)पूल द्वारा बनाए जा रहे थ्रेड्स की न्यूनतम संख्या प्राप्त करता है।
static JoinAllThreads()सभी स्वामित्व वाले थ्रेड्स को जॉइन करता है। अनंतकाल तक इंतजार करता है।
operator=(const ThreadPool&)कॉपी नहीं किया जा सकता।
static QueueUserWorkItem(WaitCallback)कार्य आइटम को कतार में रखता है जिसमें बिना पैरामीटर वाला कॉलबैक मौजूद है।
static QueueUserWorkItem(WaitCallback, const System::SharedPtr<System::Object>&)कार्य आइटम को कतार में रखता है जिसमें बिना पैरामीटर वाला कॉलबैक मौजूद है।
static SetMaxThreads(int, int)पूल द्वारा स्वामित्व वाले थ्रेड्स की संख्या सेट करता है।
static SetMinThreads(int, int)पूल द्वारा स्वामित्व वाले थ्रेड्स की न्यूनतम संख्या सेट करता है।
ThreadPool(const ThreadPool&)कॉपी नहीं किया जा सकता।

टिप्पणियाँ

#include "system/threading/thread_pool.h"
#include "system/threading/thread.h"
#include "system/object.h"
#include "system/smart_ptr.h"
#include <iostream>
#include <mutex>
#include <string>
#include <thread>

const std::string &BooleanToString(bool value)
{
  static const std::string True = "True";
  static const std::string False = "False";

  return value ? True : False;
}

int main()
{
  using namespace System::Threading;
  std::mutex m;

  const auto threadsCount = std::thread::hardware_concurrency();

  for (unsigned int i = 0; i < threadsCount; ++i)
  {
    ThreadPool::QueueUserWorkItem([&m](System::SharedPtr<System::Object> object) -> void {
      auto thread = Thread::get_CurrentThread();
      m.lock();
      std::cout << "Background: " << BooleanToString(thread->get_IsBackground()) <<
        ", Thread pool: " << BooleanToString(thread->get_IsThreadPoolThread()) <<
        ", Thread ID: " << thread->get_ManagedThreadId() << std::endl;
      m.unlock();
    });
  }

  ThreadPool::JoinAllThreads();

  return 0;
}
/*
This code example produces the following output:
Background: True, Thread pool: True, Thread ID: 1
Background: True, Thread pool: True, Thread ID: 3
Background: True, Thread pool: True, Thread ID: 5
Background: True, Thread pool: True, Thread ID: 6
Background: True, Thread pool: True, Thread ID: 9
Background: True, Thread pool: True, Thread ID: 1
Background: True, Thread pool: True, Thread ID: 7
Background: True, Thread pool: True, Thread ID: 2
Background: True, Thread pool: True, Thread ID: 4
Background: True, Thread pool: True, Thread ID: 3
Background: True, Thread pool: True, Thread ID: 12
Background: True, Thread pool: True, Thread ID: 8
Background: True, Thread pool: True, Thread ID: 5
Background: True, Thread pool: True, Thread ID: 6
Background: True, Thread pool: True, Thread ID: 16
Background: True, Thread pool: True, Thread ID: 11
*/

संबंधित देखें