Рассмотрим: < /p>
#include
#include
#include
void *thread_function(void *arg) {
char **records = malloc(1000000 * sizeof(char *));
if (records == NULL) {
perror("Memory allocation failed");
exit(EXIT_FAILURE);
}
for (int i = 0; i < 1000000; ++i) {
records = malloc(16);
if (records == NULL) {
perror("Memory allocation failed");
exit(EXIT_FAILURE);
}
sprintf(records, "%d", i);
}
printf("done\n");
for (int i = 0; i < 1000000; ++i) {
free(records);
}
free(records);
pthread_exit(NULL);
}
int main() {
pthread_t threads[400];
for (int i = 0; i < 400; ++i) {
if (pthread_create(&threads, NULL, thread_function, NULL) != 0) {
perror("Thread creation failed");
exit(EXIT_FAILURE);
}
}
for (int i = 0; i < 400; ++i) {
pthread_join(threads, NULL);
}
while (1) {
sched_yield();
}
return 0;
}
< /code>
fn main() {
for _ in 0..400 {
std::thread::spawn(|| {
let mut records: Box = Box::default();
for i in 0..1_000_000 {
let record = i.to_string();
records.push(record);
}
println!("done");
});
}
loop {
std::thread::yield_now();
}
}
< /code>
#include
#include
#include
int main() {
for (int i = 0; i < 1600; ++i) {
std::thread([=]() {
std::vector records;
for (int i = 0; i < 1000000; ++i) {
std::string record = std::to_string(i);
records.push_back(record);
}
std::cout
Учитывая мой ограниченный опыт в системном программировании, есть ли некоторые ключевые знания, которых мне может не хватать? Это еще одна значительная загадка для меня. Если количество потоков низкое, эта проблема не возникает. Если вы попытаетесь повторить это локально, желательно экспериментировать с настройкой количества потоков.
Подробнее здесь: https://stackoverflow.com/questions/777 ... erly-but-c