Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaced Queue with ConcurrentQueue. #58

Merged
merged 3 commits into from
Feb 1, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions TwitchLib.Unity/ThreadDispatcher.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Runtime.CompilerServices;
using UnityEngine;

Expand All @@ -9,7 +9,7 @@ public class ThreadDispatcher : MonoBehaviour
{
private static ThreadDispatcher _instance;

private static Queue<Action> _executionQueue = new Queue<Action>();
private static ConcurrentQueue<Action> _executionQueue = new ConcurrentQueue<Action>();

/// <summary>
/// Ensures a thread dispatcher is created if there is none.
Expand All @@ -33,10 +33,14 @@ public static void Enqueue(Action action)

private void Update()
{
//storing the count here instead of locking the queue so we don't end up with a deadlock when one of the actions queues another action
int count = _executionQueue.Count;
for (int i = 0; i < count; i++)
_executionQueue.Dequeue().Invoke();
while (!_executionQueue.IsEmpty)
{
Action action;
if (_executionQueue.TryDequeue(out action))
{
action.Invoke();
}
}
}

private static ThreadDispatcher CreateThreadDispatcherSingleton(string callerMemberName)
Expand Down