Interthread communication


The following question is about the unity game engine, but it could relate to any program trying to send data to a main thread, such as the UI thread.

I am processing some data on a separate thread (position data a read asyncrously from a socket). However, I need to act on this data on the main thread (a game object's transform can only be accessed from the main thread). The approach I have in mind is to create a thread-safe queue and follow the producer-consumer pattern. The thread would queue the position data and the main thread would deque the data and act on it. *Note: In Unity I do not have access to the System.Windows.Threading name space so I can not use Dispatcher. Also, it requires .Net 3.5 so I can't use the Collections.Concurrent name space either.

Is there a better approach?

If there isn't, what is the best way to inform the main thread when data is queued? It seems inefficient to poll, but I can't think of any way around it..

Thanks in advance.


That is a totally viable approach to threading. As you probably know, the alternative to polling found in computer hardware is the concept of interrupts.

How would you simulate interrupts in a multithreaded high-level computer program? Hard to say - your thread that changes would have to notify the UI thread "hey I'm ready", rather than the UI thread checking constantly. This requires some sort of message passing, really - it may not be feasible.

That being said, the typical game-design approach is the "game loop" that does, essentially, poll. So there is no shame in that game - you just have to make sure it doesn't murder your performance.


May be <a href="https://stackoverflow.com/questions/22356/cleanest-way-to-invoke-cross-thread-events" rel="nofollow">this question</a> has an answer for you.

However, polling a queue is a cleaner solution IMHO, and not so costly if done right and there are tons of examples in the Internet.


  • Conduit - Combining multiple Sources/Producers into one
  • How do you rotate the numbers in an numpy array of shape (n,) or (n,1)?
  • Why toDataURL Does Not Get Canvas Content on Mobile?
  • SimpleXML PHP parse OCR XML document
  • WooCommerce REST Client API - Programmatically get consumer key and secret
  • While using ConcurrentQueue, trying to dequeue while looping through in parallel
  • How does Azure's EventData.PartitionKey decide which partition to write to?
  • Task processing status with a message queue
  • My Produce Consumer Hangs
  • Interface with multiple implementations in ninject
  • is uninitialized_copy/fill(In first, In last, For dest, A &a) an oversight in the c++ standard?
  • ILMerge: Rename internalized assemblies' namespaces?
  • Type mismatch: cannot convert from ListFragment to Fragment
  • Error “Reflect.defineMetadata” while trying to load a transient web worker
  • replacing while loop with list comprehension
  • CSS Grid, position absolute an element in a css grid item: IMPOSSIBLE
  • Does the Azure table storage API cache results?
  • Spring Integration Bridge with poller not working as expected for JMS
  • C++ Single function pointer for all template instances
  • Can I have a variable number of URI parameters or key-value pairs in Laravel 4?
  • vectorized indexing/slicing in numpy/scipy?
  • How to make Twilio api Post request with the help of AFNetworking?
  • DIV instruction jumping to random location?
  • Why use database factory in asp.net mvc?
  • Scala multiline string placeholder
  • Test if a set exists before trying to drop it
  • Django: Count of Group Elements
  • QLineEdit password safety
  • Menu Color Fade on Hover with Jquery
  • WinForms: two way TextBox problem
  • Validaiting emails with Net.Mail MailAddress
  • Which linear programming package should I use for high numbers of constraints and “warm starts” [clo
  • Javascript + PHP Encryption with pidCrypt
  • R: gsub and capture
  • Comma separated Values
  • Error creating VM instance in Google Compute Engine
  • Hits per day in Google Big Query
  • What are the advantages and disadvantages of reading an entire file into a single String as opposed
  • how does django model after text[] in postgresql [duplicate]
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)