none
Windows API中有没有线程池的函数 RRS feed

  • 问题

  • 工作线程和UI线程分开的好处众所周知,不会影响用户与界面交互。

    我们要处理的数据很多,用户随时从界面输入,不同的数据处理过程也不同。

    如果每提交一个作业就开一个线程会因新建线程而增加系统负担。

    线程池应该是个好办法,但我没在API函数中找到。

    用.net的ThreadPool?则要将整个工程编译成托管的。

    还有更好办法没有?

    2009年2月5日 7:55

答案

  •  Windows核心编程 第11章 线程池的使用

    11.1   方案1:异步调用函数 QueueUserWorkItem
    11.2   方案2:按规定的时间间隔调用函数 CreateTimerQueueTimer
    11.3   方案3:当单个内核对象变为已通知状态时调用函数 RegisterWaitForSingleObject
    11.4   方案4:当异步I / O请求完成运行时调用函数 BindIoCompletionCallback


    Jackie_0009 说:
    系统到底有没有用线程池呢?这和调用_beginthread有什么区别?什么时候用_beginthread什么时候用QueueUserWorkItem?



    下面是书中对QueueUserWorkItem的一些说明。
      你自己从来不调用CreateThread。系统会自动为你的进程创建一个线程池,线程池
    中的一个线程将调用你的函数。另外,当该线程处理完客户机的请求之后,该线程并不立即被
    撤消。它要返回线程池,这样它就可以准备处理已经排队的任何其他工作项目。你的应用程序
    的运行效率可能会变得更高,因为不必为每个客户机请求创建和撤消线程。
    另外,由于线程与
    完成端口相关联,因此可以同时运行的线程数量限制为 C P U数量的两倍。这就减少了线程的上
    下文转移的开销。
    2009年2月7日 8:27

全部回复

  • 你可以做一个队列,在工作线程空闲时从队列中取一个task,UI线程产生的task都放到队列里。

    2009年2月5日 10:13
    版主
  • CreateThreadpool
    CreateThreadpoolWork

    CallbackMayRunLong Indicates that the callback may not return quickly.
    CancelThreadpoolIo Cancels the notification from the StartThreadpoolIo function.
    CloseThreadpool Closes the specified thread pool.
    CloseThreadpoolCleanupGroup Closes the specified cleanup group.
    CloseThreadpoolCleanupGroupMembers Releases the members of the specified cleanup group, waits for all callback functions to complete, and optionally cancels any outstanding callback functions.
    CloseThreadpoolIo Releases the specified I/O completion object.
    CloseThreadpoolTimer Releases the specified timer object.
    CloseThreadpoolWait Releases the specified wait object.
    CloseThreadpoolWork Releases the specified work object.
    CreateThreadpool Allocates a new pool of threads to execute callbacks.
    CreateThreadpoolCleanupGroup Creates a cleanup group that applications can use to track one or more thread pool callbacks.
    CreateThreadpoolIo Creates a new I/O completion object.
    CreateThreadpoolTimer Creates a new timer object.
    CreateThreadpoolWait Creates a new wait object.
    CreateThreadpoolWork Creates a new work object.
    DestroyThreadpoolEnvironment Deletes the specified callback environment. Call this function when the callback environment is no longer needed for creating new thread pool objects.
    DisassociateCurrentThreadFromCallback Removes the association between the currently executing callback function and the object that initiated the callback. The current thread will no longer count as executing a callback on behalf of the object.
    FreeLibraryWhenCallbackReturns Specifies the DLL that the thread pool will unload when the current callback completes.
    InitializeThreadpoolEnvironment Initializes a callback environment.
    IsThreadpoolTimerSet Determines whether the specified timer object is currently set.
    LeaveCriticalSectionWhenCallbackReturns Specifies the critical section that the thread pool will release when the current callback completes.
    ReleaseMutexWhenCallbackReturns Specifies the mutex that the thread pool will release when the current callback completes.
    ReleaseSemaphoreWhenCallbackReturns Specifies the semaphore that the thread pool will release when the current callback completes.
    SetEventWhenCallbackReturns Specifies the event that the thread pool will set when the current callback completes.
    SetThreadpoolCallbackCleanupGroup Associates the specified cleanup group with the specified callback environment.
    SetThreadpoolCallbackLibrary Ensures that the specified DLL remains loaded as long as there are outstanding callbacks.
    SetThreadpoolCallbackPool Sets the thread pool to be used when generating callbacks.
    SetThreadpoolCallbackRunsLong Indicates that callbacks associated with this callback environment may not return quickly.
    SetThreadpoolThreadMaximum Sets the maximum number of threads that the specified thread pool can allocate to process callbacks.
    SetThreadpoolThreadMinimum Sets the minimum number of threads that the specified thread pool must make available to process callbacks.
    SetThreadpoolTimer Sets the timer object. A worker thread calls the timer object's callback after the specified timeout expires.
    SetThreadpoolWait Sets the wait object. A worker thread calls the wait object's callback function after the handle becomes signaled or after the specified timeout expires.
    StartThreadpoolIo Notifies the thread pool that I/O operations may possibly begin for the specified I/O completion object. A worker thread calls the I/O completion object's callback function after the operation completes on the file handle bound to this object.
    SubmitThreadpoolWork Posts a work object to the thread pool. A worker thread calls the work object's callback function.
    TrySubmitThreadpoolCallback Requests that a thread pool worker thread call the specified callback function.
    WaitForThreadpoolIoCallbacks Waits for outstanding I/O completion callbacks to complete and optionally cancels pending callbacks that have not yet started to execute.
    WaitForThreadpoolTimerCallbacks Waits for outstanding timer callbacks to complete and optionally cancels pending callbacks that have not yet started to execute.
    WaitForThreadpoolWaitCallbacks Waits for outstanding wait callbacks to complete and optionally cancels pending callbacks that have not yet started to execute.
    WaitForThreadpoolWorkCallbacks Waits for outstanding work callbacks to complete and optionally cancels pending callbacks that have not yet started to execute.

    2009年2月5日 15:20
  •  CreateThreadpool

    Requirements

    Client

    Requires Windows Vista.

    Server

    Requires Windows Server 2008.


    xp?
    2009年2月6日 3:21
  •  QueueUserWorkItem
    2009年2月6日 9:16
  • 我的Windows XP Home sp3的kernel32.dll中好像只有QueueUserWorkItem,没有一个导出函数有Threadpool字样,自己写了段代码测试QueueUserWorkItem,发现最后一个参数为WT_EXECUTEDEFAULT实际上是单线程顺序处理多个任务,若为WT_EXECUTELONGFUNCTION则为多线程。MSDN上说这个参数作用:

    The callback function can perform a long wait. This flag helps the system to decide if it should create a new thread.

    系统到底有没有用线程池呢?这和调用_beginthread有什么区别?什么时候用_beginthread什么时候用QueueUserWorkItem?

    #include <iostream>
    #include <windows.h>

    using namespace std;

    HANDLE ghSemaphore;

    DWORD WINAPI ThreadProc(LPVOID lpParameter)
    {
     cout<<"enter ThreadProc"<<endl;
     ::Sleep(5000);
     cout<<"leave ThreadProc"<<endl;
     ::ReleaseSemaphore(ghSemaphore, 1, NULL);
     return 0;
    }

    int main()
    {
     ghSemaphore=::CreateSemaphore(NULL, 0, 3, NULL);
     //!!批处理
     //::QueueUserWorkItem(ThreadProc, NULL, WT_EXECUTEDEFAULT);
     //::QueueUserWorkItem(ThreadProc, NULL, WT_EXECUTEDEFAULT);
     //::QueueUserWorkItem(ThreadProc, NULL, WT_EXECUTEDEFAULT);
     //!!多线程
     ::QueueUserWorkItem(ThreadProc, NULL, WT_EXECUTELONGFUNCTION);
     ::QueueUserWorkItem(ThreadProc, NULL, WT_EXECUTELONGFUNCTION);
     ::QueueUserWorkItem(ThreadProc, NULL, WT_EXECUTELONGFUNCTION);

     for(int i=0; i<3; ++i)
      ::WaitForSingleObject(ghSemaphore, INFINITE);
    }

    2009年2月7日 1:32
  •  Windows核心编程 第11章 线程池的使用

    11.1   方案1:异步调用函数 QueueUserWorkItem
    11.2   方案2:按规定的时间间隔调用函数 CreateTimerQueueTimer
    11.3   方案3:当单个内核对象变为已通知状态时调用函数 RegisterWaitForSingleObject
    11.4   方案4:当异步I / O请求完成运行时调用函数 BindIoCompletionCallback


    Jackie_0009 说:
    系统到底有没有用线程池呢?这和调用_beginthread有什么区别?什么时候用_beginthread什么时候用QueueUserWorkItem?



    下面是书中对QueueUserWorkItem的一些说明。
      你自己从来不调用CreateThread。系统会自动为你的进程创建一个线程池,线程池
    中的一个线程将调用你的函数。另外,当该线程处理完客户机的请求之后,该线程并不立即被
    撤消。它要返回线程池,这样它就可以准备处理已经排队的任何其他工作项目。你的应用程序
    的运行效率可能会变得更高,因为不必为每个客户机请求创建和撤消线程。
    另外,由于线程与
    完成端口相关联,因此可以同时运行的线程数量限制为 C P U数量的两倍。这就减少了线程的上
    下文转移的开销。
    2009年2月7日 8:27
  • 是不是QueueUserWorkItem可以取代_beginThread?
    2009年2月8日 2:07
  • Jackie_0009 说:

    是不是QueueUserWorkItem可以取代_beginThread?



    在你要频繁创建线程时用QueueUserWorkItem会比较好,当然如果用Intel TBB会更好
    2009年2月8日 3:21