奇怪的列表打印函数行为。如果我打印“\\\<br/>”,工作,如果我删除它不工作 [英] Weird list print function behaviour. Works if i print &quot;\n&quot;, doesn&#39;t work if I remove it

查看:64
本文介绍了奇怪的列表打印函数行为。如果我打印“\\\<br/>”,工作,如果我删除它不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时候c ++会让我玩得很开心。我真的不能想到为什么这是/不工作,我会很高兴,如果你知道任何人。

Sometimes c++ plays me big time. I really can't think of why this does/doesn't work and I'd be happy if any of you knew.

我每秒钟调用这个函数一次

I call this function once every second on a thread.

此代码WORKS(打印正在迭代的列表中的内容):

This code WORKS (prints what is on the list being iterated):

void DeltaList::print()
{
  pthread_mutex_lock (&mutex);
  printf("\n");
  list<Delta*>::iterator it;
  for(it=deltas.begin(); it!=deltas.end(); it++)
  {
    printf("%d   ", (int) (*it)->timeleft);
  }

  pthread_mutex_unlock (&mutex);
}

这一个没有(没有打印):

This one DOESN'T (nothing is printed):

void DeltaList::print()
{
  pthread_mutex_lock (&mutex);
  //printf("\n");
  list<Delta*>::iterator it;
  for(it=deltas.begin(); it!=deltas.end(); it++)
  {
    printf("%d   ", (int) (*it)->timeleft);
  }

  pthread_mutex_unlock (&mutex);
}

那么...?

推荐答案

这与线程或C ++无关。操作系统正在缓冲您的输出,当stdout是控制台时, \\\
隐式刷新缓冲区。如果您希望每个调用立即显示其输出,请在循环后调用 fflush(stdout)

This has nothing to do with threading or C++. The OS is buffering your output, and the \n implicitly flushes the buffer when stdout is a console. Call fflush(stdout) after the loop if you want every call to show its output immediately.

这篇关于奇怪的列表打印函数行为。如果我打印“\\\<br/>”,工作,如果我删除它不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆