DennisC
DennisC

Reputation: 133

API threads and static methods

I have an APIController handling post requests that is calling a static method in a static class that I want to return the next value in a static dictionary in the static class. For example a request comes in and I want to look up the next index to use for that dictionary and then increase that index by 1 in a static property value. This works perfectly fine when I send the calls 1 at a time, but when 2 calls come in simultaneously they both get the save value back from the static method.

What I am trying to do is find a way to lock the static method so it can process 1 thread at a time so no matter how many simultaneous calls come in the static method runs synchronously. Here is basically the code I am calling. If this gets called twice at the same time they both return the same value, which is not what I want.

public static class Manager
{
  public static Dictionary<string, bool> ServerList { get; set; }
  public static int NumberOfServers { get; set; }
  public static int NextServerIndex { get; set; }

  public static string GetNextServer()
  {
    int indextocheck = NextServerIndex;
    string servername= null;
    if (indextocheck >= NumberOfServers)
      indextocheck = 0;
    while (timer < maxtime)
    {
      if (ServerList.Values.ElementAt(indextocheck) == false)
      {
        servername = ServerList.Keys.ElementAt(indextocheck);
        ServerList[server] = true;
        NextServerIndex = indextocheck + 1;
        break;
      }
      indextocheck++;
      if (indextocheck == NumberOfServers)
        indextocheck = 0;
    }
    return servername
  }
}

Obviously I am missing something here as I thought the static method (in a static class) would be running synchronously. What can I do to accomplish what I am trying to do?

Thanks!

Upvotes: 2

Views: 514

Answers (1)

Athanasios Kataras
Athanasios Kataras

Reputation: 26432

You can use a lock

static object SpinLock = new object();

lock(SpinLock)
{
   //Statements
}

In your case

hat I am trying to do is find a way to lock the static method so it can process 1 thread at a time so no matter how many simultaneous calls come in the static method runs synchronously. Here is basically the code I am calling. If this gets called twice at the same time they both return the same value, which is not what I want.

public static class Manager
{
  static object SpinLock = new object();
  // Remove the public or protect the get set with a lock
  static Dictionary<string, bool> ServerList { get; set; }
  static int NumberOfServers { get; set; }
  static int NextServerIndex { get; set; }

  public static string GetNextServer()
  {
     lock(SpinLock)
     {

        int indextocheck = NextServerIndex;
        string servername= null;
        if (indextocheck >= NumberOfServers)
          indextocheck = 0;
        while (timer < maxtime)
        {
          if (ServerList.Values.ElementAt(indextocheck) == false)
          {
            servername = ServerList.Keys.ElementAt(indextocheck);
            ServerList[server] = true;
            NextServerIndex = indextocheck + 1;
            break;
          }
          indextocheck++;
          if (indextocheck == NumberOfServers)
              indextocheck = 0;
          }
     }
    return servername;
  }
}

Upvotes: 2

Related Questions