Skip to content

Instantly share code, notes, and snippets.

@ted80
Created December 15, 2015 09:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ted80/375593434d9edc097a0d to your computer and use it in GitHub Desktop.
Save ted80/375593434d9edc097a0d to your computer and use it in GitHub Desktop.
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