Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by maikkel · Feb 03, 2012 at 01:37 AM · lockthreadsthread

Threads and variable lock

I'm looking into thread programming. I understand that you need to lock a variable so two different threads can't change a variable at the same time (and produce wrong values).

I have two threads: The main thread and a worker thread. The main thread executes at every Update(), the worker thread changes a variable at an interval of 200ms. The main thread only reads this variable, never writes to it. Do I still need to lock that variable?

I think not, because the main thread only reads it. Is that right?

I'm new to threads and I'm not sure if I completely understand them.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by rabbitfang · Feb 03, 2012 at 02:55 AM

You probably do not need locks in this case. However, you should retrieve a copy of the variable and use that copy exclusively in the main thread. If you didn't and you accessed the variable multiple times (within one Update() call), you might be reading different values, which can be problematic.

The only issue you might have is with that variable holding an object reference. If an object reference is used, you might have issues with the worker thread modifying the object after you have retrieved its reference. If the worker thread does not touch the object after assigning it to the variable, then you should be ok.

Multi-threading is a headache causing concept in programming, and is often considered an advanced concept because of this situation. I would advise reading up as much as you can about the topic. Unless you really need it, I would avoid using multithreading (use co-routines if you can).

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image maikkel · Feb 03, 2012 at 09:17 AM 0
Share

Thank you for the quick answer. Yes, I'll have to read much more about this. $$anonymous$$y game doesn't really need this at the moment, but I'm working on a very extendable Project and I don't quite know how large its going to become yet. And I read that including $$anonymous$$ulti-Threading into a finished Project is much harder than working $$anonymous$$ulti-Threaded from the beginning...

avatar image
1

Answer by steakPinball · Feb 03, 2012 at 03:05 AM

The worker thread could run at any time including while the main thread is running. Take the following situation.

  1. Line 1 of main thread executes.

  2. Main thread pauses.

  3. Entire worker thread executes.

  4. Main thread finishes.

This can occur for any line in the main thread.

For primitive types accessed only once in the main thread there shouldn't be a problem. Check for problems arising from the worker thread changing the variable while the main thread is running. Things to look for include the main thread accessing the variable more than once in the function, or an array changing size, or objects being destroyed.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Weird Threading Issue 0 Answers

Making calls to Spotify REST API blocks main thread 2 Answers

Thread ending prematurely? 2 Answers

How to spawn lots of objects without freezing the game 1 Answer

What does new Thread do, and how many threads are too many? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges