Facing problem in use of QtConcurrent.



  • I wanted to run a function in different thread. I am using QtConcurrent but facing some problem, though I am not sure whether I should use QtConcurrent for the requirement or not.
    @
    bool fun(bool f)
    {
    if (f==true)flag = true;
    return flag;
    }

    void test()
    {
    QtConcurrent::run(fun, true);
    }

    // calling test() in main()
    @

    Execution control is going into trans state. I put a break point into fun() but it never gets hit.
    If I put a wait into test() such a way, so that fun() execute first then only test() gets end, then things go correct.

    It seems since test() is creating new thread to run fun() and completed its execution, so created thread is also gets killed because of that control is going into trans state.
    Please correct me.



  • It is not clear to me what the problem is, but you are launching a function on a thread and are not evaluating any result. What are expecting this to be? Also please note that the documentation states that:

    bq. Note that the function may not run immediately; the function will only be run when a thread is available.

    So maybe your application exits before fun has a chance to run?



  • It seems like we are not allowed to call member function of a class in QtConcurrent::run(). If it is possible please let me know how to call it.
    Basically I have a class which has two functions wanted to run one in different thread from 2nd. Code that I want will be like below,
    @class test
    {
    ....
    void fun();
    void funDiffthrd();
    };

    void test::fun()
    {
    QtConcurrent::run(funDiffthrd); // wanted to acheive this.
    }@



  • Well, I have use QtConcurrent successfully to scan directory tree and place findings to a list. The call looks following ("projects" is instance of moe::ProjectDB):

    [code]
    QtConcurrent::run(&projects, &moe::ProjectDB::populate)
    [/code]

    [url=https://gitorious.org/moe/moe/blobs/master/moeqt/sources/prjmgr/moeProjectView.cpp#line84](Source: moeProjectView.cpp, line 84)[/url].



  • I guess you have to specify the instance on which the method has to be called.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.