Halcon多线程处理

Halcon中自带多线程处理算子,使用起来非常简单。下面就对线程算子进行简单的介绍。首先介绍用于多线程处理的两个算子,par_start 和par_join()。

Halcon的帮助文件中对par_start 是这样介绍的:

To start a new thread, prefix the corresponding operator or procedure call with the par_start qualifier:


par_start : gather_data()

This call starts the hypothetical procedure gather_data() as a new subthread in the background and continues to execute the subsequent program lines. The thread ID is returned in the variable ThreadID which must be specified in angle brackets. Note that par_start is not an actual operator but merely a qualifier that modifies the calling behavior. Therefore, it is not possible to select par_start in the operator window.


You can also start procedure or operator calls as a subthread from the operator window (see figure 8.3). To do this, open the section Advanced Parallelization Options at the bottom of the operator window, tick the check box and enter the name of the variable that will hold the thread ID. If you double-click on a program containing the par_start qualifier, the parallelization options will also be displayed in the operator window. For certain program lines (e.g., comments, declarations, loops, or assignments) par_start is not supported and the corresponding options will also not be available in the operator window. For a general description of the operator window see section “Operator Window” on page 336.

image.png

大致翻译一下,内容如下:

开始一个新的线程,需要将par_start添加在对应的算子或者处理过程的前面。

par_start : gather_data()

这里假设在后台调用函数gather_data()作为一个新的线程,并继续执行随后的程序。线程ID返回到给三角括号中的threadID这个变量。需要注意的是,par_start不是一个实际的处理算子,仅仅是确定调用的方式,因此无法在操作窗孔中选择par_start这个算子。

用户可以在操作窗口中以线程方式开启算子或者处理过程。方法如下,在操作窗口底部打开高级并行选项,勾选复选框,输入用于保存线程ID变量名称。如果在程序中双击包含par_start开头的程序,那么这个并行选项也回出现在操作窗口中。对于某些程序(例如注释/定义/循环或者赋值),par_start是并部支持的,在对应的操作窗口中也部支持相应的并行选项。关于操作窗口更多内容可以查看336页的“Operator Window"部分。

Halcon的帮助文件中对par_join是这样介绍的:

Name

par_join — Wait for subthreads that were started with the par_start qualifier.

Signature

par_join( : : ThreadID : )

Description

The par_join operator is used to wait in the calling procedure for all procedures or operators that have been started in separate subthreads by adding the par_start qualifier to the according program line. The subthreads to wait for are identified by their thread ids that are passed to the parameter ThreadID.

Attention: par_start is not an operator but a qualifier that is added at the begin of the program line that has to be executed in parallel to the calling procedure. The syntax is par_start : followed by the actual procedure or operator call.

Parameters

ThreadID (input_control) thread_id(-array) → (integer)

Ids of all subthreads to wait for.


翻译后内容如下:

名称

par_join等待以par_start开头的子线程。

签名

par_join( : : ThreadID : )

描述

par_join算子用于等待在对应的算子或者过程前添加par_start修饰符的独立线程。通过传递到函数ThreadID变量中的线程好来指定需要等待的子线程。

注意:par_start不是一个算子,只是将它添加在程序前表明是采用并行的方式来调用执行。具体语法是:par_start:实际需要执行的算子或过程。

参数

ThreadID(输入控制)

需要等待的子线程的ID


帮助文件中描述的很详尽,简单的说,par_start 算子执行就是开启并执行一个新的线程。假设,每一个线程完成一个检测项目,但是输出时需要将所有检测结果汇总,也就是说输出结果时要保证所有检测线程都完成,这就需要利用par_join算子来实现,将需要等待的子线程的ID传到这个算子中就可以,该算子会等待最后一个线程执行完,然后再向下进行。


首先对比一组利用线程读取图像的数据。利用线程读取4张图像,时间为0.0387812s。利用穿行的方式读取4张图像,耗时为0.12706s。线程方式较快。

image.png

再用下面一段程序说明上面所介绍的两个算子的实际作用。

1.上面代码中,开启4个线程,每个线程为等待1s,启动4个线程后,再等待1.5s,然后计算par_join函数的等待时间,发现为0.00003749s,近似于未等待。也就时说调用的四个线程已经执行完成了,par_join函数无需再次等待。

2.下面代码中,开启4个线程,然后计算par_join函数等待时间,耗时为1.0013s,也就是执行完四个线程的时间。

综上所有,虽然par_start和par_join通常为配套使用,但是par_start的执行与par_join没有关系。之所以着重强调这一点,是因为在工作中遇到多个人咨询类似的问题,很简答的算子,测试一下就可以了解并掌握。

image.png


另外,在halcon的算子窗口,也可以指定算子在新线程中运行,如下图:

image.png

选中后,会在算子或者自定义函数的前面添加一个修饰符
这样 这个函数就会在新的线程中执行了,ThreadID是该线程的ID号,之后如果需要对该线程做操作,都需要通过这个ID号,因此一定要注意保留。


水平有限,难免有错误和不足之处,恳请批评指正。


————————————————

版权声明:本文为CSDN博主「大_樱_桃」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/biggestcherry/article/details/90638307


本文出自勇哥的网站《少有人走的路》wwww.skcircle.com,转载请注明出处!讨论可扫码加群:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

会员中心
搜索
«    2024年3月    »
123
45678910
11121314151617
18192021222324
25262728293031
网站分类
标签列表
最新留言
    热门文章 | 热评文章 | 随机文章
文章归档
友情链接
  • 订阅本站的 RSS 2.0 新闻聚合
  • 扫描加本站机器视觉QQ群,验证答案为:halcon勇哥的机器视觉
  • 点击查阅微信群二维码
  • 扫描加勇哥的非标自动化群,验证答案:C#/C++/VB勇哥的非标自动化群
  • 扫描加站长微信:站长微信:abc496103864
  • 扫描加站长QQ:
  • 扫描赞赏本站:
  • 留言板:

Powered By Z-BlogPHP 1.7.2

Copyright Your skcircle.com Rights Reserved.

鄂ICP备18008319号


站长QQ:496103864 微信:abc496103864