CVI线程池简介

线程池的目的是处理一组线程的创建和缓存,这减少了程序花费在创建和销毁线程上的时间。线程池在它所管理的一个线程中运行你的函数(称为线程函数)。而不是从程序的主线程调用函数或创建一个新线程来执行函数,可以使用线程池来调度线程函数,根据调度函数时线程池的配置和当前状态,线程池将创建一个新线程来运行函数,使用现有空闲线程来运行函数,或者等待活动线程完成并使用它来运行函数。

CVI提供了一个默认的线程池。不能为默认线程池指定回调或设置其属性

默认线程池的配置如下:

ATTR_TP_THREAD_STACK_SIZE: 0 (the size is set to the size of the creating thread's stack)

ATTR_TP_THREAD_SECURITY: NULL (配置NULL则子线程不能继承线程句柄)

ATTR_TP_PROCESS_EVENTS_WHILE_WAITING: TRUE(在创建之间可以设置为false)

ATTR_TP_THREAD_PRIORITY: THREAD_PRIORITY_NORMAL

ATTR_TP_MAX_NUM_THREADS: 2 + (2 * (处理器数量))

  • 运行线程池的函数

int CmtScheduleThreadPoolFunction (CmtThreadPoolHandle poolHandle, CmtThreadFunctionPtr threadFunction, void *threadFunctionData, CmtThreadFunctionID *threadFunctionID);

如果线程池中存在空闲线程,则使用该线程执行函数。如果没有空闲线程存在,并且线程池的最大线程数还没有达到,那么线程池将创建一个新线程并在其中执行函数。如果已经达到最大线程数,并且没有空闲线程存在,那么当一个线程可用时,池将执行该函数。以这种方式等待执行的函数将按照它们被调度的顺序被分配线程。

如果你没有为threadFunctionID参数传递NULL,你必须调用CmtReleaseThreadPoolFunctionID来释放线程函数ID

Parameters

Input

Name

Type

Description

poolHandle

CmtThreadPoolHandle

  • PassDEFAULT_THREAD_POOL_HANDLE to specify the default thread pool.

threadFunction

CmtThreadFunctionPtr

  • The function to schedule for execution. The function must have the following prototype:

int CVICALLBACK ThreadFunction (void *functionData);

threadFunctionData

void *

  • The value that you want the thread pool to pass to the Thread Function as the function data parameter.

  • 不要在函数执行时传递局部变量或者其他可能无效的变量的地址。

(输出)threadFunctionID

CmtThreadFunctionID

Return Value

cmtStatus

int

The CmtStatus code that the function call returns. This function returns 0 to indicate success and negative values to indicate failure. Pass the CmtStatus code to CmtGetErrorMessage to get a description of the error code

  • 示例

int CVICALLBACK DataAcqThreadFunction (void *functionData);

int main(int argc, char *argv[])

{

int panelHandle;

int functionId;

if (InitCVIRTE (0, argv, 0) == 0)

return -1; /* out of memory */

if ((panelHandle = LoadPanel(0, "DAQDisplay.uir", PANEL)) < 0)

return -1;

DisplayPanel (panelHandle);

CmtScheduleThreadPoolFunction (DEFAULT_THREAD_POOL_HANDLE, DataAcqThreadFunction, NULL, &functionId);

RunUserInterface ();

DiscardPanel (panelHandle);

CmtWaitForThreadPoolFunctionCompletion (DEFAULT_THREAD_POOL_HANDLE, functionId, 0);

return 0;

}

int CVICALLBACK DataAcqThreadFunction (void *functionData)

{

while (!quit) {

Acquire(. . .);

Analyze(. . .);

}

return 0;

}

  • 在上面的代码中,对CmtScheduleThreadPoolFunction的主函数调用导致线程池创建一个新线程来执行DataAcqThreadFunction线程函数。主线程从CmtScheduleThreadPoolFunction返回,而不等待DataAcqThreadFunction函数完成。当主线程执行主函数中的调用时,DataAcqThreadFunction函数在辅助线程中执行。

  • CmtScheduleThreadPoolFunction的第一个参数表示要调度该函数的线程池。LabWindows/CVI实用程序库包含一个内置的默认线程池。您传递常量DEFAULT_THREAD_POOL_HANDLE来指示您想要使用默认线程池。不能自定义默认线程池的行为。您可以调用CmtNewThreadPool来创建一个可定制的线程池。CmtNewThreadPool返回一个线程池句柄,您可以将第一个参数传递给CmtScheduleThreadPoolFunction。你必须调用CmtDiscardThreadPool来释放

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值