Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Use QScript in multithread
Qt 6.11 is out! See what's new in the release blog

Use QScript in multithread

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 539 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    coderKnight
    wrote on last edited by
    #1

    I am using a public "QScriptEngine" object in multithread,but sometimes ,when I call "evaluate" function to evaluate the script,it returned failure,is it nessary for me to put a lock before call the "QScriptEngine" object functions?
    another problem is ,I need call C++ functions in script,I create a C++ class like this:

    class scriptApi : public QObject
    {
    Q_OBJECT
    public:
    explicit
    scriptApi(QObject *parent = nullptr);
    Q_INVOKABLE QString function1(QString ss);
    Q_INVOKABLE QString function2(QString signalName,QString alue);
    Q_INVOKABLE QString function3(QString signalName,QString newValue);
    Q_INVOKABLE QString function4(QString signalName);
    }
    is it possiable to use the C++ class in diffreent "QScriptEngine" objects?Do I need put a lock in static C++ functions?
    Thanks

    jsulmJ 1 Reply Last reply
    0
    • C coderKnight

      I am using a public "QScriptEngine" object in multithread,but sometimes ,when I call "evaluate" function to evaluate the script,it returned failure,is it nessary for me to put a lock before call the "QScriptEngine" object functions?
      another problem is ,I need call C++ functions in script,I create a C++ class like this:

      class scriptApi : public QObject
      {
      Q_OBJECT
      public:
      explicit
      scriptApi(QObject *parent = nullptr);
      Q_INVOKABLE QString function1(QString ss);
      Q_INVOKABLE QString function2(QString signalName,QString alue);
      Q_INVOKABLE QString function3(QString signalName,QString newValue);
      Q_INVOKABLE QString function4(QString signalName);
      }
      is it possiable to use the C++ class in diffreent "QScriptEngine" objects?Do I need put a lock in static C++ functions?
      Thanks

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @coderKnight said in Use QScript in multithread:

      I am using a public "QScriptEngine" object in multithread

      Why? QScriptEngine is not thread safe.

      "when I call "evaluate" function to evaluate the script,it returned failure" - does it also happen without multithreading?

      "Do I need put a lock in static C++ functions?" - depends what they do.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      C 1 Reply Last reply
      0
      • jsulmJ jsulm

        @coderKnight said in Use QScript in multithread:

        I am using a public "QScriptEngine" object in multithread

        Why? QScriptEngine is not thread safe.

        "when I call "evaluate" function to evaluate the script,it returned failure" - does it also happen without multithreading?

        "Do I need put a lock in static C++ functions?" - depends what they do.

        C Offline
        C Offline
        coderKnight
        wrote on last edited by
        #3

        @jsulm
        sorry reply late,QScript works well without multithread,but my project should use QScript in mutithread, I don't know what can I do

        jsulmJ 1 Reply Last reply
        0
        • C coderKnight

          @jsulm
          sorry reply late,QScript works well without multithread,but my project should use QScript in mutithread, I don't know what can I do

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @coderKnight said in Use QScript in multithread:

          I don't know what can I do

          Make your code thread safe

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          C 1 Reply Last reply
          0
          • jsulmJ jsulm

            @coderKnight said in Use QScript in multithread:

            I don't know what can I do

            Make your code thread safe

            C Offline
            C Offline
            coderKnight
            wrote on last edited by
            #5

            @jsulm
            I create diffrent QScriptEngine ‘s in diffrent thread,but when call C++ function in script,Qt complaint as below:
            QScriptValue::call() failed: cannot call function with argument created in a different engine
            Why did I get this?

            jsulmJ 1 Reply Last reply
            0
            • C coderKnight

              @jsulm
              I create diffrent QScriptEngine ‘s in diffrent thread,but when call C++ function in script,Qt complaint as below:
              QScriptValue::call() failed: cannot call function with argument created in a different engine
              Why did I get this?

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @coderKnight said in Use QScript in multithread:

              Why did I get this?

              The error tells you why.
              You are calling a function and passing it arguments which were created in a different thread...

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              2

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved