From dda9a3cd369726ebf88568f38c85bd4fc9db9094 Mon Sep 17 00:00:00 2001
From: murgatroid99 <mlumish@google.com>
Date: Thu, 14 May 2015 12:53:07 -0700
Subject: [PATCH] Made cache thread-safe

---
 src/objective-c/GRPCClient/private/GRPCChannel.m | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/src/objective-c/GRPCClient/private/GRPCChannel.m b/src/objective-c/GRPCClient/private/GRPCChannel.m
index e1aebdfe57..36f4c0aa5e 100644
--- a/src/objective-c/GRPCClient/private/GRPCChannel.m
+++ b/src/objective-c/GRPCClient/private/GRPCChannel.m
@@ -40,15 +40,15 @@
 
 @implementation GRPCChannel
 
-// TODO(mlumish): Investigate whether a cache with strong links is a good idea
-static NSMutableDictionary *channelCache;
-
 + (instancetype)channelToHost:(NSString *)host {
-  if (channelCache == nil) {
+  // TODO(mlumish): Investigate whether a cache with strong links is a good idea
+  static NSMutableDictionary *channelCache;
+  static dispatch_once_t cacheInitialization;
+  dispatch_once(&cacheInitialization, ^{
     channelCache = [NSMutableDictionary dictionary];
-  }
+  });
   GRPCChannel *channel = channelCache[host];
-  if (channel == nil) {
+  if (!channel) {
     channel = [[self alloc] initWithHost:host];
     channelCache[host] = channel;
   }
-- 
GitLab