Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using System.Threading;
- using System.Linq;
- public class ThreadManager {
- public static ThreadManager Instance { get; private set; }
- Timer timer;
- Vector3 priorityPos = new Vector3();
- List<ChunkThread> runningThreads;
- Queue<ChunkThread> queue { get; set; }
- object threadLock;
- public int maxThreads;
- int mutex;
- static ThreadManager(){
- Instance = new ThreadManager();
- }
- public ThreadManager(){
- //lock
- Interlocked.Exchange (ref mutex, 1);
- //thread queue initialized
- queue = new Queue<ChunkThread> (16384);
- //running and idle threads lists initialized
- runningThreads = new List<ChunkThread>();
- //maxThreads = 4;
- //unlock
- Interlocked.Exchange (ref mutex, 0);
- this.timer = new Timer ((obj) => {
- ProcessThread ();}, null, 16, 16);
- }
- public void ProcessThread(){
- int mval = (int) Interlocked.CompareExchange (ref mutex, 0, 1);
- if (mval != 0)
- return;
- lock (queue) {
- if (queue.Count>0 && runningThreads.Count<maxThreads){
- ChunkThread thread = queue.Dequeue();
- runningThreads.Add(thread);
- if (thread != null){
- thread.Start();
- }
- }
- }
- }
- public void AddThread(ChunkThread t){
- lock (queue) {
- queue.Enqueue(t);
- }
- }
- public void RemoveThread(ChunkThread t){
- runningThreads.Remove(t);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement