From 2accbf778b443de3dd7e4c1c6ca16a14e3a38f75 Mon Sep 17 00:00:00 2001 From: Bright Chen Date: Mon, 3 Jul 2023 00:11:52 +0800 Subject: [PATCH] Thread local without num limit --- src/butil/thread_key.cpp | 1 - src/butil/thread_key.h | 1 - test/thread_key_unittest.cpp | 13 +++++-------- 3 files changed, 5 insertions(+), 10 deletions(-) diff --git a/src/butil/thread_key.cpp b/src/butil/thread_key.cpp index 2f2ac65e14..6a95add2d6 100644 --- a/src/butil/thread_key.cpp +++ b/src/butil/thread_key.cpp @@ -17,7 +17,6 @@ #include "thread_key.h" #include "pthread.h" -#include #include #include "butil/thread_local.h" diff --git a/src/butil/thread_key.h b/src/butil/thread_key.h index 07e773963b..7a6c8c94bd 100644 --- a/src/butil/thread_key.h +++ b/src/butil/thread_key.h @@ -163,7 +163,6 @@ T* ThreadLocal::get() { } int rc = thread_setspecific(_key, ptr); if (rc != 0) { - errno = rc; DefaultDtor(ptr); return NULL; } diff --git a/test/thread_key_unittest.cpp b/test/thread_key_unittest.cpp index 8586016b74..a4609aed20 100644 --- a/test/thread_key_unittest.cpp +++ b/test/thread_key_unittest.cpp @@ -53,7 +53,6 @@ TEST(ThreadLocalTest, sanity) { std::unique_ptr data(new int(1)); int *raw_data = data.get(); ASSERT_EQ(0, butil::thread_key_create(key, NULL)); - ASSERT_NE(0, butil::thread_key_create(key, NULL)); ASSERT_EQ(NULL, butil::thread_getspecific(key)); ASSERT_EQ(0, butil::thread_setspecific(key, (void *)raw_data)); @@ -130,7 +129,7 @@ TEST(ThreadLocalTest, thread_key_create_and_delete) { } } -void* ThreadLocalFun(void* arg) { +void* ThreadLocalFunc(void* arg) { auto thread_locals = (std::vector*>*)arg; std::vector expects(thread_locals->size(), 0); for (auto tl : *thread_locals) { @@ -146,9 +145,10 @@ void* ThreadLocalFun(void* arg) { ++expects[index]; bthread_usleep(10); } + return NULL; } -void TestThreadLocalMultiThread() { +TEST(ThreadLocalTest, thread_local_multi_thread) { g_stopped = false; int thread_local_num = 20480; std::vector*> args(thread_local_num, NULL); @@ -159,7 +159,7 @@ void TestThreadLocalMultiThread() { const int thread_num = 8; pthread_t threads[thread_num]; for (int i = 0; i < thread_num; ++i) { - ASSERT_EQ(0, pthread_create(&threads[i], NULL, ThreadLocalFun, &args)); + ASSERT_EQ(0, pthread_create(&threads[i], NULL, ThreadLocalFunc, &args)); } sleep(5); @@ -172,10 +172,6 @@ void TestThreadLocalMultiThread() { } } -TEST(ThreadLocalTest, thread_local_multi_thread) { - TestThreadLocalMultiThread(); -} - struct BAIDU_CACHELINE_ALIGNMENT ThreadKeyArg { std::vector thread_keys; bool ready_delete = false; @@ -211,6 +207,7 @@ void* ThreadKeyFunc(void* arg) { EXPECT_TRUE(butil::thread_getspecific(*key) == NULL) << butil::thread_getspecific(*key); } + return NULL; } TEST(ThreadLocalTest, thread_key_multi_thread) {