A<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://www.cppe.ru/index.php?action=history&amp;feed=atom&amp;title=C%2B%2B%2FQt%2FQThread</id>
		<title>C++/Qt/QThread - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://www.cppe.ru/index.php?action=history&amp;feed=atom&amp;title=C%2B%2B%2FQt%2FQThread"/>
		<link rel="alternate" type="text/html" href="http://www.cppe.ru/index.php?title=C%2B%2B/Qt/QThread&amp;action=history"/>
		<updated>2026-04-18T18:31:59Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://www.cppe.ru/index.php?title=C%2B%2B/Qt/QThread&amp;diff=1266&amp;oldid=prev</id>
		<title> в 14:21, 25 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://www.cppe.ru/index.php?title=C%2B%2B/Qt/QThread&amp;diff=1266&amp;oldid=prev"/>
				<updated>2010-05-25T14:21:06Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 14:21, 25 мая 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
			</entry>

	<entry>
		<id>http://www.cppe.ru/index.php?title=C%2B%2B/Qt/QThread&amp;diff=1267&amp;oldid=prev</id>
		<title>Admin: 1 версия:&amp;#32;Импорт контента...</title>
		<link rel="alternate" type="text/html" href="http://www.cppe.ru/index.php?title=C%2B%2B/Qt/QThread&amp;diff=1267&amp;oldid=prev"/>
				<updated>2010-05-25T10:25:18Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия: Импорт контента...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Extends QThread to create thread==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
Foundations of Qt Development\Chapter12\inorderthreads\main.cpp&lt;br /&gt;
/*&lt;br /&gt;
 * Copyright (c) 2006-2007, Johan Thelin&lt;br /&gt;
 * &lt;br /&gt;
 * All rights reserved.&lt;br /&gt;
 * &lt;br /&gt;
 * Redistribution and use in source and binary forms, with or without modification, &lt;br /&gt;
 * are permitted provided that the following conditions are met:&lt;br /&gt;
 * &lt;br /&gt;
 *     * Redistributions of source code must retain the above copyright notice, &lt;br /&gt;
 *       this list of conditions and the following disclaimer.&lt;br /&gt;
 *     * Redistributions in binary form must reproduce the above copyright notice,  &lt;br /&gt;
 *       this list of conditions and the following disclaimer in the documentation &lt;br /&gt;
 *       and/or other materials provided with the distribution.&lt;br /&gt;
 *     * Neither the name of APress nor the names of its contributors &lt;br /&gt;
 *       may be used to endorse or promote products derived from this software &lt;br /&gt;
 *       without specific prior written permission.&lt;br /&gt;
 * &lt;br /&gt;
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS&lt;br /&gt;
 * &amp;quot;AS IS&amp;quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT&lt;br /&gt;
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR&lt;br /&gt;
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR&lt;br /&gt;
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,&lt;br /&gt;
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,&lt;br /&gt;
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR&lt;br /&gt;
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF&lt;br /&gt;
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING&lt;br /&gt;
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS&lt;br /&gt;
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.&lt;br /&gt;
 *&lt;br /&gt;
 */&lt;br /&gt;
#include &amp;lt;QApplication&amp;gt;&lt;br /&gt;
#include &amp;lt;QThread&amp;gt;&lt;br /&gt;
#include &amp;lt;QMutex&amp;gt;&lt;br /&gt;
#include &amp;lt;QMessageBox&amp;gt;&lt;br /&gt;
#include &amp;lt;QtDebug&amp;gt;&lt;br /&gt;
class TextThread : public QThread&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
  TextThread( QString text );&lt;br /&gt;
  &lt;br /&gt;
  void run();&lt;br /&gt;
  &lt;br /&gt;
private:&lt;br /&gt;
  QString m_text;&lt;br /&gt;
};&lt;br /&gt;
bool stopThreads = false;&lt;br /&gt;
TextThread::TextThread( QString text ) : QThread()&lt;br /&gt;
{&lt;br /&gt;
  m_text = text;&lt;br /&gt;
}&lt;br /&gt;
QMutex mutex;&lt;br /&gt;
void TextThread::run()&lt;br /&gt;
{&lt;br /&gt;
  while( !stopThreads )&lt;br /&gt;
  {&lt;br /&gt;
    mutex.lock();&lt;br /&gt;
    if( stopThreads )&lt;br /&gt;
      return;&lt;br /&gt;
      &lt;br /&gt;
    qDebug() &amp;lt;&amp;lt; m_text;&lt;br /&gt;
    sleep( 1 );&lt;br /&gt;
    mutex.unlock();&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
int main( int argc, char **argv )&lt;br /&gt;
{&lt;br /&gt;
  QApplication app( argc, argv );&lt;br /&gt;
  &lt;br /&gt;
  TextThread foo( &amp;quot;Foo&amp;quot; ), bar( &amp;quot;Bar&amp;quot; );&lt;br /&gt;
  &lt;br /&gt;
  foo.start();&lt;br /&gt;
  bar.start();&lt;br /&gt;
  &lt;br /&gt;
  QMessageBox::information( 0, &amp;quot;Threading&amp;quot;, &amp;quot;Close me to stop!&amp;quot; );&lt;br /&gt;
  &lt;br /&gt;
  stopThreads = true;&lt;br /&gt;
  &lt;br /&gt;
  foo.wait();&lt;br /&gt;
  bar.wait();&lt;br /&gt;
    &lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
   &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Producer and consumer by Qt thread==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
Foundations of Qt Development\Chapter12\competingsemaphore\main.cpp&lt;br /&gt;
/*&lt;br /&gt;
 * Copyright (c) 2006-2007, Johan Thelin&lt;br /&gt;
 * &lt;br /&gt;
 * All rights reserved.&lt;br /&gt;
 * &lt;br /&gt;
 * Redistribution and use in source and binary forms, with or without modification, &lt;br /&gt;
 * are permitted provided that the following conditions are met:&lt;br /&gt;
 * &lt;br /&gt;
 *     * Redistributions of source code must retain the above copyright notice, &lt;br /&gt;
 *       this list of conditions and the following disclaimer.&lt;br /&gt;
 *     * Redistributions in binary form must reproduce the above copyright notice,  &lt;br /&gt;
 *       this list of conditions and the following disclaimer in the documentation &lt;br /&gt;
 *       and/or other materials provided with the distribution.&lt;br /&gt;
 *     * Neither the name of APress nor the names of its contributors &lt;br /&gt;
 *       may be used to endorse or promote products derived from this software &lt;br /&gt;
 *       without specific prior written permission.&lt;br /&gt;
 * &lt;br /&gt;
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS&lt;br /&gt;
 * &amp;quot;AS IS&amp;quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT&lt;br /&gt;
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR&lt;br /&gt;
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR&lt;br /&gt;
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,&lt;br /&gt;
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,&lt;br /&gt;
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR&lt;br /&gt;
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF&lt;br /&gt;
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING&lt;br /&gt;
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS&lt;br /&gt;
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.&lt;br /&gt;
 *&lt;br /&gt;
 */&lt;br /&gt;
#include &amp;lt;QApplication&amp;gt;&lt;br /&gt;
#include &amp;lt;QThread&amp;gt;&lt;br /&gt;
#include &amp;lt;QSemaphore&amp;gt;&lt;br /&gt;
#include &amp;lt;QMutex&amp;gt;&lt;br /&gt;
#include &amp;lt;QtDebug&amp;gt;&lt;br /&gt;
const int bufferSize = 20;&lt;br /&gt;
QChar buffer[ bufferSize ];&lt;br /&gt;
QSemaphore freeSpace( bufferSize );&lt;br /&gt;
QSemaphore availableData( 0 );&lt;br /&gt;
QSemaphore atEnd( 0 );&lt;br /&gt;
class TextProducer : public QThread&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
  TextProducer( QString text );&lt;br /&gt;
  &lt;br /&gt;
  void run();&lt;br /&gt;
  &lt;br /&gt;
private:&lt;br /&gt;
  QString m_text;&lt;br /&gt;
};&lt;br /&gt;
TextProducer::TextProducer( QString text ) : QThread()&lt;br /&gt;
{&lt;br /&gt;
  atEnd.release();&lt;br /&gt;
  m_text = text;&lt;br /&gt;
}&lt;br /&gt;
void TextProducer::run()&lt;br /&gt;
{&lt;br /&gt;
  static int index = 0;&lt;br /&gt;
  static QMutex indexMutex;&lt;br /&gt;
  &lt;br /&gt;
  for( int i=0; i&amp;lt;m_text.length(); ++i )&lt;br /&gt;
  {&lt;br /&gt;
    freeSpace.acquire();&lt;br /&gt;
    indexMutex.lock();&lt;br /&gt;
    buffer[ index++ % bufferSize ] = m_text.data()[ i ];&lt;br /&gt;
    indexMutex.unlock();&lt;br /&gt;
    &lt;br /&gt;
    if( i == m_text.length()-1 )&lt;br /&gt;
      atEnd.acquire();&lt;br /&gt;
      &lt;br /&gt;
    availableData.release();&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
class TextConsumer : public QThread&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
  void run();&lt;br /&gt;
};&lt;br /&gt;
void TextConsumer::run()&lt;br /&gt;
{&lt;br /&gt;
  int i = 0;&lt;br /&gt;
  &lt;br /&gt;
  while( atEnd.available() || availableData.available() )&lt;br /&gt;
  {&lt;br /&gt;
    availableData.acquire();&lt;br /&gt;
    qDebug() &amp;lt;&amp;lt; buffer[ i ];&lt;br /&gt;
    i = (i+1) % bufferSize;&lt;br /&gt;
    freeSpace.release();&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
int main( int argc, char **argv )&lt;br /&gt;
{&lt;br /&gt;
  QApplication app( argc, argv );&lt;br /&gt;
  &lt;br /&gt;
  TextProducer p1( &amp;quot;this text is written using lower case characters.&amp;quot;&lt;br /&gt;
    &amp;quot;it will compete with a text written using upper case characters.&amp;quot; );&lt;br /&gt;
  TextProducer p2( &amp;quot;THIS TEXT IS WRITTEN USING UPPER CASE CHARACTERS!&amp;quot;&lt;br /&gt;
    &amp;quot;IT WILL COMPETE WITH A TEXT WRITTEN USING LOWER CASE CHARACTERS!&amp;quot; );&lt;br /&gt;
  TextConsumer consumer;&lt;br /&gt;
  &lt;br /&gt;
  p1.start();&lt;br /&gt;
  p2.start();&lt;br /&gt;
  consumer.start();&lt;br /&gt;
  &lt;br /&gt;
  p1.wait();&lt;br /&gt;
  p2.wait();&lt;br /&gt;
  consumer.wait();&lt;br /&gt;
    &lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
   &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Thread sleeps==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;  &lt;br /&gt;
Foundations of Qt Development\Chapter12\simplethreads\main.cpp&lt;br /&gt;
/*&lt;br /&gt;
 * Copyright (c) 2006-2007, Johan Thelin&lt;br /&gt;
 * &lt;br /&gt;
 * All rights reserved.&lt;br /&gt;
 * &lt;br /&gt;
 * Redistribution and use in source and binary forms, with or without modification, &lt;br /&gt;
 * are permitted provided that the following conditions are met:&lt;br /&gt;
 * &lt;br /&gt;
 *     * Redistributions of source code must retain the above copyright notice, &lt;br /&gt;
 *       this list of conditions and the following disclaimer.&lt;br /&gt;
 *     * Redistributions in binary form must reproduce the above copyright notice,  &lt;br /&gt;
 *       this list of conditions and the following disclaimer in the documentation &lt;br /&gt;
 *       and/or other materials provided with the distribution.&lt;br /&gt;
 *     * Neither the name of APress nor the names of its contributors &lt;br /&gt;
 *       may be used to endorse or promote products derived from this software &lt;br /&gt;
 *       without specific prior written permission.&lt;br /&gt;
 * &lt;br /&gt;
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS&lt;br /&gt;
 * &amp;quot;AS IS&amp;quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT&lt;br /&gt;
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR&lt;br /&gt;
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR&lt;br /&gt;
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,&lt;br /&gt;
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,&lt;br /&gt;
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR&lt;br /&gt;
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF&lt;br /&gt;
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING&lt;br /&gt;
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS&lt;br /&gt;
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.&lt;br /&gt;
 *&lt;br /&gt;
 */&lt;br /&gt;
#include &amp;lt;QApplication&amp;gt;&lt;br /&gt;
#include &amp;lt;QThread&amp;gt;&lt;br /&gt;
#include &amp;lt;QMessageBox&amp;gt;&lt;br /&gt;
#include &amp;lt;QtDebug&amp;gt;&lt;br /&gt;
class TextThread : public QThread&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
  TextThread( QString text );&lt;br /&gt;
  &lt;br /&gt;
  void run();&lt;br /&gt;
  &lt;br /&gt;
private:&lt;br /&gt;
  QString m_text;&lt;br /&gt;
};&lt;br /&gt;
bool stopThreads = false;&lt;br /&gt;
TextThread::TextThread( QString text ) : QThread()&lt;br /&gt;
{&lt;br /&gt;
  m_text = text;&lt;br /&gt;
}&lt;br /&gt;
void TextThread::run()&lt;br /&gt;
{&lt;br /&gt;
  while( !stopThreads )&lt;br /&gt;
  {&lt;br /&gt;
    qDebug() &amp;lt;&amp;lt; m_text;&lt;br /&gt;
    sleep( 1 );&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
int main( int argc, char **argv )&lt;br /&gt;
{&lt;br /&gt;
  QApplication app( argc, argv );&lt;br /&gt;
  &lt;br /&gt;
  TextThread foo( &amp;quot;Foo&amp;quot; ), bar( &amp;quot;Bar&amp;quot; );&lt;br /&gt;
  &lt;br /&gt;
  foo.start();&lt;br /&gt;
  bar.start();&lt;br /&gt;
  &lt;br /&gt;
  QMessageBox::information( 0, &amp;quot;Threading&amp;quot;, &amp;quot;Close me to stop!&amp;quot; );&lt;br /&gt;
  &lt;br /&gt;
  stopThreads = true;&lt;br /&gt;
  &lt;br /&gt;
  foo.wait();&lt;br /&gt;
  bar.wait();&lt;br /&gt;
    &lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
   &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Using QThread==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;  &lt;br /&gt;
/****************************************************************************&lt;br /&gt;
**&lt;br /&gt;
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).&lt;br /&gt;
** All rights reserved.&lt;br /&gt;
** Contact: Nokia Corporation (qt-info@nokia.com)&lt;br /&gt;
**&lt;br /&gt;
** This file is part of the documentation of the Qt Toolkit.&lt;br /&gt;
**&lt;br /&gt;
** $QT_BEGIN_LICENSE:LGPL$&lt;br /&gt;
** Commercial Usage&lt;br /&gt;
** Licensees holding valid Qt Commercial licenses may use this file in&lt;br /&gt;
** accordance with the Qt Commercial License Agreement provided with the&lt;br /&gt;
** Software or, alternatively, in accordance with the terms contained in&lt;br /&gt;
** a written agreement between you and Nokia.&lt;br /&gt;
**&lt;br /&gt;
** GNU Lesser General Public License Usage&lt;br /&gt;
** Alternatively, this file may be used under the terms of the GNU Lesser&lt;br /&gt;
** General Public License version 2.1 as published by the Free Software&lt;br /&gt;
** Foundation and appearing in the file LICENSE.LGPL included in the&lt;br /&gt;
** packaging of this file.  Please review the following information to&lt;br /&gt;
** ensure the GNU Lesser General Public License version 2.1 requirements&lt;br /&gt;
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.&lt;br /&gt;
**&lt;br /&gt;
** In addition, as a special exception, Nokia gives you certain additional&lt;br /&gt;
** rights.  These rights are described in the Nokia Qt LGPL Exception&lt;br /&gt;
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.&lt;br /&gt;
**&lt;br /&gt;
** GNU General Public License Usage&lt;br /&gt;
** Alternatively, this file may be used under the terms of the GNU&lt;br /&gt;
** General Public License version 3.0 as published by the Free Software&lt;br /&gt;
** Foundation and appearing in the file LICENSE.GPL included in the&lt;br /&gt;
** packaging of this file.  Please review the following information to&lt;br /&gt;
** ensure the GNU General Public License version 3.0 requirements will be&lt;br /&gt;
** met: http://www.gnu.org/copyleft/gpl.html.&lt;br /&gt;
**&lt;br /&gt;
** If you have questions regarding the use of this file, please contact&lt;br /&gt;
** Nokia at qt-info@nokia.com.&lt;br /&gt;
** $QT_END_LICENSE$&lt;br /&gt;
**&lt;br /&gt;
****************************************************************************/&lt;br /&gt;
#include &amp;lt;QThread&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class MyThread : public QThread&lt;br /&gt;
{&lt;br /&gt;
    Q_OBJECT&lt;br /&gt;
protected:&lt;br /&gt;
    void run();&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;QCache&amp;gt;&lt;br /&gt;
#include &amp;lt;QMutex&amp;gt;&lt;br /&gt;
#include &amp;lt;QThreadStorage&amp;gt;&lt;br /&gt;
#include &amp;quot;threads.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
void MyThread::run()&lt;br /&gt;
 &lt;br /&gt;
{&lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
#define Counter ReentrantCounter&lt;br /&gt;
&lt;br /&gt;
class Counter&lt;br /&gt;
 &lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    Counter() { n = 0; }&lt;br /&gt;
    void increment() { ++n; }&lt;br /&gt;
    void decrement() { --n; }&lt;br /&gt;
    int value() const { return n; }&lt;br /&gt;
private:&lt;br /&gt;
    int n;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
#undef Counter&lt;br /&gt;
#define Counter ThreadSafeCounter&lt;br /&gt;
&lt;br /&gt;
class Counter&lt;br /&gt;
 &lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    Counter() { n = 0; }&lt;br /&gt;
    void increment() { QMutexLocker locker(&amp;amp;mutex); ++n; }&lt;br /&gt;
    void decrement() { QMutexLocker locker(&amp;amp;mutex); --n; }&lt;br /&gt;
    int value() const { QMutexLocker locker(&amp;amp;mutex); return n; }&lt;br /&gt;
private:&lt;br /&gt;
    mutable QMutex mutex;&lt;br /&gt;
    int n;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
typedef int SomeClass;&lt;br /&gt;
&lt;br /&gt;
QThreadStorage&amp;lt;QCache&amp;lt;QString, SomeClass&amp;gt; *&amp;gt; caches;&lt;br /&gt;
void cacheObject(const QString &amp;amp;key, SomeClass *object)&lt;br /&gt;
 &lt;br /&gt;
{&lt;br /&gt;
    if (!caches.hasLocalData())&lt;br /&gt;
        caches.setLocalData(new QCache&amp;lt;QString, SomeClass&amp;gt;);&lt;br /&gt;
    caches.localData()-&amp;gt;insert(key, object);&lt;br /&gt;
}&lt;br /&gt;
void removeFromCache(const QString &amp;amp;key)&lt;br /&gt;
 &lt;br /&gt;
{&lt;br /&gt;
    if (!caches.hasLocalData())&lt;br /&gt;
        return;&lt;br /&gt;
    caches.localData()-&amp;gt;remove(key);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
   &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Wait condition==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;  &lt;br /&gt;
/****************************************************************************&lt;br /&gt;
**&lt;br /&gt;
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).&lt;br /&gt;
** All rights reserved.&lt;br /&gt;
** Contact: Nokia Corporation (qt-info@nokia.com)&lt;br /&gt;
**&lt;br /&gt;
** This file is part of the examples of the Qt Toolkit.&lt;br /&gt;
**&lt;br /&gt;
** $QT_BEGIN_LICENSE:LGPL$&lt;br /&gt;
** Commercial Usage&lt;br /&gt;
** Licensees holding valid Qt Commercial licenses may use this file in&lt;br /&gt;
** accordance with the Qt Commercial License Agreement provided with the&lt;br /&gt;
** Software or, alternatively, in accordance with the terms contained in&lt;br /&gt;
** a written agreement between you and Nokia.&lt;br /&gt;
**&lt;br /&gt;
** GNU Lesser General Public License Usage&lt;br /&gt;
** Alternatively, this file may be used under the terms of the GNU Lesser&lt;br /&gt;
** General Public License version 2.1 as published by the Free Software&lt;br /&gt;
** Foundation and appearing in the file LICENSE.LGPL included in the&lt;br /&gt;
** packaging of this file.  Please review the following information to&lt;br /&gt;
** ensure the GNU Lesser General Public License version 2.1 requirements&lt;br /&gt;
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.&lt;br /&gt;
**&lt;br /&gt;
** In addition, as a special exception, Nokia gives you certain additional&lt;br /&gt;
** rights.  These rights are described in the Nokia Qt LGPL Exception&lt;br /&gt;
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.&lt;br /&gt;
**&lt;br /&gt;
** GNU General Public License Usage&lt;br /&gt;
** Alternatively, this file may be used under the terms of the GNU&lt;br /&gt;
** General Public License version 3.0 as published by the Free Software&lt;br /&gt;
** Foundation and appearing in the file LICENSE.GPL included in the&lt;br /&gt;
** packaging of this file.  Please review the following information to&lt;br /&gt;
** ensure the GNU General Public License version 3.0 requirements will be&lt;br /&gt;
** met: http://www.gnu.org/copyleft/gpl.html.&lt;br /&gt;
**&lt;br /&gt;
** If you have questions regarding the use of this file, please contact&lt;br /&gt;
** Nokia at qt-info@nokia.com.&lt;br /&gt;
** $QT_END_LICENSE$&lt;br /&gt;
**&lt;br /&gt;
****************************************************************************/&lt;br /&gt;
#include &amp;lt;QtCore&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
const int DataSize = 100000;&lt;br /&gt;
const int BufferSize = 8192;&lt;br /&gt;
char buffer[BufferSize];&lt;br /&gt;
QWaitCondition bufferNotEmpty;&lt;br /&gt;
QWaitCondition bufferNotFull;&lt;br /&gt;
QMutex mutex;&lt;br /&gt;
int numUsedBytes = 0;&lt;br /&gt;
&lt;br /&gt;
class Producer : public QThread&lt;br /&gt;
   &lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    void run();&lt;br /&gt;
};&lt;br /&gt;
void Producer::run()&lt;br /&gt;
{&lt;br /&gt;
    qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));&lt;br /&gt;
    &lt;br /&gt;
    for (int i = 0; i &amp;lt; DataSize; ++i) {&lt;br /&gt;
        mutex.lock();&lt;br /&gt;
        if (numUsedBytes == BufferSize)&lt;br /&gt;
            bufferNotFull.wait(&amp;amp;mutex);&lt;br /&gt;
        mutex.unlock();&lt;br /&gt;
        buffer[i % BufferSize] = &amp;quot;ACGT&amp;quot;[(int)qrand() % 4];&lt;br /&gt;
        mutex.lock();&lt;br /&gt;
        ++numUsedBytes;&lt;br /&gt;
        bufferNotEmpty.wakeAll();&lt;br /&gt;
        mutex.unlock();&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Consumer : public QThread&lt;br /&gt;
 &lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    void run();&lt;br /&gt;
};&lt;br /&gt;
void Consumer::run()&lt;br /&gt;
{&lt;br /&gt;
    for (int i = 0; i &amp;lt; DataSize; ++i) {&lt;br /&gt;
        mutex.lock();&lt;br /&gt;
        if (numUsedBytes == 0)&lt;br /&gt;
            bufferNotEmpty.wait(&amp;amp;mutex);&lt;br /&gt;
        mutex.unlock();&lt;br /&gt;
        fprintf(stderr, &amp;quot;%c&amp;quot;, buffer[i % BufferSize]);&lt;br /&gt;
        mutex.lock();&lt;br /&gt;
        --numUsedBytes;&lt;br /&gt;
        bufferNotFull.wakeAll();&lt;br /&gt;
        mutex.unlock();&lt;br /&gt;
    }&lt;br /&gt;
    fprintf(stderr, &amp;quot;\n&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[])&lt;br /&gt;
 &lt;br /&gt;
{&lt;br /&gt;
    QCoreApplication app(argc, argv);&lt;br /&gt;
    Producer producer;&lt;br /&gt;
    Consumer consumer;&lt;br /&gt;
    producer.start();&lt;br /&gt;
    consumer.start();&lt;br /&gt;
    producer.wait();&lt;br /&gt;
    consumer.wait();&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
   &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>