Qt World Summit: Register Today!

Please help to synchronize the execution of processes

  • Hi again, dear community. Please, help me to solve the following question or give a reference to the information or better - to custom example. I have such a problem. There are two processes. The first process sends (emit) a signal with the parameter QImage* p_img to the second class , which saves the image *p_img to the disk and removes p_img. Of course the second process performs its task more slowly than the first one and data is accumulated in RAM as the calls of unprocessed signals are in the queue. Here is the question -how is it possible with Qt libraries to implement the living queue for 3 processes. Example - implementation of select{..} in Golang

  • @smartcoder.v.arch said in Please help to synchronize the execution of processes:

    implementation of select{..} in Golang

    the closest thin is QtConcurrent, something like http://doc.qt.io/qt-5/qtconcurrent.html#run

    The rest of the question is quite confusing as you are mixing processes an threads. if you make what you are trying to do more clear we might be more helpful

  • @VRonin Thanks for Your answer. I'll try to explain. My program is designed to convert video into gif. The code will be opened, but it is not about that right now. I use "QAbstractVideoSurface" class and in its method "QAbstractVideoSurface::present" with the help of a signal I transmit "void frameAvailable (QImage p_img)" pass a pointer of the image to another class, the task of which is to perform the change of the size of the image, that p_img pointer points to and saving of it to the hard drive and then release the memory that allocated to p_img. The problem is that the size of the operation of the saving and the changing of the size performs for too long and the calls "void frameAvailable(QImage p_img)" wait for the processing and take quite much of the RAM. I would like to make 3 or more processes gain data from "void frameAvailable(QImage* p_img)", and as soon as one of these processes will complete its operation, it accepts the new task, that is, to organize a sort of instruction pipeline, where one process conveys the instructions to 5 processes-workers and they perform it simultaneously.

  • @VRonin Sorry , I WAS WRONG - the stream (not a process)

  • // declare this global function
    void saveImage(QImage* p_img){
    // do your stuff here
    // p_img->scaled().save();
    delete p_img;
    // connect(videosurface,&MyVideoSurface::frameAvailable,someclass,&SomeClass::saveFrame);
    void SomeClass::saveFrame(QImage* p_img){

  • @VRonin Thank you. I'll try your solution and will write you tomorrow here

  • @VRonin One more question - how safe is your method? As I understand the function will be called in a new stream each time?.. How many streams the same time is acceptably? What will happen if the number of calls of function will achieve 1000?

  • Lifetime Qt Champion

    It most likely depends on the hardware if it can handle 1000 concurrent saveImage.
    Also, the bottleneck could be the IO system, if they are accessing the drive to store at the same time.
    Furthermore, some platforms might have limited amount of open file handles
    possible and many other factors like that.

    You can control how many is allowed with
    ( as far as i know)

  • QtConcurrent is multi-threading made easy. it has some limitations (that can be overcame with QThread) but it is also safer than a naive call to std::async

    from http://doc.qt.io/qt-5/qtconcurrent-index.html

    QtConcurrent automatically adjust the number of threads used according to the number of processor cores available.

    What will happen if the number of calls of function will achieve 1000?

    it will execute QThread::idealThreadCount() saves at the same time and queue the others as stated here: http://doc.qt.io/qt-5/qtconcurrent.html#run

    Note that function may not run immediately; function will only be run once a thread becomes available.

    what you call stream is called thread

  • Thank Friends for Your answers . And sorry for my English.

Log in to reply