- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我真正需要的是 *nix pthread 标识符 (pthread_t) 的可移植比较和打印。存在 pthread_equal 函数来比较两个线程 id 是否相等,但是不可能将它们与运算符 <<= >=> (当然我的意思是可移植的)进行比较,因为在某些实现中 pthread_t 是一个指向结构的指针。所以我找到了解决方案,我想分享并讨论它的可移植性。
假设我们有 thred_id 类包装器,它应该是 less-then-comparable、equality-comparable 并且当然是可打印的。我正在制作具有两个部分特化的模板类 - 一个用于指针,一个用于算术类型
template <typename T, bool A = is_arithmetic<T>::value>
struct hash
{
static unsigned long calculate(T thread_id)
{
return hash<T*, A>::calculate(&thread_id);
}
};
template <typename T>
struct hash<T*, false>
{
static unsigned long calculate(T* thread_id)
{
std::size_t hash = 0;
if (char* data = reinterpret_cast<char*>(thread_id))
{
for (int i = 0; i < sizeof(T); ++i)
{
hash = (hash << 6) ^ (hash >> 26) ^ data[i];
}
}
return hash;
}
};
template <typename T>
struct hash<T, true>
{
static unsigned long calculate(T thread_id)
{
return static_cast<unsigned long>(thread_id);
}
};
那么它是如何工作的。如果我们需要比较两个线程 ID,我们只需调用
pthread_equal(tid1, tid2);
但是对于运算符<我们使用hash来比较
hash<pthread_t>::calculate(tid1) < hash<pthread_t>::calculate(tid2)
所以在这里
如果 pthread_t
被实现为一个指针,那么我们将为指向的对象计算哈希值。
如果它是算术类型,那么我们将尝试将其转换为 unsigned int。
如果它作为一个结构实现——我们将为结构对象本身计算散列。
哈希值将仅用于 operator less 和线程 id 输出。
你怎么看?这个解决方案的可移植性如何?还有比这更好的吗?
先谢谢大家。
最佳答案
而不是尝试比较 pthread_t
您可以只封装线程创建并将新线程添加到 map<some_id, pthread_t>
生成 id 且唯一的位置。然后你总是通过 id 引用线程,你可以按照你喜欢的方式对它们进行排序、排序和比较。
关于c++ - 可移植打印和比较 pthread_t,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4762307/
如何让 pthread_t 停止一段时间?我的意思是不使用 sleep 功能;) 最佳答案 使用pthread_cond_wait(或pthread_cond_timedwait,但可能不是)阻塞线程
我想知道使用不同数量的 pthread_t 变量的差异。 这是我编写的一个简单代码: #include #include void *thread(void *vargp); int main(i
我想像这样将 pthread_t 存储在数组中: pthread_t tThreads[nThreads]; 不久之后,我使用 for 循环遍历数组以启动线程 pthread_create( &tTh
我写这个简短的例子是为了理解 C 语言的线程编程。它应该写“线程 0”。但没有输出。这是代码。 #include #include #include int i=0; pthread_mutex
我真正需要的是 *nix pthread 标识符 (pthread_t) 的可移植比较和打印。存在 pthread_equal 函数来比较两个线程 id 是否相等,但是不可能将它们与运算符 => (当
我写了一个小的 pthread 多线程编程,我想清理我创建的 pthred_t 数组。但是当我这样做时,我得到了一个 glib 已被检测到的错误。我怎样才能删除数组,或者我根本不需要。代码在没有释放内
我有一个接受输入的程序 n从命令行,并作为其操作的一部分,尝试调用以下代码: pthread_t threads[n*n]; 现在,对于任何n 1023 时,我遇到了段错误。正是这条特殊的线导致了它
我正在使用 pthread_t 打印出我在 C 中手动创建的线程的 pid。但是,我在创建新线程之前打印它(通过 ref 作为参数传递)并且它打印出不同的值(大概我的主要功能正在执行的线程)。我本来希
我正在使用 C 和 pthreads 进行编程。我有一个长时间运行的函数,我想在一个单独的线程中运行: void long_running_function(void * arg) { ...
已搜索,但没有找到令人满意的答案。 我知道没有可移植的方式来打印 pthread_t。 你是如何在你的应用中做到这一点的? 更新: 实际上我不需要 pthread_t,而是一些小的数字 id,在调试消
我有一个 C 头文件,它定义了以下函数: void my_func(pthread_t tid); 这是由另一个函数定义的: void my_func(pthread_t tid) { ... 当我编
我编写此测试代码是为了尝试将线程2的pthread_t传递给线程1,并编写了让主线程等待thread1完成的代码,而不是thread1 等待thread2 完成: void *function_
我现在尝试通过两个线程(主线程和第二个线程)之间的管道发送一些数据,但我收到有关文件描述符的 errno 9。我认为当涉及线程时文件描述符是重复的,但这里似乎不是这种情况。它通常会返回“read fr
我想为给定的线程 ID 调用 pthread_join,但前提是该线程已经启动。安全的解决方案可能是添加一个变量来跟踪哪个线程已启动或未启动。但是,我想知道是否可以检查 pthread_t 变量,类似
我想在映射中存储我自己的一个类,键是 pthread_t 类型。(我类的每个对象都包含一个 pthread_t 数据成员,所以我希望每个实例都与该 pthread_t 作为键相关联, 在 map 中)
我有一组 pthreads,它们都需要完成一个任务,然后阻塞直到最后一个完成任务,然后让它们都转到下一个任务。许多任务都会发生这种情况。 我正在考虑有一个计数器,它在每个任务开始时开始等于线程数,并且
是否可以确定我声明的 pthread_t 结构是否未使用?我有一个 pthread_t 结构数组,在我的程序结束时,我对每个结构调用 pthread_join(),但如果其中一些从未使用过,我不想对它
假设我有代码片段: ... pthread_t tt; ... pthread_create(&tt, NULL, thread_func, NULL); ... if( tt != 0) {
SO 上的帖子表明 pthread_t 是不透明类型,不是数字,当然也不是线程索引,你不应该直接比较 pthread_t 的,等等等等 问题: 为什么?是否真的有意支持没有线程数字 ID 的系统?当
我正在尝试动态创建 pthread 并面临变量寻址问题。你能告诉我应该如何访问地址吗 int main (int argc, char *argv[]) { pthread_t *threads
我是一名优秀的程序员,十分优秀!