- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
有时,当我尝试使用 pthread_key_create
创建 key 时,我会收到 EAGAIN 错误代码。是否有可能确切地知道为什么?
文档说:
The system lacked the necessary resources to create another thread-specific data key, or the system-imposed limit on the total number of keys per process [PTHREAD_KEYS_MAX] would be exceeded.
如何检查它是否是键的限制?也许一些监视工具之王可以检查系统中已经打开了多少键,还有多少可以使用?
关于我们的代码的一件重要事情:我们使用 fork()
并且有多个进程在运行。每个进程可以有多个线程。
我发现我们在使用fork()
时,对thread key没有独立的限制。这是一个小例子。
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
size_t create_keys(pthread_key_t *keys, size_t number_of_keys)
{
size_t counter = 0;
for (size_t i = 0; i < number_of_keys; i++)
{
int e = pthread_key_create(keys + i, NULL);
if (e)
{
printf("ERROR (%d): index: %ld, pthread_key_create (%d)\n", getpid(), i, e);
break;
}
counter++;
}
return counter;
}
int main(int argc, char const *argv[])
{
printf("maximim number of thread keys: %ld\n", sysconf(_SC_THREAD_KEYS_MAX));
printf("process id: %d\n", getpid());
const size_t number_of_keys = 1024;
pthread_key_t keys_1[number_of_keys];
memset(keys_1, 0, number_of_keys * sizeof(pthread_key_t));
printf("INFO (%d): number of active keys: %ld\n", getpid(), create_keys(keys_1, number_of_keys));
pid_t p = fork();
if (p == 0)
{
printf("process id: %d\n", getpid());
pthread_key_t keys_2[number_of_keys];
memset(keys_2, 0, number_of_keys * sizeof(pthread_key_t));
printf("INFO (%d): number of active keys: %ld\n", getpid(), create_keys(keys_2, number_of_keys));
}
return 0;
}
当我在 Ubuntu 16.04 上运行此示例时,我发现如果我使用与限制 (1024) 相同数量的键,则子进程无法创建任何新线程键。但是,如果我为父进程和子进程使用 512 键,我可以无错误地运行它。
最佳答案
如您所知,fork() 的传统工作方式是将进程复制到内存中,然后从每个副本中作为父进程和子进程的同一点继续执行。这就是 fork() 的返回码所指示的。
为了执行 fork(),必须复制进程的内部结构。内存、堆栈、打开的文件,可能还有线程本地存储键。每个系统在 fork() 的实现上都是不同的。某些系统允许您自定义要复制的进程区域(请参阅 Linux clone(2) 界面)。但是,概念保持不变。
因此,转到您的示例代码:如果您在父进程中分配 1024 个键,每个子进程都会继承一个完整的键表并且没有备用键可以使用,从而导致错误。如果在父级中只分配了 512 个键,那么每个子级继承了一个半空的键表,并且有 512 个备用键可以使用,因此不会出现错误。
关于linux - 为什么 EAGAIN 在 pthread_key_create 发生?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55904512/
根据 pthread_key_create手册页我们可以关联一个在线程关闭时调用的析构函数。我的问题是我注册的析构函数没有被调用。我的代码要点如下。 static pthread_key_t key;
我的代码: const uptr kPthreadDestructorIterations = 2; static pthread_key_t key; static bool destructor_
我们可以在使用 pthread_create/(not key_) 时将参数传递给线程,如果每个线程使用值结束。如果关于使用静态存储,我们使用互斥锁,那么,我们使用 pthread_key_creat
我在弄清楚 pthread_key_t 和 pthread_key_create 如何工作时遇到了一些麻烦。据我了解,每个线程都有 TLS(线程本地存储),并且使用 key 来访问线程本地存储。我不明
我已经从这里下载了 gtest 1.7.0 源代码: https://code.google.com/p/googletest/downloads/list 并在 ubuntu 13.10 上构建 g
我写了一个程序来学习 Linux (Linux 3.13.0-24-generic#46-Ubuntu) 上的线程专有数据,如下。 我尝试在传递给 pthread_key_create() 的析构函数
CGO_CFLAGS="--sysroot=android-sdk-linux/ndk-bundle/platforms/android-9/arch-arm/" CGO_ENABLED=1 CC=a
假设有以下代码: pthread_key_t key; pthread_key_create(&key, NULL); /* failure here */ pthread_key_delete
假设如下代码: pthread_key_t key; pthread_key_create(&key, NULL); /* failure here */ pthread_key_delete(
这是构建日志,有人可以帮忙吗? gcc -DHAVE_CONFIG_H -DHAVE_VERSION_H -DLIBRARY_DIR="\"/usr/local/lib\"" -DSBIN_DIR="
有时,当我尝试使用 pthread_key_create 创建 key 时,我会收到 EAGAIN 错误代码。是否有可能确切地知道为什么? 文档说: The system lacked the nec
这个问题已经有答案了: undefined reference to `pthread_key_create' (linker error) (5 个回答) 已关闭 8 年前。 在 Ubuntu Li
采用这个简单的函数,在由 std::mutex 实现的锁下递增整数: #include std::mutex m; void inc(int& i) { std::unique_lock l
我是一名优秀的程序员,十分优秀!