1
votes

I have a multithreaded program that downloads and gets information from a website, then takes the info, creates a object that i then add to my GridLayout with a image.

I have thousands of objects that i need created and then added to my gridlayout, using 8 threads to get the information and those same 8 threads then create the objects and add them to 8 different grids.

After the program runs for about 20 seconds, i get a
QThread::start: Failed to create thread (The access code is invalid.) errors. After another few more seconds i get some Runtime C++ Errors that hang and don't display a message. What does this imply? What confuses me, is that at this point, my threads are already created and im not actually creating more threads.. inside those threads though they are each creating Widget Objects and then im sending those to the Main Gridlayout..

I checked the amount of threads i have running (QThread:idealThreadCount) which returns only 8.

I'm multithreading to download the information from the sites, which then each records returns values back which i created into a Object to put into my GridLayout.

Im thinking because at that point, these objects all belong to the single thread, and not ever emit(finished()) until all urls are done, so its hogging something and causing this error?

Should i try MoveToThread and send all these objects back to the Main thread after the information is downladed and the new object is added to my gridlayout?

Does any of this make sense of what is happening, and if do you want me to post the code?

Thanks.

Edit:

Just tried moving the threads after creation, but learnt QObject::moveToThread: Widgets cannot be moved to a new thread

Here is my creation of threads

void checkNewArrivals::createWorkers(QString url, QString category, QString subCategory){
    QThread* thread = new QThread;
    checkNewArrivalWorker* worker = new checkNewArrivalWorker(url, category, subCategory);
    worker->moveToThread(thread);
    connect(worker, SIGNAL(error(QString)), this, SLOT(errorString(QString)));
    connect(thread, SIGNAL(started()), worker, SLOT(process()));
    connect(worker, SIGNAL(finished()), thread, SLOT(quit()));
    connect(worker, SIGNAL(result(QString,QString,QString,QString,QString,int, int)), this, SLOT(addItem(QString,QString,QString,QString,QString,int, int)));
    connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()));
    connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
    thread->start();
}

Here is my worker class function that emits the results

void checkNewArrivalWorker::getHtml(QString url){
    html = QString::fromStdString(Curl->getWebsiteHtml(url.toStdString()));
    html = html.mid(html.indexOf("Goods List"));
    html = html.mid(0, html.indexOf("footer"));

    for (int i = 0; i < html.count("GoodsBox"); i++){
        //blah blah blah blah


        emit result(idLink, picLink, price, category, subCategory, row, col);
        col++;
        if (col == 5){
            col = 0;
            row++;
        }

        html = html.replace(itemRow, "");
    }
}

Then i add the results to create a item and add it to a gridlayout

void checkNewArrivals::addToGrid(QGridLayout *layout, QString id, QString picUrl, QString usPrice, int row, int col){
    checkNewArrivalItem* item = new checkNewArrivalItem;
    if (item->setupItem(id, picUrl, usPrice) == true){
        layout->addWidget(item, row, col);
    };
}

These items work fine, until a overload of items i think.. not too sure why im getting this error.

1

1 Answers

3
votes

Qt GUI objects cannot exist in any thread other than the main thread. The GUI objects need to be created and added to the layout in the main thread. You can still gather data from background threads, but you'll need to pass it back to the main thread for display. For that, I recommend using signals, as signal/slot connections are thread-safe by default.

Edit: Your new threading looks like it should work. However, I think you'd find it preferable to keep a specific number of threads running and delegate the work to them. For one, creating, starting, stopping and destroying threads is an expensive process. But more importantly, perhaps, is that you might just be creating too many threads.

Just to be really clear, QThread::idealThreadCount() is not the number of threads you have running, but is the number of threads your CPU can most efficiently handle. I'm guessing you have a quad-core CPU with hyperthreading, making it 8.

I think it's most likely that you're creating too many threads. This sounds a lot like the time when I mistakenly did that.