bkietz commented on a change in pull request #10644:
URL: https://github.com/apache/arrow/pull/10644#discussion_r663119109
##########
File path: cpp/src/arrow/util/io_util.cc
##########
@@ -1734,5 +1665,21 @@ int64_t GetRandomSeed() {
return static_cast<int64_t>(seed_gen());
}
+uint64_t GetThreadId() {
+ uint64_t equiv{0};
+ // std::thread::id is trivially copyable as per C++ spec,
+ // so type punning as a uint64_t should work
+ static_assert(sizeof(std::thread::id) <= sizeof(uint64_t),
+ "std::thread::id can't fit into uint64_t");
+ const auto tid = std::this_thread::get_id();
+ memcpy(&equiv, reinterpret_cast<const void*>(&tid), sizeof(tid));
+ return equiv;
+}
+
+uint64_t GetOptionalThreadId() {
+ auto tid = GetThreadId();
+ return (tid == 0) ? tid - 1 : tid;
Review comment:
If we're going to use -1 in multiple places like this, let's define it
as a global constant:
```suggestion
return (tid == 0) ? kUnlikelyThreadId : tid;
```
##########
File path: cpp/src/arrow/util/io_util.h
##########
@@ -399,5 +338,12 @@ Status SendSignalToThread(int signum, uint64_t thread_id);
ARROW_EXPORT
int64_t GetRandomSeed();
+/// \brief Get the current thread id
Review comment:
It seems odd to put this in io_util.h and not thread_pool.h
##########
File path: cpp/src/arrow/util/io_util.cc
##########
@@ -1734,5 +1665,21 @@ int64_t GetRandomSeed() {
return static_cast<int64_t>(seed_gen());
}
+uint64_t GetThreadId() {
+ uint64_t equiv{0};
+ // std::thread::id is trivially copyable as per C++ spec,
+ // so type punning as a uint64_t should work
+ static_assert(sizeof(std::thread::id) <= sizeof(uint64_t),
+ "std::thread::id can't fit into uint64_t");
+ const auto tid = std::this_thread::get_id();
+ memcpy(&equiv, reinterpret_cast<const void*>(&tid), sizeof(tid));
+ return equiv;
+}
+
+uint64_t GetOptionalThreadId() {
+ auto tid = GetThreadId();
+ return (tid == 0) ? tid - 1 : tid;
Review comment:
Actually I'm not sure I understand the intent of this function and it's
not exported
##########
File path: cpp/src/arrow/util/io_util.h
##########
@@ -399,5 +338,12 @@ Status SendSignalToThread(int signum, uint64_t thread_id);
ARROW_EXPORT
int64_t GetRandomSeed();
+/// \brief Get the current thread id
+///
+/// In addition to having the same properties as std::thread, the returned
value
+/// is a regular integer value, which is more convenient than an opaque type.
+ARROW_EXPORT
+uint64_t GetThreadId();
+
Review comment:
```suggestion
ARROW_EXPORT
uint64_t GetOptionalThreadId();
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]