Jump to content
  • Advertisement
Sign in to follow this  
nbertoa

Is DirectXMath thread safe?

This topic is 740 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi community

I was looking for information about this in DirectXMath webpage and forums but I did not found anything.

Suppose I have a View Matrix of type DirectX::XMFLOAT4X4, and I spawn multiple threads and pass that variable as const reference. Is it thread safe to call DirectXMath functions to load that matrix in a XMMATRIX and apply multiple operations there, but store final result in a different XMFLOAT4X4?

I think the answer is yes but I want to be sure.

Share this post


Link to post
Share on other sites
Advertisement

As far as i was aware (no, i can't find definitive answers either) the DirectXMath functions have no intrinsic locks, and aren't gauranteed to be atomic in nature.

 

You should place your own thread locks etc around them, I would assume this is designed this way for performance reasons rather than forcing locks where they may not be needed (e.g. in a single threaded application).

Share this post


Link to post
Share on other sites
Ok. Thanks for the answers.

I think I should pass that matrix by value instead so each thread has its own copy and we avoid potential problems.

Share this post


Link to post
Share on other sites

Don't all of the DirectXMath functions operate on local by-value parameters and return by value? They don't know anything about threads or references, they're just functions operating on data. Anything YOU do with assigning or reading thread-shared variables is on you, and no different from anything else.


I think he was asking if any of the functions utilize static variables? For instance, we know strtok() in c/c++ isn't safe since it uses static buffers to keep track of parsing.

Share this post


Link to post
Share on other sites
To be honest I think it's safe to assume, without documentation to the contrary, that directxmath doesn't have anything like that.

It's 2016 and if code is going to do odd stuff like that it better be documented and there'd better be a damn good reason for it... My 2 cents worth.

Share this post


Link to post
Share on other sites

They're POD types. If you've externally synchronized them correctly*, then yes, it should be safe for multiple threads to share read-only access to a POD type.

 

*This means that there are appropriate barriers between the code that initializes the data, and the many threads that read the data. The simplest way to ensure this is correct is to wrap the initialization code in a mutex / critical section.

Edited by Hodgman

Share this post


Link to post
Share on other sites
@Hodgman:

Yes, I share the data once it is properly initialized.

I did several tests yesterday, reading different DirectXMath types by different threads, and there were no problems (crashes, data modified, etc)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!