Created
December 15, 2015 09:28
-
-
Save ted80/375593434d9edc097a0d 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 UnityEngine.UI; | |
using System; | |
using System.Threading; | |
using System.Collections; | |
using System.Diagnostics; | |
public class Main : MonoBehaviour | |
{ | |
public System.Object comlock; | |
public bool test = false; | |
public Thread thread; | |
private System.Object breakLock; | |
public bool breaker; | |
void Start () | |
{ | |
comlock = new System.Object(); | |
breakLock = new System.Object(); | |
breaker = false; | |
StartCoroutine("delayedStart"); | |
} | |
private IEnumerator delayedStart() | |
{ | |
yield return new WaitForSeconds(2f); | |
UnityEngine.Debug.Log ("start"); | |
test = true; | |
thread = new Thread(new ThreadStart(run)); | |
thread.IsBackground = true; | |
thread.Start (); | |
yield return new WaitForSeconds(2f); | |
test = false; | |
UnityEngine.Debug.Log ("end"); | |
} | |
void Update() | |
{ | |
if(test) | |
{ | |
long start = DateTime.Now.Ticks; | |
lock(comlock) | |
{ | |
} | |
long end = DateTime.Now.Ticks; | |
//UnityEngine.Debug.Log ("Took: " + (end - start)); | |
} | |
} | |
public void run() | |
{ | |
Stopwatch sw2 = new Stopwatch(); | |
while(true) | |
{ | |
long start = DateTime.Now.Ticks; | |
sw2.Reset(); | |
sw2.Start(); | |
lock(comlock) | |
{ | |
UnityEngine.Debug.Log ("Sending data"); | |
} | |
while(sw2.ElapsedTicks < 1000000L) { } | |
lock(breakLock) | |
{ | |
if(breaker) { break; } | |
} | |
long end = DateTime.Now.Ticks; | |
UnityEngine.Debug.Log ("Took: " + (end - start)); | |
} | |
} | |
void OnApplicationQuit() | |
{ | |
lock(breakLock) | |
{ | |
breaker = true; | |
thread = null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment