C++ (Qt)class Searcher;class MyThread : public QThread{private: Searcher *_searcher; void *pBuf; qint64 bufSize; bool stop;public: void run(); void setScanBuf(void *buf, qint64 size) { pBuf = buf; bufSize = size; }};
C++ (Qt)void MyThread::run(){ while (!stop) { bool found = _searcher->Search(pBuf, bufSize); emit result(found); waitForSomeMutex(someMutex); }}
class Searcher;class MyThread : public QThread{ Q_OBJECT private: QEventLoop *_eventLoop; Searcher *_searcher; void *pBuf; qint64 bufSize; bool stop;public: void run(); void setScanBuf(void *buf, qint64 size) { pBuf = buf; bufSize = size; } void seach() { emit exitLoop(); }signals: void exitLoop();};
void MyThread::run(){ QEventLoop eventLoop; connect(this, SIGNAL(exitLoop()), &eventLoop, SLOT(quit()), Qt::QueuedConnection); while (!stop) { eventLoop.exec(); bool found = _searcher->Search(pBuf, bufSize); emit result(found); }}
C++ (Qt)void MyThread::run(){ while (true) { void * pBuf; qint64 bufSize; mSemaphore.acquire(); // ждем на семафоре если нет буферов if (stop) return; else { QMutexLocker locker(&theBufMuteх); // захватываем контейнер буферов pBuf = GetBuf(&bufSize); // извлекаем буфер из контейнера (takeAt) if (!pBuf) continue; // подстраховались if (pBuf == theEndOfJob) return; // часто так лучше чем stop } bool found = _searcher->Search(pBuf, bufSize); emit result(found); }}
for (int i = 0; i < threadCount; ++i)_treads[i]->start();
for (int i = 0; i < threadCount; ++i){ _treads[i]->exit(0); _treads[i]->wait(); _treads[i]->setScanBuf(pBuf, bufSize); _treads[i]->start();}
for (int i = 0; i < threadCount; ++i){// _treads[i]->exit(0); <== выкидываем// _treads[i]->wait(); <== выкидываем _treads[i]->setScanBuf(pBuf, bufSize); _treads[i]->seach(); << == добавляем// _treads[i]->start(); <== выкидываем}
eventLoop.exec();