如何保持一定数量的线程

我的职业是看店铺的 2013-08-19 11:16:54
我现在有这么一个程序,程序的流程是这样的,
首先从数据库中读取一定数量的内容,假设数量为N
然后通过N个线程去访问读取到内容,时间是指定的
然后等待线程的结束
如此循环下去

但是有这么一个问题,那就是我现在的程序,我开辟N个线程后,假设有一个线程访问指定的内容慢了,而其他线程已经返回状态了,这样的话程序总的运行时间 = 数据库总的内容数量 / 每次读取的内容数量 * 指定的时间

现在我希望是开辟了线程后,指定数量的内容返回一些后,立马去读取数据库,保证每一时刻都会有N个数量的线程去访问内容,从而减少程序总的运行时间

还请大家指点下迷津
谢谢

下面是目前程序的main函数

int
main(int argc, char *argv[])
{
struct timeval start;
struct timeval end;
unsigned long timer;
gettimeofday(&start, NULL);

welcome();
if (sql_login(argv[1], argv[2], argv[3]))
fprintf(stdout, "Welcome to Donkey\n");
else
{
fprintf(stdout, "Login MySQL failure\n");
exit(0);
}

struct DOMAIN domain[PTHREAD_NUM];
pthread_t tid[PTHREAD_NUM];
int res;
MYSQL_RES *result = NULL;
MYSQL_ROW row = NULL;
MYSQL_FIELD *fields;
int exe_num;
unsigned int i = 0;
unsigned int num_fields = 0;
#ifdef DEBUG
unsigned int exe_time = 0;
#endif
int m;
struct tm *now;

res = pthread_mutex_init(&mutext, NULL);

while (1)
{
fprintf(stdout, "S-------T-----------A---------R--------T---->\n");
memset(domain, 0, sizeof(domain));

// update the time_out
result = exe_sql("select distinct user_id, domain, ip, status, last_check from domain");
exe_num = mysql_num_rows(result);
fprintf(stdout, "select num rows: %d\n", exe_num);
timeout = exe_num / PTHREAD_NUM;
if ((timeout > 10) || (timeout < 1) )
timeout = 10;

fprintf(stdout, "timeout %d\n", timeout);

// Get info from MySQL
num_fields = mysql_num_fields(result);
fields = mysql_fetch_fields(result);
#ifdef DEBUG
int count = 0;
#endif
i = 0;
while ((row = mysql_fetch_row(result)) != NULL)
{
domain[i].user_id = atoi(row[0]);
sprintf(domain[i].domain, "%s", row[1]);
sprintf(domain[i].ip, "%s", row[2]);
sprintf(domain[i].status, "%s", row[3]);
strncpy(domain[i].last_check, row[4], 16);
strcat(domain[i].last_check, "\0");
#ifdef DEBUG
fprintf(stdout, "%-3d:user_id %d domain %-30s ip %-10s status %s last_check %s\n",
count, domain[i].user_id, domain[i].domain, domain[i].ip, domain[i].status, domain[i].last_check);
count++;
#endif

i++;
if (i == PTHREAD_NUM)
{
// get current time
now = getlocaltime();
#ifdef DEBUG
fprintf(stdout, "%d now time: %d-%d-%d %d:%d:%d\n",
count, now->tm_year+1900, now->tm_mon+1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec);
#endif
// check the status
for (m = 0; m < PTHREAD_NUM; m++)
if (comparetime(*now, domain[m].last_check, timeout))
if ((res = pthread_create(&tid[m], NULL, check, (void *)&domain[m] )) != 0)
fprintf(stderr, "pthread_create:%s\n", strerror(res));

for (m = 0; m < PTHREAD_NUM; m++)
res = pthread_join(tid[m], NULL);
i = 0;
memset(domain, 0, sizeof(domain));
}
}
// check the status
for (m = 0; m < i; m++)
if (comparetime(*now, domain[m].last_check, timeout))
if ((res = pthread_create(&tid[m], NULL, check, (void *)&domain[m] )) != 0)
fprintf(stderr, "pthread_create:%s\n", strerror(res));

for (m = 0; m < i; m++)
res = pthread_join(tid[m], NULL);

mysql_free_result(result);
fprintf(stdout, "E-------------------N------------------D---->\n");
}

gettimeofday(&end,NULL);
timer = 1000000 * (end.tv_sec-start.tv_sec)+ end.tv_usec-start.tv_usec;
fprintf(stdout, "Goodbye Dokey runs :SQL Number [%d] Time [%ld] us\n", exe_num, timer);
return 0;
}

...全文
161 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
max_min_ 2013-08-19
  • 打赏
  • 举报
回复
这个应该要取决你的cpu是几核的吧! 如果只是单核话, 你开再多线程, 也只能有一个线程在跑,其他的线程被挂起等待的啊!
大尾巴猫 2013-08-19
  • 打赏
  • 举报
回复
线程读了数据就返回,然后需要读的时候再开线程。 ------------------------------------------------ 没理解错的话,你的程序是这样的流程吧。 哪有这么用多线程的。 我只会windows的线程,以在windows下为例: 多个线程一旦开启,先等待事件或者其他的信号量。 事件等到,就开始工作(读数据库),读好了,继续等待事件,并不返回的。 一直到主线程通知其他线程返回(或者其他什么条件,比如数据库读好了,不用再读), 所有的线程返回。 整个程序执行过程中,每个线程只创建一次,销毁一次。线程有工作的时候做,没工作就挂起。

70,020

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧