Skip to content

Commit

Permalink
hexo blog
Browse files Browse the repository at this point in the history
  • Loading branch information
charon-cheung committed May 27, 2024
1 parent e435879 commit 4fc9a86
Show file tree
Hide file tree
Showing 1,130 changed files with 2,252 additions and 2,198 deletions.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion 2018/04/30/C++/多线程/多线程 1/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
error: 'Copy error',
noSupport: 'The browser does not support'
}
} </script><meta name="generator" content="Hexo 5.4.0"></head><body><canvas class="fireworks"></canvas><i class="fa fa-arrow-right" id="toggle-sidebar" aria-hidden="true"></i><div id="sidebar"><div class="toggle-sidebar-info text-center"><span data-toggle="Toggle article">Toggle site</span><hr></div><div class="sidebar-toc"><div class="sidebar-toc__title">Catalog</div><div class="sidebar-toc__progress"><span class="progress-notice">You've read</span><span class="progress-num">0</span><span class="progress-percentage">%</span><div class="sidebar-toc__progress-bar"></div></div><div class="sidebar-toc__content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%89%8B%E5%8A%A8%E5%BC%BA%E5%88%B6%E4%BA%8B%E4%BB%B6%E5%BE%AA%E7%8E%AF"><span class="toc-number">1.</span> <span class="toc-text">手动强制事件循环</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%A4%9A%E7%BA%BF%E7%A8%8B%E5%A4%84%E7%90%86%E3%80%82"><span class="toc-number">2.</span> <span class="toc-text">多线程处理。</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8QThread"><span class="toc-number">2.1.</span> <span class="toc-text">使用QThread</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BF%A1%E5%8F%B7%E4%B8%8E%E6%A7%BD%E5%AE%9E%E7%8E%B0%E7%BA%BF%E7%A8%8B%E9%97%B4%E9%80%9A%E4%BF%A1"><span class="toc-number">2.1.1.</span> <span class="toc-text">信号与槽实现线程间通信</span></a></li></ol></li></ol></li></ol></div></div><div class="author-info hide"><div class="author-info__avatar text-center"><img src="https://s2.loli.net/2022/02/17/HQaCXUBn64zsMIL.png"></div><div class="author-info__name text-center">Charon Cheung</div><div class="author-info__description text-center"></div><hr><div class="author-info-articles"><a class="author-info-articles__archives article-meta" href="/archives"><span class="pull-left">Articles</span><span class="pull-right">659</span></a><a class="author-info-articles__tags article-meta" href="/tags"><span class="pull-left">Tags</span><span class="pull-right">6</span></a><a class="author-info-articles__categories article-meta" href="/categories"><span class="pull-left">Categories</span><span class="pull-right">51</span></a></div></div></div><div id="content-outer"><div id="top-container" style="background-image: url(https://s2.loli.net/2022/11/17/QOR8cePIS5NdDrK.jpg)"><div id="page-header"><span class="pull-left"> <a id="site-name" href="/">沉默杀手</a></span><i class="fa fa-bars toggle-menu pull-right" aria-hidden="true"></i><span class="pull-right menus"><a class="site-page" href="/">Home</a><a class="site-page" href="/archives">Archives</a><a class="site-page" href="/tags">Tags</a><a class="site-page" href="/categories">Categories</a></span></div><div id="post-info"><div id="post-title">Qt中多线程的使用(一)</div><div id="post-meta"><time class="post-meta__date"><i class="fa fa-calendar" aria-hidden="true"></i> 2018-04-30</time><span class="post-meta__separator">|</span><i class="fa fa-inbox post-meta__icon" aria-hidden="true"></i><a class="post-meta__categories" href="/categories/C/">C++</a><i class="fa fa-angle-right" aria-hidden="true"></i><i class="fa fa-inbox post-meta__icon" aria-hidden="true"></i><a class="post-meta__categories" href="/categories/C/%E5%A4%9A%E7%BA%BF%E7%A8%8B/">多线程</a><span class="post-meta__separator">|</span><i class="fa fa-comment-o post-meta__icon" aria-hidden="true"></i><a href="/2018/04/30/C++/%E5%A4%9A%E7%BA%BF%E7%A8%8B/%E5%A4%9A%E7%BA%BF%E7%A8%8B%201/#disqus_thread"><span class="disqus-comment-count" data-disqus-identifier="2018/04/30/C++/多线程/多线程 1/"></span></a><div class="post-meta-wordcount"><span>Word count: </span><span class="word-count">2.2k</span><span class="post-meta__separator">|</span><span>Reading time: 8 min</span></div></div></div></div><div class="layout" id="content-inner"><article id="post"><div class="article-container" id="post-content"><p>我们要实现的是读取大文件<code>qtgui.index</code>的内容加入文本框中。<br><img src="https://c1.staticflickr.com/1/907/41750558822_351a2ffe67_z.jpg" alt="预览"><br>很容易想到的方法:<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">QFile* file = <span class="keyword">new</span> <span class="built_in">QFile</span>(<span class="string">&quot;E:\qtgui.index&quot;</span>);</span><br><span class="line">file-&gt;<span class="built_in">open</span>(QIODevice::ReadOnly);</span><br><span class="line">QTextStream *stream = <span class="keyword">new</span> <span class="built_in">QTextStream</span>(file);</span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span>(!stream-&gt;<span class="built_in">atEnd</span>())</span><br><span class="line">&#123;</span><br><span class="line"> QString line = stream-&gt;<span class="built_in">readLine</span>();</span><br><span class="line"> ui-&gt;textEdit-&gt;<span class="built_in">append</span>(line);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><br>结果运行后发现程序失去响应。因为读取大文件要很长时间,事件循环一直等待函数返回,这样导致阻塞事件循环。结果,GUI线程所有的绘制和交互都被阻塞在事件队列中,无法执行重绘等事件,整个程序就失去响应了。</p>
} </script><meta name="generator" content="Hexo 5.4.0"></head><body><canvas class="fireworks"></canvas><i class="fa fa-arrow-right" id="toggle-sidebar" aria-hidden="true"></i><div id="sidebar"><div class="toggle-sidebar-info text-center"><span data-toggle="Toggle article">Toggle site</span><hr></div><div class="sidebar-toc"><div class="sidebar-toc__title">Catalog</div><div class="sidebar-toc__progress"><span class="progress-notice">You've read</span><span class="progress-num">0</span><span class="progress-percentage">%</span><div class="sidebar-toc__progress-bar"></div></div><div class="sidebar-toc__content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%89%8B%E5%8A%A8%E5%BC%BA%E5%88%B6%E4%BA%8B%E4%BB%B6%E5%BE%AA%E7%8E%AF"><span class="toc-number">1.</span> <span class="toc-text">手动强制事件循环</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%A4%9A%E7%BA%BF%E7%A8%8B%E5%A4%84%E7%90%86%E3%80%82"><span class="toc-number">2.</span> <span class="toc-text">多线程处理。</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8QThread"><span class="toc-number">2.1.</span> <span class="toc-text">使用QThread</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BF%A1%E5%8F%B7%E4%B8%8E%E6%A7%BD%E5%AE%9E%E7%8E%B0%E7%BA%BF%E7%A8%8B%E9%97%B4%E9%80%9A%E4%BF%A1"><span class="toc-number">2.1.1.</span> <span class="toc-text">信号与槽实现线程间通信</span></a></li></ol></li></ol></li></ol></div></div><div class="author-info hide"><div class="author-info__avatar text-center"><img src="https://s2.loli.net/2022/02/17/HQaCXUBn64zsMIL.png"></div><div class="author-info__name text-center">Charon Cheung</div><div class="author-info__description text-center"></div><hr><div class="author-info-articles"><a class="author-info-articles__archives article-meta" href="/archives"><span class="pull-left">Articles</span><span class="pull-right">660</span></a><a class="author-info-articles__tags article-meta" href="/tags"><span class="pull-left">Tags</span><span class="pull-right">6</span></a><a class="author-info-articles__categories article-meta" href="/categories"><span class="pull-left">Categories</span><span class="pull-right">51</span></a></div></div></div><div id="content-outer"><div id="top-container" style="background-image: url(https://s2.loli.net/2022/11/17/QOR8cePIS5NdDrK.jpg)"><div id="page-header"><span class="pull-left"> <a id="site-name" href="/">沉默杀手</a></span><i class="fa fa-bars toggle-menu pull-right" aria-hidden="true"></i><span class="pull-right menus"><a class="site-page" href="/">Home</a><a class="site-page" href="/archives">Archives</a><a class="site-page" href="/tags">Tags</a><a class="site-page" href="/categories">Categories</a></span></div><div id="post-info"><div id="post-title">Qt中多线程的使用(一)</div><div id="post-meta"><time class="post-meta__date"><i class="fa fa-calendar" aria-hidden="true"></i> 2018-04-30</time><span class="post-meta__separator">|</span><i class="fa fa-inbox post-meta__icon" aria-hidden="true"></i><a class="post-meta__categories" href="/categories/C/">C++</a><i class="fa fa-angle-right" aria-hidden="true"></i><i class="fa fa-inbox post-meta__icon" aria-hidden="true"></i><a class="post-meta__categories" href="/categories/C/%E5%A4%9A%E7%BA%BF%E7%A8%8B/">多线程</a><span class="post-meta__separator">|</span><i class="fa fa-comment-o post-meta__icon" aria-hidden="true"></i><a href="/2018/04/30/C++/%E5%A4%9A%E7%BA%BF%E7%A8%8B/%E5%A4%9A%E7%BA%BF%E7%A8%8B%201/#disqus_thread"><span class="disqus-comment-count" data-disqus-identifier="2018/04/30/C++/多线程/多线程 1/"></span></a><div class="post-meta-wordcount"><span>Word count: </span><span class="word-count">2.2k</span><span class="post-meta__separator">|</span><span>Reading time: 8 min</span></div></div></div></div><div class="layout" id="content-inner"><article id="post"><div class="article-container" id="post-content"><p>我们要实现的是读取大文件<code>qtgui.index</code>的内容加入文本框中。<br><img src="https://c1.staticflickr.com/1/907/41750558822_351a2ffe67_z.jpg" alt="预览"><br>很容易想到的方法:<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">QFile* file = <span class="keyword">new</span> <span class="built_in">QFile</span>(<span class="string">&quot;E:\qtgui.index&quot;</span>);</span><br><span class="line">file-&gt;<span class="built_in">open</span>(QIODevice::ReadOnly);</span><br><span class="line">QTextStream *stream = <span class="keyword">new</span> <span class="built_in">QTextStream</span>(file);</span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span>(!stream-&gt;<span class="built_in">atEnd</span>())</span><br><span class="line">&#123;</span><br><span class="line"> QString line = stream-&gt;<span class="built_in">readLine</span>();</span><br><span class="line"> ui-&gt;textEdit-&gt;<span class="built_in">append</span>(line);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><br>结果运行后发现程序失去响应。因为读取大文件要很长时间,事件循环一直等待函数返回,这样导致阻塞事件循环。结果,GUI线程所有的绘制和交互都被阻塞在事件队列中,无法执行重绘等事件,整个程序就失去响应了。</p>
<p>解决阻塞一般有两种方法:</p>
<h2 id="手动强制事件循环"><a href="#手动强制事件循环" class="headerlink" title="手动强制事件循环"></a>手动强制事件循环</h2><p>在任务中不断调用QCoreApplication::processEvents()手动强制事件循环,它会在处理完队列中所有事件后返回。但是如果两次函数调用的间隔时间不够短,用户仍能明显感觉到程序卡顿。所以在while循环最后加一行<code>QApplication::processEvents();</code>即可。</p>
<h2 id="多线程处理。"><a href="#多线程处理。" class="headerlink" title="多线程处理。"></a>多线程处理。</h2><p>Qt提供了三种方式:QThread、QRunnable / QThreadPool、QtConcurrent。其中最常用的是 QThread。</p>
Expand Down
Loading

0 comments on commit 4fc9a86

Please sign in to comment.