About threads



  • Hello

    The around thread becomes new from R20 and it does not work with the old code.
    I am trying while looking at the SDK sample, but it does not move with an error.

    No 1 error
    An operator that handles the right operand of maxon :: ResultMemT <maxon :: BaseRef <IMPLEMENTATION, maxon :: StrongRefHandler >> 'can not be found (or can not be converted).

    class MyThread : public maxon::ThreadInterfaceTemplate<MyThread>
    {
    public:
    const char* GetName() const { return "MyThread"; }
    maxon::Result<void> operator ()()
    {
    GePrint("ok"_s);
    }
    };

    class MenuTest : public CommandData
    {
    static maxon::ThreadRefTemplate<MyThread> g_test;
    public:
    virtual Bool Execute(BaseDocument
    doc);
    };

    Bool MenuTest::Execute(BaseDocument doc)
    {
    if (!g_test) g_test = MyThread::Create() iferr_return; // No1 Error
    }

    Please tell me if you can understand.


  • Global Moderator

    Hello,

    there are a few issues with the code you posted.

    First, please make sure to use the correct function declarations. CommandData::Execute() is defined as

    Execute(BaseDocument* doc)
    

    The doc argument is a pointer, not a copy.

    Second, please pay attention to return values. The Execute() function must return a Bool value. The operator()() of the thread must return a maxon::Result<void>.

    In the Execute() function you create a thread instance. You must handle the error object returned by the Create() function.

    There are multiple ways to handle such error values. You find examples in the Error Handling manual.

    One way would be to use iferr_scope_handler. With that the Execute() function can look like this:

    Bool MenuTest::Execute(BaseDocument* doc)
    {
      iferr_scope_handler
      {
        err.DiagOutput();
        err.DbgStop();
        return true;
      };
    
      if (!g_test)
      {
        g_test = MyThread::Create() iferr_return; 
      }
    
      return true;
    }
    

    The worker function (operator()()) of the thread must return a Result<void> object. This is typically maxon::OK when everything is OK. You find an example of such a custom thread in the Threads Manual.

    Some more hints:

    best wishes,
    Sebastian



  • Hi.

    Thank you for teaching me.
    I am not sure of the new maxon API Threading.
    Are there samples of jobs and Threads?


  • Global Moderator

    Hello,

    you find information and examples on jobs and threads in these manuals:

    best wishes,
    Sebastian