我需要创建无限循环,并使用线程池创建例如 200 个线程来完成无限循环的工作。
我正在使用这个线程池 - https://github.com/Pithikos/C-Thread-Pool
同时,我正在监视服务器资源(使用 htop),发现内存每秒增加 3 MB,直到内核终止应用程序。
代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include "thpool.h"
#define MAX_IPv4 256
/* Args for thread start function */
typedef struct {
int octet1;
int octet2;
int octet3;
int octet4;
} args_struct;
/* Thread task */
void task1(void *args) {
args_struct *actual_args = args;
printf("%d.%d.%d.%d\n", actual_args->octet1, actual_args->octet2, actual_args->octet3, actual_args->octet4);
/* Do some job */
sleep(1);
/* Free the args */
free(args);
}
/* Main function */
int main( void ) {
int i=0, j=0, n=0, m=0;
/* Making threadpool n threads */
threadpool thpool = thpool_init(200);
/* Infinite loop start from the certain ip*/
while (1) {
for (i=0; i < MAX_IPv4; ++i) {
for (j=0; j < MAX_IPv4; ++j) {
for (n=0; n < MAX_IPv4; ++n) {
for (m=0; m < MAX_IPv4; ++m) {
/* Heap memory for the args different for the every thread */
args_struct *args = malloc(sizeof *args);
args->octet1 = i;
args->octet2 = j;
args->octet3 = n;
args->octet4 = m;
/* Create thread */
thpool_add_work(thpool, (void*)task1, (void*)args);
}
}
}
}
/* Start from 0.0.0.0 */
i=0;
j=0;
n=0;
m=0;
}
/* Wait until the all threads are done */
thpool_wait(thpool);
/* Destroy the threadpool */
thpool_destroy(thpool);
return 0;
}
如何解决这个问题?
最佳答案
查看您的库的问题(尤其是 this one 有关内存消耗的问题)。
建议检查作业队列长度threadpool.jobqueue.len
;
也许您的代码可以在将作业添加到队列后进行检查
不幸的是,线程池
是一个不透明的指针,您无法直接访问该值。
我建议在 thpool.c 中为线程池添加一个函数:
int thpool_jobqueue_length(thpool_* thpool_p) {
return thpool->jobqueue->len;
}
并且不要忘记thpool.h
中的声明
int thpool_jobqueue_length(threadpool);
然后修改你的代码
const int SOME_ARBITRARY_VALUE = 400
...
thpool_add_work(thpool, (void*)task1, (void*)args);
while( ( thpool_jobqueue_length(thpool) > SOME_ARBITRARY_VALUE ) ) {
sleep(1);
}
...
关于c - C、Linux 中线程池内存不足,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38714429/