How Cherno explain about Thread
How we could do multi-thread(parallel computating)
1 |
|
Here we want program to do two things at the same time.
- keep printing
Working....
- receive the
cin.get()
from user, once get something from the users_Finished = true
The behavior will be like this
However we might not want to let one single thread occupy whole CPU resource, we can use std::this_thread::sleep_for(1s)
.
1 |
|
The demo of code above.
Can notice that two std::this_thread::get_id()
are different, cuz they are on the different threads.
How we could implement multi-thread in Grasshopper
Like this tutorial shown, we can use
1 | System.Threading.Tasks.Parallel.For(0, loopCount, i =>{ { |
instead of using regular for-loop to achieve multi-thread.
unsafe problem and how to tackle it
This is the part I might not fully understand, and you are welcome to correct me. Thanks.
Seems simply use multi_thread with List
, like the example in tutorial, is not safe.
Means multi-thread cannot merge to the List in the order correctly. As a result, this cause some data lost.
How we can tackle this problem
- We can use
lock
syntax to ensure the action of List - We use regular
array
instead of List
Here are the codes.
Lock
1 | private void RunScript(List<Point3d> startPts, object y, ref object A) |
array
1 | private void RunScript(List<Point3d> startPts, object y, ref object A) |
Rhino Official Documents_General Overview
Rhino Official Documents_Developer
Overview
Grasshopper for Rhino6 allows you to develop mult-threaded components by way of the new IGH_TaskCapableComponent
interface. Benchmarks have shown that Grasshopper can run significantly faster when using multi-threaded components. Results may vary, as not all solutions can be compute in parallel
The Interface
When a component implement the IGH_TaskCapableComponent
interface, Grasshopper will notice and potentially call a full enumeration of SolveInstance
for the component twice in consecutive passes:
- The first pass is for collecting data and starting tasks to compute result
- The second pass is for using the results from the tasks to sets outputs.
https://discourse.mcneel.com/t/v6-feature-multi-threaded-gh-components/47049
I found that there are some Rhino official posts talk about mult-threaded
in grasshopper.
From User interface, if you see the component has two grey dots on its upper-left corner, means that component support mult-threaded computation(parallel computing
).
Still working on…. To be continue
(James_Ramsden_Multithreading)[http://james-ramsden.com/multithreading-a-foreach-loop-in-a-grasshopper-components-in-c/]