Skip to content

Commit

Permalink
Remove redundant const of ThreadLocal (#2855)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenBright authored Dec 31, 2024
1 parent 103da1d commit 5c03414
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/butil/thread_key.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ class ThreadLocal {

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,
Expand Down
15 changes: 11 additions & 4 deletions test/thread_key_unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ struct ThreadKeyInfo {
uint32_t seq;
};

struct ThreadKeyData {
int a{0};
};

TEST(ThreadLocalTest, sanity) {
{
ThreadKey key;
Expand All @@ -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());
}
}

Expand Down

0 comments on commit 5c03414

Please sign in to comment.