+ 1

Multithreading application

can someone share with me a simple application based on multithreading i want just to see how it's and how can benefit of it .

31st May 2021, 1:04 PM
Mohamed Chouai
Mohamed Chouai - avatar
3 odpowiedzi
+ 2
I copied the code below from: https://www.cplusplus.com/reference/thread/thread/ It creates 2 extra threads and waits for each to complete. You can benefit by getting better performance through use of more CPU cores or by freeing up more execution when some functions block for IO or other resources. I've used multiple threads in a c++ application for rendering fractals in CPU since rendering lots of frames of animation at high resolution and high quality can be time consuming. Running that in 4 threads was almost 4 times faster than 1 thread on a 6-core CPU and could be faster with more threads but I usually didn't go with 6 threads since it used more power to run CPU fans for days on end. // thread example #include <iostream> // std::cout #include <thread> // std::thread void foo() { // do stuff... std::cout << "foo() called." << std::endl; } void bar(int x) { // do stuff... std::cout << "bar() called." << std::endl; } int main() { std::thread first (foo); // spawn new thread that calls foo() std::thread second (bar,0); // spawn new thread that calls bar(0) std::cout << "main, foo and bar now execute concurrently...\n"; // synchronize threads: first.join(); // pauses until first finishes second.join(); // pauses until second finishes std::cout << "foo and bar completed.\n"; return 0; }
31st May 2021, 3:06 PM
Josh Greig
Josh Greig - avatar
0
Thank you
1st Jun 2021, 10:36 AM
Mohamed Chouai
Mohamed Chouai - avatar
- 2
532
2nd Jun 2021, 9:49 AM
Koko Koko
Koko Koko - avatar