wo80 / Triangle.NET

C# / .NET version of Jonathan Shewchuk's Triangle mesh generator.
463 stars 85 forks source link

Triangle.NET is not thread-safe #19

Closed timoria21 closed 2 years ago

timoria21 commented 2 years ago

I'm trying to use this library inside a Parallel.For loop and discovered that it's not thread-safe. I'm trying to triangulate a large number of contours faster. I first thought about providing seeds to Random class constructors but it didn't help.

Did somebody succeed in making this library thread-safe? Is it somewhat possible?

Do you have any suggestions on where to start?

Thank you.

wo80 commented 2 years ago

Those contours represent independent geometries, not directly related to each other? If that's the case, you could try to implement it along the lines of this example.

wo80 commented 2 years ago

I just checked the usage of Random. It's only used in TriangleSampler, which is used in TriangleLocator, which again is heavily used by ConstraintMesher. This usage is not thread-safe, so there still might be a problem.

Unfortunately, the Random.Shared instance is only available as of .NET 6. Maybe it would be worth adding our own implementation, like this one https://stackoverflow.com/questions/3049467/is-c-sharp-random-number-generator-thread-safe

Or, alternatively, provide a shared instance through the Configuration class, which then can be set to use Random.Shared in the user code.

timoria21 commented 2 years ago

Wow, thanks. Will try the example first.