- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我现在已经在一个使用 pthreads 的小 C 程序上工作了几天。我昨天或多或少花了一整天时间寻找死锁错误,但现在我发现问题并不是真正的死锁错误。下面的一段代码有完全相同的问题。
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <unistd.h>
#define NTHREADS 507
pthread_mutex_t runningThreadsMutex;
pthread_cond_t runningThreadsCond;
int runningThreads = 0;
void* HelloWorld(void* arg) {
sleep(1);
pthread_mutex_lock(&runningThreadsMutex);
runningThreads--;
printf("End thread %d\n", runningThreads);
pthread_cond_signal(&runningThreadsCond);
pthread_mutex_unlock(&runningThreadsMutex);
return NULL;
}
int main() {
pthread_t thread;
pthread_mutex_init(&runningThreadsMutex, NULL);
pthread_cond_init(&runningThreadsCond, NULL);
for (int i = 0; i < NTHREADS; ++i) {
pthread_mutex_lock(&runningThreadsMutex);
printf("Create thread %d\n", runningThreads++);
pthread_mutex_unlock(&runningThreadsMutex);
pthread_create(&thread, NULL, HelloWorld, NULL);
// pthread_detach(thread);
}
pthread_mutex_lock(&runningThreadsMutex);
while(runningThreads > 0) {
pthread_cond_wait(&runningThreadsCond, &runningThreadsMutex);
}
pthread_mutex_unlock(&runningThreadsMutex);
return 0;
}
对于 NTHREADS < 506,上面的代码似乎在我的笔记本电脑(64 位 linux 机器)上运行良好。在那种情况下,它打印出如下内容:
Create thread 0
Create thread 1
.
.
.
Create thread 505
End thread 505
End thread 504
.
.
.
End thread 0
并按预期终止。但是,如果我使用 NTHREADS >= 506,例如NTHREADS = 510 我得到
Create thread 0
Create thread 1
.
.
.
Create thread 509
End thread 509
End thread 508
.
.
.
End thread 4
它停止而不会终止的地方。所以看起来最后四个 (510-506=4) 线程永远不会终止(或者根本不会开始?)。
我也在旧的 32 位 Linux 机器上尝试了这段代码。在那里我得到了相同的行为,除了它适用于 NTHREADS < 382 但不适用于 NTHREADS >= 382(而不是 506)。
当我用谷歌搜索解决方案时,我还发现了这个问题:http://bytes.com/topic/c/answers/728087-pthreads-limit ,有人在使用 pthread_join 时遇到同样的问题(这在使用 pthreads 时可能更自然)但他们没有给出任何好的解释。
任何人都可以向我解释我做错了什么以及这段代码的根本问题是什么?我想这一定是对允许线程数的某种限制,但我应该如何处理它?</p>
最佳答案
您需要检查 pthread_create
的返回值。如果它非零,则该函数无法创建线程。一个典型的问题是新线程的堆栈内存不足。例如对于每个线程 1Mb 的堆栈,系统将需要至少 510Mb 的可用内存来启动 510 个线程。
为什么要运行这么多线程?除非你有一个拥有数百个处理器的大规模并行系统,否则这些线程只会竞争 CPU 时间和其他资源。以最合适的顺序执行工作的线程数量较少(与系统中的处理器数量数量级相同)可能会更好。
关于c - pthreads : allowed number of threads,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3387762/
从 angular 5.1 更新到 6.1 后,我开始从我的代码中收到一些错误,如下所示: Error: ngc compilation failed: components/forms/utils.
我正在学习 Typescript 并尝试了解类型和接口(interface)的最佳实践。我正在玩一个使用 GPS 坐标的示例,想知道一种方法是否比另一种更好。 let gps1 : number[];
type padding = [number, number, number, number] interface IPaddingProps { defaultValue?: padding
这两种格式在内存中保存结果的顺序上有什么区别吗? number = number + 10; number += 10; 我记得一种格式会立即保存结果,因此下一行代码可以使用新值,而对于另一种格式,
在 Python 匹配模式中,如何匹配像 1 这样的文字数字在按数字反向引用后 \1 ? 我尝试了 \g用于此目的的替换模式中可用的语法,但它在我的匹配模式中不起作用。 我有一个更大的问题,我想使用一
我的源文件here包含 HTML 代码,我想将电话号码更改为可在我的应用程序中单击。我正在寻找一个正则表达式来转换字符串 >numbernumber(\d+)$1numbernumber<",我们在S
我们有一个包含 2 个字段和一个按钮的表单。我们想要点击按钮来输出位于 int A 和 int B 之间的随机整数(比如 3、5 或 33)? (不需要使用 jQuery 或类似的东西) 最佳答案 你
我收到以下类型错误(TypeScript - 3.7.5)。 error TS2345: Argument of type '(priority1: number, priority2: number
只想创建简单的填充器以在其他功能中使用它: function fillLine(row, column, length, bgcolor) { var sheet = SpreadsheetApp
我有一个问题。当我保存程序输出的 *.txt 时,我得到以下信息:0.021111111111111112a118d0 以及更多的东西。 问题是: 这个数字中的“d0”和“a”是什么意思? 我不知道“
首先:抱歉标题太长了,但我发现很难用一句话来解释这个问题;)。是的,我也四处搜索(这里和谷歌),但找不到合适的答案。 所以,问题是这样的: 数字 1-15 将像这样放在金字塔中(由数组表示):
我想从字符串中提取血压。数据可能如下所示: text <- c("at 10.00 seated 132/69", "99/49", "176/109", "10.12 I 128/51, II 1
当尝试执行一个简单的 bash 脚本以将前面带有 0 的数字递增 1 时,原始数字被错误地解释。 #!/bin/bash number=0026 echo $number echo $((number
我有一个类型为 [number, number] 的字段,TypeScript 编译器(strict 设置为 true)出现问题,提示初始值值(value)。我尝试了以下方法: public shee
你能帮我表达数组吗:["232","2323","233"] 我试试这个:/^\[("\d{1,7}")|(,"\d{1,7}")\]$/ 但是这个表达式不能正常工作。 我使用 ruby(rail
这个问题在这里已经有了答案: meaning of (number) & (-number) (4 个回答) 关闭6年前. 例如: int get(int i) { int res = 0;
我正在考虑使用 Berkeley DB作为高度并发的移动应用程序后端的一部分。对于我的应用程序,使用 Queue对于他们的记录级别锁定将是理想的。但是,如标题中所述,我需要查询和更新概念建模的数据,如
我正在尝试解决涉及重复数字的特定 JavaScript 练习,为此我需要将重复数字处理到大量小数位。 目前我正在使用: function divide(numerator, denominator){
我有这个数组类型: interface Details { Name: string; URL: string; Year: number; } interface AppState {
我们正在使用 Spring 3.x.x 和 Quartz 2.x.x 实现 Web 应用程序。 Web 服务器是 Tomcat 7.x.x。我们有 3 台服务器。 Quartz 是集群式的,因此所有这
我是一名优秀的程序员,十分优秀!