Thread wie geht das?



  • Hab nit getestet...

    Aber mal daran gedacht:

    * This program requires the multithreaded library.
    *
    * If you are using the Visual C++ development environment, select the
    * Multi-Threaded runtime library in the compiler Project Settings
    * dialog box.

    und daran:

    #include <process.h>    /* _beginthread, _endthread */
    

    Edit: Mist doppeltpost, einen löschen bitte 😮



  • Hab nit getestet...

    Aber mal daran gedacht:

    * This program requires the multithreaded library.
    *
    * If you are using the Visual C++ development environment, select the
    * Multi-Threaded runtime library in the compiler Project Settings
    * dialog box.

    und daran:

    #include <process.h>    /* _beginthread, _endthread */
    


  • Bei mir kommt die Fehlermeldung auch! Auch mit dem Include.



  • Zeig mal deinen Code.



  • Thr schrieb:

    Ich dachte das Forum wäre auch Newbie-freundlich, darum habe ich das
    hier gepostet. Entschuldigt, falls das hier nicht hingehört.
    Ich habe erst mit C++ angefangen (vor knapp 2 Wochen). 🙂

    Mit 2 Wochen C/C++-"Erfahrung" solltest du dich IMVHO nicht mit Threads rumschlagen...

    Thr schrieb:

    Muss man aber wohl mit Pointern innerhalb der Funktion arbeiten oder?

    Jein.

    Wenn du deinem Thread Daten übergeben willst kannst du das folgendermaßen bewerkstelligen:

    #include <stdio.h>
    #include <process.h>
    #include <windows.h>
    
    struct thread_data_t {
    
        int a;
        int b;
    };
    
    void __cdecl thread_proc( void *thread_data )
    {
        printf( "Die zwei Parameter, die ich bekommen habe, lauten %i und %i.\n",
                ( ( thread_data_t* )thread_data )->a,
                ( ( thread_data_t* )thread_data )->b );
    }
    
    int main( )
    {
        thread_data_t thread_data;
        thread_data.a = 10;
        thread_data.b = 20;
    
        _beginthread( thread_proc, 0, &thread_data );
    
        printf( "Ich wart' mal eine Sekunde...\n" );
        Sleep( 1000 );
    }
    

    Natürlich musst du dein Programm mit der Multithread-Version der CRT linken. Wie das geht verrät dir die Dokumentation deines Compilers.

    Greetz, Swordfish



  • das thread_data objekt sollte man aber in den meisten fällen auf dem heap anlegen.



  • Kann in einigen Fällen Sinn machen. Argumente?

    Greetz, Swordfish



  • In deinem Beispiel ist es sogar nötig oder du ändest es in Sleep(INFINITE);



  • wenn main() nach _beginthread() die thread_data nicht verändern will bzw. nicht von ihr abhängt ist's wurscht.

    Greetz, Swordfish



  • Es kann doch passieren das main() erst ganz durchläuft, auch wenn es bei einer Sekunde extrem unwahrscheinlich ist, und dann erst der erstellte Thread an die Reihe kommt. Dann ist der Inhalt der Variablen undefiniert.


Anmelden zum Antworten