将变量提供给新任务 [英] Supplying variables to a new task

查看:82
本文介绍了将变量提供给新任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常使用以下基本结构来启动并行任务时我没有

需要等待结果



 私有  sub  yyy()

< span class =code-keyword> Dim SUB_VAR1 as string = xxx
Dim SUB_Var2 as integer = xxx

Dim T1 as new task( sub
Dim TASK_VAR1 as 字符串 = SUB_VAR1
Dim TASK_VAR2 as 整数 = SUB_VAR2
.. 。
... ' 在任务中做一些工作和TASK_Variable(s)
...
...
结束 Sub

T1.Start()

结束 sub





没问题 - 但这是我的问题



是否有保证在我开始任务之前,我的SUB_variable值总是被转移到Task_VARIABLES中或之后发生这种情况(我希望不会:-))......如果之后发生,Sub_variables可能不再存在,我会得到异常。 ..



PS我只是想了解这实际上是如何处理的



我曾尝试过:



这对我来说更像是一个通用的问题......

解决方案

答案很简单e:是。

和否。

和可能。



他们这一切都是真的...这一切都有点量......



线程并不简单,只是因为它本质上是非确定性的:你很少或根本无法控制事情发生的时间,因为系统会根据机器上的负载和可用于处理线程的内核数量来控制。

启动一个新线程并不意味着它立即运行,它将它添加到系统需要执行的任务的线程池中,并且当有一个免费核心并且它到达顶部时列表中,它将执行。但是......启动新任务的线程也是池的一部分,Windows是抢占式的(这意味着它将暂停正在运行的任务以给其他人一个机会)所以你的新线程可能会在Start方法返回之前运行完成原始线程,或者它可以与原始线程代码同时运行(在不同的核心上),或者它可以等待半个小时然后做它的事情。



除非你开始编码锁和互斥锁等,否则你无法控制使用哪个订单,以确保在你预期时发生。



所以...鉴于您的 SUB_xxx 值是 yyy 方法的本地值,当您的任务实际启动时,它们可能存在或不存在执行。



而不是依靠运气来获取你的价值,将它们作为参数传递给任务,你保证任务将处理的值: vb.net - Visual Basic .NET:如何使用参数从方法创建任务 - Stack Overflow [ ^ ]

I often use the following basic structure for starting a parallel task when I don't
need to wait for the result

Private sub yyy()
 
Dim SUB_VAR1  as  string = xxx
Dim SUB_Var2 as integer = xxx

Dim T1 as new task(sub)
 Dim TASK_VAR1 as String = SUB_VAR1
 Dim TASK_VAR2 as Integer = SUB_VAR2 
...
... 'do some work within the task and the TASK_Variable(s)
...
...
End Sub)

T1.Start()

End sub



No problem there - but here is my question

Is it guaranteed that my SUB_variable values ALWAYS get transferred into the Task_VARIABLES before I start the task or does this happen afterwards (I hope not :-)) ... If it would happen afterwards, the Sub_variables might not be present anymore and I would get exceptions ...

PS I'm just trying to understand how this actually get's processed

What I have tried:

This is more of a generic question for me...

解决方案

The answer is simple: "Yes".
And "No".
And "Maybe".

And they are all true at the same time ... it all goes a bit quantum ...

Threading is not simple, if only because it is inherently non-deterministic: you have little or no control over when things happen because the system controls that, subject to the load on the machine and the number of cores you have available to process threads.
Starting a new thread doesn't mean that it runs immediately, it adds it to a "thread pool" of tasks the system needs to do, and when there is a free core and it reaches the top of the list, it will execute. But ... the thread that started the new task is also part of a pool, and Windows is preemptive (which means it will suspend running tasks to give others a chance) so your new thread may run to completion before the Start method returns in the original thread, or it may run at the same time (on a different core) as the original thread code, or it may wait half an hour and then do it's thing.

And you have no significant control over which order is used unless you start coding locks and mutexes etc. to ensure things happen when you expect them to.

So ... given that your SUB_xxx values are local to the yyy method they could exist or not when your Task actually starts to execute.

Instead of relying on luck to get your values in, pass them as parameters to the task and you guarantee the values the Task will process: vb.net - Visual Basic .NET: how to create a task from a method with parameters - Stack Overflow[^]


这篇关于将变量提供给新任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆