- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在玩 Posix 线程并编写了以下代码,我在其中创建了很多线程,但为此目的重用了 pthread_t
变量:
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
// The amount of thread creation iterations
static const int N = 300;
static pthread_t thread_1, thread_2, thread_3;
void * logic_1(void * arg)
{
usleep(1 * 1000);
printf("logic_1 end\n");
return 0;
}
void * logic_2(void * arg)
{
usleep(1 * 1000);
printf("logic_2 end\n");
return 0;
}
void * logic_3(void * arg)
{
usleep(1 * 1000);
printf("logic_3 end\n");
return 0;
}
int main()
{
int counter = 0;
int i;
for(i = 0; i < N; ++i)
{
if (pthread_create(&thread_1, NULL, &logic_1, NULL) != 0)
{
perror("error: ");
printf("thread1 did not start after %d threads that were started\n", counter);
break;
}
else
{
++counter;
printf("start %d thread\n", counter);
}
if (pthread_create(&thread_2, NULL, &logic_2, NULL) != 0)
{
perror("error: ");
printf("thread2 did not start after %d threads that were started\n", counter);
break;
}
else
{
++counter;
printf("start %d thread\n", counter);
}
if (pthread_create(&thread_3, NULL, &logic_3, NULL) != 0)
{
perror("error: ");
printf("thread3 did not start after %d threads that were started\n", counter);
break;
}
else
{
++counter;
printf("start %d thread\n", counter);
}
usleep(500 * 1000);
}
pthread_join(thread_1, NULL);
pthread_join(thread_2, NULL);
pthread_join(thread_3, NULL);
return 0;
}
执行后出现错误:
...
start 376 thread
start 377 thread
start 378 thread
logic_3 end
logic_2 end
logic_1 end
start 379 thread
start 380 thread
start 381 thread
logic_3 end
logic_2 end
logic_1 end
error: : Cannot allocate memory
thread1 did not start after 381 threads that were started
你能告诉我我做错了什么吗?我认为我在 Linux 中遇到了一些界限或限制? logic_1
、logic_2
、logic_3
中的return 0;
语句每次调用后,线程的资源是否会被释放? > 功能?也许我应该使用一个线程数组,并为这个数组的每个项目调用 pthread_join
函数?
最佳答案
您应该在循环内而不是在循环外调用 pthread_join,以便在下一次迭代中启动新的线程集之前释放分配的资源根据 pthread_join 手册页,如果您不加入线程,您将丢失系统资源
Failure to join with a thread that is joinable (i.e., one that is not
detached), produces a "zombie thread". Avoid doing this, since each
zombie thread consumes some system resources, and when enough zombie
threads have accumulated, it will no longer be possible to create new
threads (or processes).
关于c - 多次 pthread_create 调用后出现 `Cannot allocate memory` 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44569631/
我正在努力实现以下目标: 强制新创建的线程在 pthread_create() 之后立即开始运行。没有使用实时调度。 来自pthread_create() man page : Unless real
我想做这样的事情: void *do_work_son(void *data) { mystruct *d = (mystruct*)data; while(tr
typedef struct client { pthread thread; Window_t *win }client; client * client_create(int ID)
我编译最新的 buildroot,并使用输出主机 mipsel-linux-gcc 来编译我的 c 程序。我已经测试了 hello world 程序,它在 MIPS 机器上运行良好(实际上是一个用 p
每当我在我的程序上运行 valgrind 时,它都会告诉我在调用 pthread_create 的任何地方都可能丢失了内存。我一直在尝试遵循 上的指导 valgrind memory leak err
是否有(在 glibc-2.5 和更新版本中)为 pthread_create 定义 Hook 的方法? 有很多二进制应用程序,我想编写一个动态库以通过 LD_PRELOAD 加载 我可以在 main
我正在开发一个多线程程序,但是由于某种原因,我无法创建线程。当我尝试调试时,它在我的pthread_join语句处中断。 for (i = 0; i < numThreads; ++i) { pt
我使用pthread_create()创建了一个线程。新线程创建成功,控制权传递给新创建的线程。然而,主线程似乎不再执行了。主线程处于无限循环中并且永远不会退出。以下是代码片段: void *star
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: Multiple arguments to function called by pthread_creat
我在 GCC 中运行我的程序时遇到了段错误。这是一个相当长的程序,所以我只发布我认为相关的部分;如果需要更多信息,请告诉我。 void *RelaxThread(void *para) { p
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
所以我之前就一个具体问题提出了一个问题。我已经查看了该网站上的其他问题,但大多数问题都没有解决我的问题,特别是尽管我认为这个问题对其他初学者很有用。这是代码。 (pi.h) 我的结构是如何布局的 #i
我是 C 新手,这些指针的概念对我来说非常困惑。我试图做一些看起来很简单的事情,但我遇到了很多编译错误。 我想生成一个新线程并将多个指针作为参数传递给它(似乎在 c 中使用全局变量的唯一方法是通过指针
我一生都无法弄清楚为什么这是段错误。 这是段错误 get_ranks_parallel 上线 for (struct node* node = data->plist->head; node!=NUL
我的服务器正在向客户端发送 udp 数据报,并在数据包丢失时从客户端接收 NACK 数据报。我想创建一个线程来处理每个 NACK 数据包,但只有当我有东西要从客户端接收时我才想创建线程。为此,我想使用
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 8 年前。 Improve th
我看过pthread_create的文档 在底部的示例中,他们使用的是: pthread_create(&tinfo[tnum].thread_id, &attr, &thread_start, &t
我需要将多个参数传递给我想在单独线程上调用的函数。我有read执行此操作的典型方法是定义一个结构体,向函数传递一个指向该结构体的指针,并为参数取消引用它。但是,我无法让它工作: #include #
所以我试图编写一个程序,在c中创建一个线程,其工作是找到2个给定数字中的最大值。我编写的第一个程序(名为askisi.c)如下: #include #include #include int m
程序中存在两个问题首先,当我在主函数中取消注释 pthread_join() 时,会出现段错误,否则程序将运行...其次,输出文件将丢失上次读取文件中存储在全局变量words中的每个单词的第一个字母。
我是一名优秀的程序员,十分优秀!