This is an automated email from the ASF dual-hosted git repository.

wwbmmm pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brpc.git


The following commit(s) were added to refs/heads/master by this push:
     new 5c034141 Remove redundant const of ThreadLocal (#2855)
5c034141 is described below

commit 5c0341410c1ea77210337df7c42bb99fe0ce3fe5
Author: Bright Chen <chenguangmin...@foxmail.com>
AuthorDate: Tue Dec 31 15:32:45 2024 +0800

    Remove redundant const of ThreadLocal (#2855)
---
 src/butil/thread_key.h       |  4 ++--
 test/thread_key_unittest.cpp | 15 +++++++++++----
 2 files changed, 13 insertions(+), 6 deletions(-)

diff --git a/src/butil/thread_key.h b/src/butil/thread_key.h
index e95fa2fa..c150528b 100644
--- a/src/butil/thread_key.h
+++ b/src/butil/thread_key.h
@@ -108,9 +108,9 @@ public:
 
     T* get();
 
-    T* operator->() const { return get(); }
+    T* operator->() { return get(); }
 
-    T& operator*() const { return *get(); }
+    T& operator*() { return *get(); }
 
     // Iterate through all thread local objects.
     // Callback, which must accept Args params and return void,
diff --git a/test/thread_key_unittest.cpp b/test/thread_key_unittest.cpp
index f254ce41..5a95b8f0 100644
--- a/test/thread_key_unittest.cpp
+++ b/test/thread_key_unittest.cpp
@@ -46,6 +46,10 @@ struct ThreadKeyInfo {
     uint32_t seq;
 };
 
+struct ThreadKeyData {
+    int a{0};
+};
+
 TEST(ThreadLocalTest, sanity) {
     {
         ThreadKey key;
@@ -65,13 +69,16 @@ TEST(ThreadLocalTest, sanity) {
     }
 
     for (int i = 0; i < 5; ++i) {
-        ThreadLocal<int> tl;
-        ASSERT_TRUE(tl.get()!=NULL);
-        int* data = new int;
+        ThreadLocal<ThreadKeyData> tl;
+        ASSERT_TRUE(tl.get());
+        ASSERT_EQ(tl->a, 0);
+        auto data = new ThreadKeyData;
+        data->a = 1;
         tl.reset(data); // tl owns data
         ASSERT_EQ(data, tl.get());
+        ASSERT_EQ((*tl).a, 1);
         tl.reset(); // data has been deleted
-        ASSERT_TRUE(tl.get()!=NULL);
+        ASSERT_TRUE(tl.get());
     }
 }
 


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@brpc.apache.org
For additional commands, e-mail: dev-h...@brpc.apache.org

Reply via email to