#include <QCoreApplication>#include <QStringList>#include <QThread>#include <QTime>static const char *chars = "0123456789";#include "util.h"#include "testfunctions.h"SharedString::Data SharedString::shared_null = { 1, 0, 0, 0, { 0 } };AtomicString::Data AtomicString::shared_null = { Q_ATOMIC_INIT(1), 0, 0, 0, { 0 } };#ifdef Q_OS_WIN#include <io.h>#define isatty _isatty#endifenum { TimeToRun = 1000, DefaultMeasurementInterval = 1000 };int MeasurementInterval = DefaultMeasurementInterval;template <typename T>void ensureLength(T &s, int length){ s = T(); int i = 0; while (s.length() < length) { s += chars[i++ % 10]; }}template <typename T, void (*F)(T &, int &, const int &)>class TestThread : public QThread{ const int length;public: TestThread(int length) : length(length), result(0) { } int result; void run() { T s; ensureLength(s, length); int i = 0; int elapsed; QTime t; t.start(); do { for (int k = 0; k < MeasurementInterval; ++k) { F(s, i, length); ++i; } elapsed = t.elapsed(); } while (elapsed < TimeToRun); result = i / elapsed; }};template <typename T, void (*F)(T &, int &, const int &)>void runTest(int length, int threadCount){ if (isatty(fileno(stdout))) { printf("Calibrating... "); fflush(stdout); } // calibration MeasurementInterval = DefaultMeasurementInterval; forever { QTime t; t.start(); T x; ensureLength(x, length); for (int k = 0; k < MeasurementInterval; ++k) F (x, k, length); int elapsed = t.elapsed(); if (elapsed < TimeToRun / 100) { MeasurementInterval *= 10; continue; } else if (elapsed < TimeToRun / 10) { MeasurementInterval *= 2; continue; } break; } if (isatty(fileno(stdout))) { // erase "Calibrating... " printf("\r "); printf("\rMeasuring... "); fflush(stdout); } TestThread<T, F> **threads = new TestThread<T, F>*[threadCount]; for (int i = 0; i < threadCount; ++i) { threads[i] = new TestThread<T, F>(length); } // always run thread[0] separately threads[0]->run(); // run rest of threads for (int i = 1; i < threadCount; ++i) { threads[i]->start(); } for (int i = 1; i < threadCount; ++i) { threads[i]->wait(); } // print results and cleanup if (isatty(fileno(stdout))) printf("\r"); printf(" length %4d", length); for (int i = 0; i < threadCount; ++i) { printf(", thr%c: %6d per-ms", 'A' + i, threads[i]->result); delete threads[i]; } delete [] threads; printf("\n"); fflush(stdout);}#define RUN(Class, TestFunction) \ do { \ int i = 0; \ printf("%s %s:\n", #Class, #TestFunction); \ do { \ int length = lengths[i]; \ int threads = strcmp("SharedString", #Class) == 0 ? 1 : 3; \ runTest<Class, TestFunction>(length, threads); \ } while(lengths[++i]!=0); \ printf("\n"); \ } while(false)int main(int argc, char **argv){ QCoreApplication app(argc, argv); bool comparison = app.arguments().contains("-comparison"); int lengths[] = { 10, 100, 1000, 0 }; printf("Time to run = %d ms\n" "--------------------------------------------------\n\n", TimeToRun); RUN(SimpleString, copyConstruction); RUN(SimpleString2, copyConstruction); RUN(SharedString, copyConstruction); RUN(AtomicString, copyConstruction); if (comparison) { RUN(std::string, copyConstruction); RUN(QByteArray, copyConstruction); } printf("--------------------------------------------------\n\n"); fflush(stdout); RUN(SimpleString, appendSingleCharacter); RUN(SimpleString2, appendSingleCharacter); RUN(SharedString, appendSingleCharacter); RUN(AtomicString, appendSingleCharacter); if (comparison) { RUN(std::string, appendSingleCharacter); RUN(QByteArray, appendSingleCharacter); } printf("--------------------------------------------------\n\n"); fflush(stdout); RUN(SimpleString, nonMutatingAccess); RUN(SimpleString2, nonMutatingAccess); RUN(SharedString, nonMutatingAccess); RUN(AtomicString, nonMutatingAccess); if (comparison) { RUN(std::string, nonMutatingAccess); RUN(QByteArray, nonMutatingAccess); } printf("--------------------------------------------------\n\n"); fflush(stdout); RUN(SimpleString, nonMutatingAccessAfterCopy); RUN(SimpleString2, nonMutatingAccessAfterCopy); RUN(SharedString, nonMutatingAccessAfterCopy); RUN(AtomicString, nonMutatingAccessAfterCopy); if (comparison) { RUN(std::string, nonMutatingAccessAfterCopy); RUN(QByteArray, nonMutatingAccessAfterCopy); } printf("--------------------------------------------------\n\n"); fflush(stdout); RUN(SimpleString, nonMutatingAccessOnCopy); RUN(SimpleString2, nonMutatingAccessOnCopy); RUN(SharedString, nonMutatingAccessOnCopy); RUN(AtomicString, nonMutatingAccessOnCopy); if (comparison) { RUN(std::string, nonMutatingAccessOnCopy); RUN(QByteArray, nonMutatingAccessOnCopy); } printf("--------------------------------------------------\n\n"); fflush(stdout); RUN(SimpleString, mutatingCopy1); RUN(SimpleString2, mutatingCopy1); RUN(SharedString, mutatingCopy1); RUN(AtomicString, mutatingCopy1); if (comparison) { RUN(std::string, mutatingCopy1); RUN(QByteArray, mutatingCopy1); } printf("--------------------------------------------------\n\n"); fflush(stdout); RUN(SimpleString, mutatingCopy2); RUN(SimpleString2, mutatingCopy2); RUN(SharedString, mutatingCopy2); RUN(AtomicString, mutatingCopy2); if (comparison) { RUN(std::string, mutatingCopy2); RUN(QByteArray, mutatingCopy2); } printf("--------------------------------------------------\n\n"); fflush(stdout); RUN(SimpleString, functionWithArgument); RUN(SimpleString2, functionWithArgument); RUN(SharedString, functionWithArgument); RUN(AtomicString, functionWithArgument); if (comparison) { RUN(std::string, functionWithArgument); RUN(QByteArray, functionWithArgument); } printf("--------------------------------------------------\n\n"); fflush(stdout); RUN(SimpleString, functionWithTemporaryArgument); RUN(SimpleString2, functionWithTemporaryArgument); RUN(SharedString, functionWithTemporaryArgument); RUN(AtomicString, functionWithTemporaryArgument); if (comparison) { RUN(std::string, functionWithTemporaryArgument); RUN(QByteArray, functionWithTemporaryArgument); } printf("--------------------------------------------------\n\n"); fflush(stdout); RUN(SimpleString, functionReturningCopy); RUN(SimpleString2, functionReturningCopy); RUN(SharedString, functionReturningCopy); RUN(AtomicString, functionReturningCopy); if (comparison) { RUN(std::string, functionReturningCopy); RUN(QByteArray, functionReturningCopy); } printf("--------------------------------------------------\n\n"); fflush(stdout); return 0;}
#include "DeleteTest.h"#include <QChar>#include <QString>#include <QObject>#include <QThread>#include <QtDebug>#include <QtTest/QtTest>#include <Job.h>#include <State.h>#include <QueuePolicy.h>#include <JobSequence.h>#include <JobCollection.h>#include <DebuggingAids.h>#include <WeaverObserver.h>#include <DependencyPolicy.h>#include <ResourceRestrictionPolicy.h>#include <ThreadWeaver.h>#include <Thread.h>#include "AppendCharacterJob.h"int main(int argc, char **argv){ DeleteTest dt(argc, argv); return dt.exec();}DeleteTest::DeleteTest(int argc, char **argv) : QCoreApplication(argc, argv){ ThreadWeaver::setDebugLevel ( true, 3 ); ThreadWeaver::Weaver::instance()->setMaximumNumberOfThreads(4); m_finishCount = 100; for (int i = 0; i < 100; ++i) { ThreadWeaver::JobSequence* jobSeq = new ThreadWeaver::JobSequence( this ); connect ( jobSeq, SIGNAL( done( Job* ) ), SLOT( deleteSequence( Job* ) ) ); jobSeq->addJob( new BusyJob ); jobSeq->addJob( new BusyJob ); ThreadWeaver::Weaver::instance()->enqueue( jobSeq ); } ThreadWeaver::Weaver::instance()->resume();}void DeleteTest::deleteSequence(Job* job){ Q_ASSERT(job); delete job; QMutexLocker lock(&m_finishMutex); --m_finishCount; if (m_finishCount == 0) exit(0);}QMutex s_GlobalMutex;#include "DeleteTest.moc"
QEventLoop el; //член данных класса//организация задержкиQTimer::singleShot(600000, this, SLOT(timeout()));el.exec();void timeout() //слот того же класса{ el.quit();}
ADeamon::ADeamon(QObject* parent) : QObject(parent) { timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(update())); timer->start(10000);}void ADeamon::update() { qWarning("Oksss");}
main.cpp: In function ‘int main(int, char**)’:main.cpp:7: warning: unused variable ‘deamon’
#include <QtCore>#include "adeamon.h"int main(int argc, char* argv[]) { QCoreApplication* app = new QCoreApplication(argc, argv); ADeamon* deamon = new ADeamon(app); return app->exec();}