You can use std::thread :
#include <iostream> #include <thread> void func1() //Π€ΡΠ½ΠΊΡΠΈΡ Π΄Π»Ρ Π·Π°ΠΏΡΡΠΊΠ° Π² ΠΏΠΎΡΠΎΠΊΠ΅ { std::cout << "Executing func1" << std::endl; } void func2(int x) //ΠΡΠ΅ ΠΎΠ΄Π½Π° ΡΡΠ½ΠΊΡΠΈΡ Ρ Π°ΡΠ³ΡΠΌΠ΅Π½ΡΠΎΠΌ { std::cout << "Executing func2 with x=" << x << std::endl; } int main() { //ΠΠ°ΠΏΡΡΠΊΠ°Π΅ΠΌ ΡΡΠ½ΠΊΡΠΈΠΈ Π² ΠΎΡΠ΄Π΅Π»ΡΠ½ΡΡ
ΠΏΠΎΡΠΎΠΊΠ°Ρ
std::thread thread1 (func1); std::thread thread2 (func2,1); //ΠΠ°ΡΠ°Π»Π»Π΅Π»ΡΠ½ΠΎ ΠΌΠΎΠΆΠ΅ΠΌ ΠΈΡΠΏΠΎΠ»Π½ΡΡΡ ΠΊΠΎΠ΄ Π² Π³Π»Π°Π²Π½ΠΎΠΌ ΠΏΡΠΎΡΠ΅ΡΡΠ΅ std::cout << "Execute some commands in main process" << std::endl; //ΠΠ΄Π΅ΠΌ Π·Π°Π²Π΅ΡΡΠ΅Π½ΠΈΡ ΠΊΠΎΠ΄Π° Π² ΠΏΠΎΡΠΎΠΊΠ°Ρ
thread1.join(); thread2.join(); std::cout << "Thread execution completed" << std::endl; return 0; }
In addition, you can use std::async to parallelize the process:
#include <cstdlib> #include <iostream> #include <vector> #include <algorithm> #include <numeric> #include <future> #include <chrono> //ΠΠ΅ΠΊΠΎΡΠΎΡΠ°Ρ Π·Π°Π΄Π°ΡΠ° ΠΊΠΎΡΠΎΡΡΡ ΠΌΡ Π±ΡΠ΄Π΅ΠΌ Π²ΡΠΏΠΎΠ»Π½ΡΡΡ Π°ΡΠΈΠ½Ρ
ΡΠΎΠ½Π½ΠΎ template <typename RandomIt> int parallel_sum(RandomIt beg, RandomIt end) { auto len = end - beg; //ΠΎΠΏΡΠ΅Π΄Π΅Π»ΡΠ΅ΠΌ Π΄Π»ΠΈΠ½Ρ Π²Π΅ΠΊΡΠΎΡΠ° if (len < 1000) return std::accumulate(beg, end, 0); //ΠΊΠΎΡΠΎΡΠΊΠΈΠΉ Π²Π΅ΠΊΡΠΎΡ ΡΡΠΌΠΌΠΈΡΡΠ΅ΠΌ ΡΡΠ°Π·Ρ //Π Π΅ΠΊΡΡΡΠΈΠ²Π½ΠΎ Π·Π°ΠΏΡΡΠΊΠ°Π΅ΠΌ parallel_sum Π΄Π»Ρ ΠΏΠΎΠ»ΡΡΠ΅Π½ΠΈΡ Π±ΠΎΠ»ΡΡΠ΅Π³ΠΎ ΡΠΈΡΠ»Π° ΠΏΠΎΡΠΎΠΊΠΎΠ² RandomIt mid = beg + len/2; auto handle = std::async(std::launch::async, parallel_sum<RandomIt>, mid, end); int sum = parallel_sum(beg, mid); return sum + handle.get(); } int main() { //ΠΠ»Ρ ΠΏΡΠΈΠΌΠ΅ΡΠ° Π±ΡΠ΄Π΅ΠΌ ΡΡΠΌΠΌΠΈΡΠΎΠ²Π°ΡΡ Π±ΠΎΠ»ΡΡΠΎΠΉ vector ΡΠ»ΡΡΠ°ΠΉΠ½ΡΡ
ΡΠΈΡΠ΅Π» std::vector<int> v(100000); for (auto& e: v){ e = std::rand(); } //ΠΠ»Ρ ΠΈΠ½ΡΠ΅ΡΠ΅ΡΠ° ΡΡΠ°Π²Π½ΠΈΠΌ Π²ΡΠ΅ΠΌΡ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΡΡΠΌΠΌΠΈΡΠΎΠ²Π°Π½ΠΈΡ Π²Π΅ΠΊΡΠΎΡΠ° Ρ ΠΏΠΎΠΌΠΎΡΡΡ async: auto t0 = std::chrono::high_resolution_clock::now(); std::cout << "The sum is " << parallel_sum(v.begin(), v.end()) << '\n'; auto t1 = std::chrono::high_resolution_clock::now(); std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(t1-t0).count() << "msec\n"; //ΠΈ ΡΡΠΌΠΌΠΈΡΠΎΠ²Π°Π½ΠΈΠ΅ Π² ΠΎΠ΄Π½ΠΎΠΌ Π³Π»Π°Π²Π½ΠΎΠΌ ΠΏΠΎΡΠΎΠΊΠ΅: t0 = std::chrono::high_resolution_clock::now(); std::cout << std::accumulate(v.begin(), v.end(), 0); t1 = std::chrono::high_resolution_clock::now(); std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(t1-t0).count() << "msec\n"; }
My result is the following:
The sum is 1746632618 9msec 17466326181msec
std::async is a high-level tool, it does not guarantee that the code will be executed in a separate thread.
Further, I give the simplest example of flow control, although this is a completely different story, I emphasize that this is the simplest example, there is still a lot to be solved, for example, sharing cout with all threads:
#include <iostream> #include <thread> #include <vector> using namespace std; void func(const int i) { // Π€ΡΠ½ΠΊΡΠΈΡ, ΠΈΡΠΏΠΎΠ»Π½ΡΠ΅ΠΌΠ°Ρ Π² ΠΏΠΎΡΠΎΠΊΠ°Ρ
cout << "Thread N " << i << " created" << endl; } int main() { vector<thread> thread_pool; //ΡΠΎΠ·Π΄Π°Π΅ΠΌ ΠΏΡΠ» ΠΏΠΎΡΠΎΠΊΠΎΠ² Π½Π° ΠΎΡΠ½ΠΎΠ²Π΅ Π²Π΅ΠΊΡΠΎΡΠ° int threadNum = 10; //ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΠΏΠΎΡΠΎΠΊΠΎΠ² for (int i=0; i < threadNum; i++) { thread_pool.push_back(thread(func,i)); //Π·Π°ΠΏΡΡΠΊΠ°Π΅ΠΌ ΠΏΠΎΡΠΎΠΊΠΈ } for (auto& t : thread_pool) { t.join(); //ΠΎΠΆΠΈΠ΄Π°Π΅ΠΌ ΠΎΠΊΠΎΠ½ΡΠ°Π½ΠΈΠ΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΏΠΎΡΠΎΠΊΠΎΠ² } }
pidall the processes that you want to wait for completion in the array, then at the waiting point you are looking for thepidreturned bywait()in this array and if you find "strike out" it. As soon as the array became empty you waited for all the desired ones. - avp