array of threads erzeugen



  • Ich will für einen unsigned int anzahl_threads

    anzahl_threads Threads erstellen, wobei jedem Thread eine von seiner Nummer abhängige unsigned int übergeben werden soll. Wie mache ich das?



  • #include <iostream>
    #include <future>
    
    void machwas(unsigned int ui){
    
    }
    
    int main(){
    	unsigned int anzahl_threads;
    	if (std::cin >> anzahl_threads){
    		for (unsigned int thread_nr = 0; thread_nr < anzahl_threads; thread_nr++)
    			std::async(std::launch::async, machwas, thread_nr);
    	}
    }
    


  • nwp3 schrieb:

    std::async(std::launch::async, machwas, thread_nr);

    Nö, so geht das nicht. std::async kannst du in die Tonne rühren, das ist völlig nutzlos (siehe cppreference).



  • Was wäre dann ein geeigneter Ansatz um das Problem zu lösen?



  • anzahl_threads einlesen, entsprechend viele std::thread erstellen und sie mit entsprechendem wert starten ?



  • async async = not async schrieb:

    nwp3 schrieb:

    std::async(std::launch::async, machwas, thread_nr);

    Nö, so geht das nicht. std::async kannst du in die Tonne rühren, das ist völlig nutzlos (siehe cppreference).

    #include <iostream>
    #include <future>
    #include <vector>
    
    void machwas(unsigned int ui){
    
    }
    
    int main(){
    	unsigned int anzahl_threads;
    	if (std::cin >> anzahl_threads){
    		std::vector<decltype (std::async(std::launch::async, machwas, 0u))> thread_array;
    		thread_array.reserve(anzahl_threads);
    		for (unsigned int thread_nr = 0; thread_nr < anzahl_threads; thread_nr++)
    			thread_array.push_back(std::async(std::launch::async, machwas, thread_nr));
    		for (auto &t : thread_array)
    			t.wait(); //t.get() wenn machwas einen sinnvollen returnwert hat
    	}
    }
    


  • Ich persönlich finde Threading mit C++11 noch etwas undurchsichtig, weshalb ich über verschiedene Lösungsstrategien dankbar bin.

    Danke nochmal für die Antwort.



  • nwp3 schrieb:

    for (unsigned int thread_nr = 0; thread_nr < anzahl_threads; thread_nr++)
    	thread_array.push_back(std::async(std::launch::async, machwas, thread_nr));
    

    immer noch falsch. std::async(std::launch::async,...) kann blockieren.



  • Joa die meisten Threading-Implementierungen in C++ 11 sind doch broken.
    Lieber ne ausgereifte Bibliothek dafür benutzen z.B. QT.



  • async async = not async schrieb:

    immer noch falsch. std::async(std::launch::async,...) kann blockieren.

    Wo kann man das nachlesen? So wie ich die Doku lese darf es das nicht.



  • async async = not async schrieb:

    nwp3 schrieb:

    for (unsigned int thread_nr = 0; thread_nr < anzahl_threads; thread_nr++)
    	thread_array.push_back(std::async(std::launch::async, machwas, thread_nr));
    

    immer noch falsch. std::async(std::launch::async,...) kann blockieren.

    Wo steht das?
    ~future() kann blockieren. Das sollte hier doch aber nicht stören, wenn's ordentlich auf den Heap gemoved wird. Oder?
    Ich hätte wohl emplace_back() genommen.


Anmelden zum Antworten