70,037
社区成员
发帖
与我相关
我的任务
分享//使用动态分配
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
int i,L;
char *p;
void main() {
for (i=0;i<20000;i++) {
L=rand();
p=malloc(L);
if (NULL==p) {
printf("malloc error!\n");
continue;
}
memset(p,0,L);
free(p);
}
}
//不使用动态分配
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#define MAXLEN 30000
int i,L;
char buf[MAXLEN];
char *p;
void main() {
p=&buf[0];
for (i=0;i<20000;i++) {
L=rand();
if (L>MAXLEN) {
printf("L>MAXLEN==%d, ignore spilth.\n",MAXLEN);
L=MAXLEN;
}
memset(p,0,L);
}
}
个人倾向“不使用动态分配”
[/quote]
一:线程池中每个不忙的线程在同一个临界区内各自判断主程序是否有新任务,有就将自己置忙,通知主程序已无新任务,自己开始新任务。
二:主程序和线程池中不忙的线程在同一个临界区内:领导逐一判断空闲的线程并按照一定策略把任务给某个线程并置忙,记录已无新任务;线程发现自己被置忙就去忙着完成新任务。
