Wie kann ich bei Boost-Threads Variablen übergeben?



  • Ich habe eine Funktion:

    void threadFun(int a,int b)
    {
    printf("%i %i"a,b);
    }

    diese rufe ich als Boost-Thread wie folgt auf:

    *int main()
    {
    int a=1,b=2;

    boost::thread_group grp;
    grp.create_thread(threadFun(a,b));
    boost::thread::yield();
    grp.join_all();

    return 0;
    }*
    Folgende Fehlermeldung beim Kompilieren:

    invalid use of void expression

    für die in der ich den Thread aufrufe ( grp.create_thread(threadFun(a,b)); )
    Vielleicht kennt sich jemand aus und kann mir sagen was ich falsch mache. Mit dem Aufruf grp.create_thread(threadFun,a,b) hab ich es übrigens auch schon versucht.



  • guck mal - hier gibts cpp-tags:

    void threadFun(int a, int b)
    { 
      std::cout << a << ' ' << b;
    } 
    
    int main() 
    { 
      int a=1, b=2; 
    
      boost::thread_group grp; 
      grp.create_thread(threadFun(a,b)); 
      boost::thread::yield(); 
      grp.join_all(); 
    }
    

    da ich allerdings keinen plan von boost::thread hab, belass ichs mal dabei, die cpp-tags eingefügt zu haben ;P



  • grp.create_thread(boost::bind(threadFun, a, b));
    // oder
    grp.add_thread(new boost::thread(threadFun, a, b));
    

    Ich hab zwar keine Ahnung von boost.thread, aber ein kurzer Blick in die Doku hat mir das geliefert.



  • Danke, läuft.



  • Aus der Doku:

    Thread Constructor

    template<typename Callable>
    thread(Callable func);

    Preconditions:

    Callable must by copyable.
    Effects:

    func is copied into storage managed internally by the thread library, and that copy is invoked on a newly-created thread of execution. If this invocation results in an exception being propagated into the internals of the thread library that is not of type boost::thread_interrupted, then std::terminate() will be called.

    Man erstellt eine neue Klasse mit Member, ueberlaedt den operator()() und greift in diesem auf die Member zu.

    Moeglichkeiten gibt es viele ...


Anmelden zum Antworten