Created
October 15, 2015 12:34
-
-
Save ted80/b7638beb30f366477d51 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System; | |
using System.Threading; | |
using System.Collections; | |
public class Test : MonoBehaviour | |
{ | |
private Queue queueCalc; | |
private Queue queueMain; | |
private System.Random random; | |
void Awake () | |
{ | |
random = new System.Random (); | |
queueCalc = Queue.Synchronized(new Queue()); | |
queueMain = Queue.Synchronized(new Queue()); | |
/* | |
Thread thread = new Thread (new ThreadStart (run)); | |
thread.IsBackground = true; | |
thread.Start (); | |
*/ | |
} | |
void Update () | |
{ | |
/* | |
queueCalc.Enqueue (new Calculation(random.Next(1000), random.Next(1000))); | |
int i = 0; | |
while(queueMain.Count > 0) | |
{ | |
i++; | |
queueMain.Dequeue(); | |
} | |
Debug.Log (i); | |
*/ | |
} | |
public void run() | |
{ | |
while(true) | |
{ | |
if(queueCalc.Count > 0) | |
{ | |
Calculation c = (Calculation)queueCalc.Dequeue(); | |
c.c = c.a + c.b; | |
queueMain.Enqueue(c); | |
} | |
else | |
{ | |
Thread.Sleep(1); | |
} | |
} | |
} | |
} | |
class Calculation | |
{ | |
public int a; | |
public int b; | |
public int c; | |
public Calculation(int _a, int _b) | |
{ | |
a = _a; | |
b = _b; | |
c = 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://code.msdn.microsoft.com/windowsdesktop/Producer-Consumer-inter-aeaa9578