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 jjobby · Jul 14, 2010 at 02:07 AM · threads

Threads don't operate properly in every frame.

Edit: OK. It's just my mistake for not reset the variable.

I've tried to implement the multithreading. I'm still not familiar with it. This is the code. There is no error or warning so I think the code is fine.

private int _threadCounter = 0; private float[] _num; private Vector3 _vec01, _vec02;

IEnumerator Start () { Thread[] thread = new Thread[2]; _num = new float[threads];

 _vec01 = 22 * new Vector3(1, 0, 0);
 _vec02 = 33 * new Vector3(1, 0, 0);

 while (true)
 {
     for (int i = 0; i < threads; i++)
     {
         thread[i] = new Thread(TestMethod);

         thread[i].Start(i);
     }

     while (_threadCounter < 2) yield return null;

     print(_num[0] + " " + _num[1]);

     yield return null;
 }

}

private void TestMethod(object iteration) { int iter = (int)iteration;

 _num[iter] = iter + 10;

 Add100(ref _num[iter]);

 _num[iter] += Return1000();

 if (iter == 0)
     _num[iter] *= _vec01.magnitude; 
 else _num[iter] *= _vec02.magnitude;

 _threadCounter++;

}

private void Add100(ref float iter) { iter += 100; }

private int Return1000() { return 1000; }

I just want to play with return variable and some simple calculation. So the method may look non-sense. Please don't care about it. :) I've monitored the array variable _num in every frame. The system should print 24420 36663. The problem is that sometimes the second thread didn't calculate the entire method. The system printed 24420 1111 (or 111) which means it didn't do all calculation. It didn't happen often though. It might be just a fraction of seconds. But it did happen more often if I increased the magnitude of multiply vector. What did I do wrong here? What was the caused of this problem?

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

1 Reply

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

Answer by Mike 3 · Jul 14, 2010 at 02:18 AM

It's doing the calculation just fine - remember that it's threaded, it's often going to be half way through the function when you're checking the current results

If the issue is that your code should be waiting for both threads to finish before posting the results, then you'll need to reset the threadCounter variable to 0 at the top of your while loop, otherwise it'll skip it after the first iteration

One thing I'll add though - you should probably look into how to design threadsafe code, having two threads accessing the same data can be catastrophic later

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 jjobby · Jul 14, 2010 at 02:51 AM 0
Share

Arg! How could I forget to reset threadCounter variable. >_<

Big thanks to you!

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

No one has followed this question yet.

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

WorldToScreenPoint in Update/OnGUI: Bad Performance 1 Answer

Can I use Unity math function in other threads? 4 Answers

How to import the object from server to unity 2 Answers

Threads in EditorScript? 2 Answers


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