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 Olivarra1 · Jan 22, 2014 at 02:23 PM · webplayerthreads

Thread semaphore in webplayer

I need to use a semaphore for limiting a class resource.

The thing is that my game sometimes needs to more than 20 web calls to an API. If I don't create a separate thread for each one, the client looks locked for ~5 seconds until all calls are made.

After making one thread for each call, the problem I had then is that in the extreme case I have to send ~50 API calls, I get an exception of too many sockets open, and that's when I decided to use a semaphore.

This semaphore is inited to 10, and everytime a thread wants to make an API call waits for it, and then it releases it. In this way, I have at most 10 API calls in the same time.

Now I tested it in the webplayer (which will be the release platform) and I get a security exception for using semaphores. Anyone knows a clean way to solve this? [something close to a semaphore...]

Dirty hacks I thought was to make a single thread with a queue on it, and this thread is responsible of processing his own queue by sending the calls 1 at a time (but that'd be slow, and I have to handle a lock for the queue).

And the other it's similar to this. Split all the API calls that needs to be made to 10 different threads. The problem is that if they haven't finished, and I have to send them again, then i'd have 20 threads again. And also, having multiple queues with multiple locks it's a call for deadlocks.

So.. any thread-safe non-blocking solution?

Comment
Add comment · Show 2
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 Olivarra1 · Jan 22, 2014 at 03:09 PM 0
Share

Now i'm trying to implement my own semaphore by using the class $$anonymous$$onitor. Have to test if it's available in the webplayer.

avatar image Jamora · Jan 22, 2014 at 05:08 PM 0
Share

When doing multithreaded stuff and there's a limited resource, I would think it's better to have only one (or as many as the resource allows) thread access the resource. At least to me, having one thread access a resource is more logical than having a hundred threads compete over access to that resource...

This could be achieved with e.g. a thread-safe queue of jobs to perform on the resource that all other threads write to and only one worker reads. The worker reads the next job in the queue, performs it on the resource and returns a result-object. Easy enough (I would imagine) to do with delegates and databases, where the result is always a DatabaseTable or some such.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Olivarra1 · Jan 22, 2014 at 03:29 PM

My solution: implement my own semaphore, that I think it's clean. Not sure it's completely safe, so use it at your own risk.

It's a shame though that they don't let you use semaphores "Because of security" and I have to end up implmenting my own semaphore, that's most probably to be thread unsafe. The chances of having a deadlock here are way greater than using the standard's C# library. Oh well...

 private object _lock;
 private int max;
 private int count;
 
 public MySemaphore(int maximumCount)
 {
     _lock = new object();
     max = maximumCount;
     count = 0;
 }
 
 public void WaitOne()
 {
     bool green = false;
 
     bool haveLock = false;
     try
     {
         Monitor.Enter(_lock); // Get the lock
         haveLock = true; // Monitor.Enter should have a 2nd parameter to make this operation atomic. Dammit unity...
         while(!green)
         {
             if(count < max)
             {
                 count++;
                 green = true;
             }else if(count == max){
                 Monitor.Wait(_lock); // Release the lock and wait for a change. Then reaquire the lock
             }else{
                 Debug.LogWarning("Semaphore broken."); // This should not be called. If it is, the semaphore is broken. Threading is hard...
                 Monitor.Wait(_lock);
             }
         }
     } finally {
         if(haveLock) Monitor.Exit(_lock); // Release the lock
     }
 }

 public void Release()
 {
     bool haveLock = false;
     try
     {
         Monitor.Enter(_lock); // Get the lock
         haveLock = true;
         count--;
         Monitor.PulseAll(_lock); // Send a signal to everyone waiting that the count has been decreased.
     } finally {
         if(haveLock) Monitor.Exit(_lock); // Release the lock
     }
 }
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

19 People are following this question.

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

Related Questions

Why can't I build Web Player in Unity while I have no problems with building standalone versions? 2 Answers

Focusing the UnityPlayer 1 Answer

WebPlayer Log not found 1 Answer

Adding extensions to (Compiled) games? 1 Answer

Unity web player game 0 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