The problem here simple addition is being used +. Load on the CPU is decided based on machine instructions finally. Replace + with say sin(i) function. Then we can say the advantage of thread parallelism over single. I tested and it is working. Also when trying with sin function, reduce last to 190000000(or have floats) otherwise it might take lot of time.
Hi guys, Hope you enjoyed the video, more advance video is coming in few days on thread topic. And please let me know if i can provide something else on this topic. Your support is appreciated. LIKE & SUBSCRIBE for more videos like this.
Hi, I have two questions... can I run the same function with different threads (at the same time obv)? And how should I manage the return of my function? ( something like this "result= thread t0(example,1); " ? )
Thank you for the step-by-step instructions. I did not understand other videos, but you kept it simple and now my program runs in 1:17 and not 5:40. (4 threads) Thank you!. Subbed
We can use "auto duration = duration_cast(stopTime - startTime); " to get the seconds directly instead of getting in micro sec and then dividing it to get in seconds.
There are so many questions about the program not behaving correct on different systems, so i double checked everything and found that if we pass by std::ref which i did then it is some how behaving different in some system, so i have made some changes in program, now instead of std::ref i am sending the old school address and it is working fine. check it out: ideone.com/ypngpe let me know if link is not working i will create another one.
I ran the program given in the above link in Cygwin and it ran in time twice of what it took to run without the threads. More specifically the time for code with threads took 17 seconds and without threads, it took 9 seconds. Is there an issue with running threads in Cygwin?
Bear in mind, threading doesn't always mean your execution time will be faster. There are a lot of factors that can make threading even slower than serial.
1. All performance testing should be done in Release. It should be mentioned. (Otherwise 2-threaded version may take longer time to execute). 2. What is the point of duration_cast and then dividing it by 1000000 ? Why not to duration_cast ?
@@pavelkravchenko2810 I have faced same issue.. it was taking almost 2x time with 2 threads. So these are steps that resolved my issue : Editor -- XCode 1) Product -> Scheme -> Edit Scheme -> Change Build Configuration to "Release" 2) Now clean Build -> Run app you will get desired result shown in video.. :)
I would never thought that i would discover that with a bitwise operators i could check if a number is odd or even. This makes me wonder the world of possibilities that bitwise operators can give.
How are you running thread in sublime text My sublime is giving me this error: In function 'int main()': error: 'thread' was not declared in this scope thread t1(temp); ^~~~~~ error: expected ';' before 't2' thread t2(temp2); ^~ error: 't1' was not declared in this scope t1.join(); error: 't2' was not declared in this scope t2.join(); ^~ [Finished in 668ms]
With the below program the timing issue will be solved: The problem is passing reference in thread, If reference and pointers removed it will work. Below is the program #include #include #include #include using namespace std; using namespace std::chrono; typedef long long int ull; void findEven(ull start, ull end, ull EvenSum) { for (ull i = start; i
hey man, absolutely love your explanations video's. But if I can give you 1 tip, make your intro sound a little less loud. You have a very calm soft voice, so during the video I turn my volume up but when another video starts playing from your playlists I almost go deaf from your intro sound. But keep up the good work, loads of love from a jr. developer in holland
I am using VS Code with MinGw compiler and getting the error message "thread" was not declared. I ensured to copy the code correctly, but the program is not running, any suggestions?
I am surprised by seeing the result in my VS2015, without threads it took 9secs and with two threads it took 20Secs for the same program.It's strange..
@@srinu571 No idea about visual studio, i have verified in Mac and Linux. May be some day i will run using visual studio and see if there is something i can do to overcome this.
I wrote exactly same code visual studio its taking 21 seconds without thread concept by using 2 separate threads as you did its taking double 40 seconds.. its happening ulta...
Hey guys, I was having the same problem I see many of you talk about here: Single thread 9seconds, 2 threads = 14seconds. Checking the comments I saw someone talk about debug and release modes and how to change that in the IDE he uses, so I searched about it on google: Try compiling using g++ -O3 filename or g++ -Ofast filename Using the -Ofast I got 2.3seconds single thread, 1.17 seconds 2-threads. Did NOT see that coming haha. There are some explanations about this stuff here: gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html
It worked for me in Visual Studio 2019. (Code from Link in description) But the time take is different at every run. (I am running in Debug x86 configuration) Its 10s, 26s, 30s...
This should happen only when you have single core CPU. otherwise i do get comments like yours but can't really think of reasons. I tried myself in 3 different systems and it was giving less time while threading, so no clue, i am sorry for not been able to answer to this question.
Apparently it depends on the system - multithreading isn't always faster for everyone depending on what you're doing. stackoverflow.com/questions/46759930/multithreading-slower-than-single-threading
sir I wrote the same code, I gave start=0, end=2000000000 (=2B) but in my case multithreaded code took 37 seconds wherease normal code took 28 second....why?
This is a small program that just finds sum of even and odd nuns but when you are coding a large application that has billions of lines of code eg. Google(2 billion lines of code), your program should be as fast as possible and it should be able to do multiple tasks at same time.
Sir i have a question, lets say we have 10 threads and we want them to be able to pick up a job dynamically, (assuming we have 15 callable functions), how can we dynamically pass the name of functions and parameters? Do threads accept function names stored in variables?
why it is showing error like:- error: 'thread' is not a member of 'std' Can you please help on this. I am running my code on vs code and minGW compiler installed
can you explain what might be happening when I calculate the sums using the main thread print time(comes out to be 8 seconds), when I do it using the 2 threads in the same execution of the program without commenting out the main thread calculation without use of threading it takes me aroudn 11 to 15 seconds via multiple threads then weirdly . But if i comment out the calculation of main thread and then execute just the multiple thread calculation it does almost half the processing time for the calculation. So basically when I execute these two methods exclusively the multithreading results in efficiency else if I execute the 2 scenarios together in the same run the multithreading actually takes more time weirdly ?? This was all when done with debug configuration was debug though when i change config to release I get almost half the execution time with multithreading based approach even when running in the same run
My knowledge is from stackoverlfow. i was involved in stackoverflow and used to give answers to people and see the answers for my doubts and there were so many discussion, i learned alot from there.
@@CppNuts Thanks for reply..but is there any specific book or sites u visited earlier in ur initial learnubg phase..if u could throw some light definetly it wud help beginners like me and all
Duede trust me there is nomagic stick, i can tell u one thing only which might be of help. Decide a topic what ever it is and then fuck the internet for hunting more and more about it, thats it you are now master at that topic. Thats All!!
Your content is good but the ads in between breaks concentration and it becomes irritating. I am posting this comment after watching 2 ads in just first 5 mins. That is ridiculous.
Thanks for making this video. It was very helpful. However, when I ran the code, shown in the video as well as mentioned in the above link, it takes more time when use thread compared to when I don't. For example, for the code mentioned at the above link, it takes 7 seconds to execute the code with threading; while it takes 6 seconds when I'm not using threading!! (I'm running the code using VSCode in Ubuntu 16.04) Do you have any idea? Why it's happening? Thanks again :)
question, what happens when the second thread finishes first before the first thread? will the t1.join() wait for the t1 to finsh before it can t2.join() wait for the t2? or.... both of this join() will run parallel too?
C++ threading class may be cool, but nothing like using the native Linux pthread_create() like POSIX APIs to manage your threads, also note that not all thread related services are in the c++ class, for example, pthread_cond_wait(), signal and broadcast APIs which helps a producer thread to wake up waiting consumer threads. Perhaps in the case of writing highly portable c++ code, might be useful to use the thread class. any thoughts?
There are so many questions about the program not behaving correct on different systems, so i double checked everything and found that if we pass by std::ref which i did then it is some how behaving different in some system, so i have made some changes in program, now instead of std::ref i am sending the old school address and it is working fine. check it out: ideone.com/ypngpe let me know if link is not working i will create another one.
I crosschecked many times and pasted your code also but same result... With thread.. it takes 13 secs but Without thread it takes 10 secs.. how ?????
Did you took latest code i updated in video details.
I have provided link.
@@CppNuts Yes rupesh... latest one from link where you are passing reference of OddSum and EvenSum instead of making them global variable....
Then no idea why is it like this.
I have 3 system i check in every system but not getting this issue.
The problem here simple addition is being used +. Load on the CPU is decided based on machine instructions finally. Replace + with say sin(i) function. Then we can say the advantage of thread parallelism over single. I tested and it is working. Also when trying with sin function, reduce last to 190000000(or have floats) otherwise it might take lot of time.
creating a thread is quite an CPU intensive task. So for simple straight forward functions on an older CPU is not always faster when threaded
Hi guys,
Hope you enjoyed the video, more advance video is coming in few days on thread topic.
And please let me know if i can provide something else on this topic.
Your support is appreciated.
LIKE & SUBSCRIBE for more videos like this.
Your efforts are really appreciated and is helpful for many of us.
@@WithHumble Thanks man, it really motivates to create more videos.
Hi, I have two questions... can I run the same function with different threads (at the same time obv)?
And how should I manage the return of my function? ( something like this "result= thread t0(example,1); " ? )
That comes with future and promise topics in cpp, i will start soon.
@@CppNuts thanks, I need it so much... :) Anyway why didn't you used "using namespace std;" it is so useful...
Explained in simple way. Way better than other multi-threading tutorials. Kudos to you guys.
Thanks man!!
Thank you for the step-by-step instructions. I did not understand other videos, but you kept it simple and now my program runs in 1:17 and not 5:40. (4 threads) Thank you!. Subbed
Great.. Things like this encourages me like anything.. 😍🤓
We can use "auto duration = duration_cast(stopTime - startTime); " to get the seconds directly instead of getting in micro sec and then dividing it to get in seconds.
There are so many questions about the program not behaving correct on different systems, so i double checked everything and found that if we pass by std::ref which i did then it is some how behaving different in some system, so i have made some changes in program, now instead of std::ref i am sending the old school address and it is working fine.
check it out: ideone.com/ypngpe
let me know if link is not working i will create another one.
Hi Rupesh, with threads and without threads the execution time is same no difference,
What is the configuration of your computer?
@@CppNuts i5 processor with 8Gb of ram
We have to setup program one I have explained in video and one I have given in the link so, in both the cases is it same or it's different.
I ran the program given in the above link in Cygwin and it ran in time twice of what it took to run without the threads. More specifically the time for code with threads took 17 seconds and without threads, it took 9 seconds. Is there an issue with running threads in Cygwin?
thank you! you’ve explained this so well. feeling more confident about threads already
Amazing Rupesh... This i would call a social service....Keep it up... Clear cut explanation. God bless you...
Thanks man..
Bear in mind, threading doesn't always mean your execution time will be faster. There are a lot of factors that can make threading even slower than serial.
True..
@@CppNuts could you tell me some cases when threading is slower than others
I absolutely love how he drew his channel logo at the end, you're cool 😎❤️
Thanks..
when you were interrupted, it was a perfect example of threading
Yes that's correct !!
1. All performance testing should be done in Release. It should be mentioned. (Otherwise 2-threaded version may take longer time to execute).
2. What is the point of duration_cast and then dividing it by 1000000 ? Why not to duration_cast ?
Pls explain about the testing in Release, cuz I have this result with 2 X time 🤷♀️
@@pavelkravchenko2810 I have faced same issue.. it was taking almost 2x time with 2 threads. So these are steps that resolved my issue :
Editor -- XCode
1) Product -> Scheme -> Edit Scheme -> Change Build Configuration to "Release"
2) Now clean Build -> Run app
you will get desired result shown in video.. :)
I would never thought that i would discover that with a bitwise operators i could check if a number is odd or even. This makes me wonder the world of possibilities that bitwise operators can give.
hello, i wanted to thank you for keeping it simple! the example is great, as well as the explanation! keep it up! cheers!
Thanks dude..
Very useful. This subject is very interesting, i hope you'll talk more about it in your next videos. Keep up with good work.
Sure man, and thanks for the comment.
How are you running thread in sublime text
My sublime is giving me this error:
In function 'int main()':
error: 'thread' was not declared in this scope
thread t1(temp);
^~~~~~
error: expected ';' before 't2'
thread t2(temp2);
^~
error: 't1' was not declared in this scope
t1.join();
error: 't2' was not declared in this scope
t2.join();
^~
[Finished in 668ms]
I really like the way how you explain things. Thank you so much.
walking up by alexa is cool
Please make tutorial on C++ boost library. There aren't many tutorials online and also your channel focuses on C++
you said it, I love C++! And I love Hindi!
Excellent vedio which can be easily understood
Thanks
you explaination is unbaeatable
Very eloquently explained, great Vid. Subbed!
Salutes from a Chinese fellow engineer
Thanks dude..
Hi, guys. In QtCreator it only works in release mode. If you put debug mode you will see a bigger time.
I love c++ ♥️. Because,it power the world.
very good example, perfect explanation
Thanks for the comment man!
Superbb explanation sir. I will never miss yr videos 😍😍😍
Thanks for your support man!!
Great video. Greetings from Spain
Thanks
I just love all your videos and teaching sir... Grt😍👍
Glad to hear that
With the below program the timing issue will be solved:
The problem is passing reference in thread, If reference and pointers removed it will work.
Below is the program
#include
#include
#include
#include
using namespace std;
using namespace std::chrono;
typedef long long int ull;
void findEven(ull start, ull end, ull EvenSum) {
for (ull i = start; i
hey man, absolutely love your explanations video's. But if I can give you 1 tip, make your intro sound a little less loud. You have a very calm soft voice, so during the video I turn my volume up but when another video starts playing from your playlists I almost go deaf from your intro sound. But keep up the good work, loads of love from a jr. developer in holland
I was wonder if that is an issue. Thanks for the tip.
Very nice video, thancks, it helps beginner like me
Thanks for your comment man!!
I love when someone write that my videos are helping.
Well explained sir.
Your work is appreciated
Thanks man!!
I am using VS Code with MinGw compiler and getting the error message "thread" was not declared. I ensured to copy the code correctly, but the program is not running, any suggestions?
loved it, best explanation done
I am surprised by seeing the result in my VS2015, without threads it took 9secs and with two threads it took 20Secs for the same program.It's strange..
Please check the code link in description field.
@@CppNuts Even with this new code (Passing evensum and odd sum address and taking as pointer in functions) taking more time 28 sec
@@srinu571 No idea about visual studio, i have verified in Mac and Linux.
May be some day i will run using visual studio and see if there is something i can do to overcome this.
switch to release. never check performance in debug mode.
Hi just found your channel. thanks man lovin it.
Thanks man, i am out of station, next threading video will come around next week.
getting error like this, using c++17
error: 'thread' is not a member of 'std'
Very nice series. Please continue to make some great content. Can you make a series on ipcs n socket programming
What's your source of knowledge ? I mean what do you read , which book you follow ? Which resources you follow for learning C++
when i replaced my sync code to thread code, took more time XD lmao, why my luck like that in always
You're just awesome mate. Thanks a lot
I wrote exactly same code visual studio
its taking 21 seconds without thread concept
by using 2 separate threads as you did its taking double 40 seconds..
its happening ulta...
Me too
@@mariamalbarghouti4385 same
@@MemoirsOfADrainCleaner same
You're running in debug mode. Switch to release
@@Sandroidus How can I switch to release?
Thanks
wow, this is really nice demo & explanation, thanks
Thanks dude..
Alexa learning to program herself!
Correct..
very much nicely explained !!
Explanation was great that helps keep doing good work thanks
Thanks for the encouragement dude!!
Hi sir
Can you please explain me if two threads are trying to access same code in singalton class in C++ please I humble request to you
Hey guys, I was having the same problem I see many of you talk about here:
Single thread 9seconds, 2 threads = 14seconds.
Checking the comments I saw someone talk about debug and release modes and how to change that in the IDE he uses, so I searched about it on google:
Try compiling using
g++ -O3 filename
or
g++ -Ofast filename
Using the -Ofast I got 2.3seconds single thread, 1.17 seconds 2-threads. Did NOT see that coming haha.
There are some explanations about this stuff here:
gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html
OS?
@@CppNuts w10
Hey Rupesh, which CPP editor are you using?
It's VS Code.
Beautifully explained!
Glad you think so!
why is my system taking 15 seconds to execute with threads and 5 seconds without threads
It worked for me in Visual Studio 2019. (Code from Link in description)
But the time take is different at every run.
(I am running in Debug x86 configuration)
Its 10s, 26s, 30s...
Not working on raspberry pi 3 :( Error from g++ undefined reference to `pthread_create'
In visual studio 2022 debug mode, multithreading does not have any speed benefits for this piece of code. Are there any experts here?
Can you please create interview question series on threading?
i need implementation thread and fork can you help im looking someone implement i need understand how its work please help
Nice explaination, thank you sir
Welcome..
Super video sir very very thanks.
Welcome dude..
Thank you for this video
My pleasure
what specification were you using?
My multithreaded program took more time than sequential one. Why would that be happening?
This should happen only when you have single core CPU. otherwise i do get comments like yours but can't really think of reasons.
I tried myself in 3 different systems and it was giving less time while threading, so no clue, i am sorry for not been able to answer to this question.
Apparently it depends on the system - multithreading isn't always faster for everyone depending on what you're doing.
stackoverflow.com/questions/46759930/multithreading-slower-than-single-threading
sir I wrote the same code, I gave start=0, end=2000000000 (=2B)
but in my case multithreaded code took 37 seconds wherease normal code took 28 second....why?
Why there is need of creating 2 threads? We already have one thread main() which can do say findEven and we can create only one thread for findOdd
This is a small program that just finds sum of even and odd nuns but when you are coding a large application that has billions of lines of code eg. Google(2 billion lines of code), your program should be as fast as possible and it should be able to do multiple tasks at same time.
can you please tell me how to compile it? And what changes we need to make if we want to run it in .c format?
Can you make a video on gui in C++ ?
really need it thanks
I never worked on GUI in c++, so it will be little hard for me.
I have worked but with QT and that was very basic, won't help much here.
@@CppNuts make video of qthead
Font not clear with black background.
So sorry to hear that!!
Sir i have a question, lets say we have 10 threads and we want them to be able to pick up a job dynamically, (assuming we have 15 callable functions), how can we dynamically pass the name of functions and parameters? Do threads accept function names stored in variables?
why it is showing error like:- error: 'thread' is not a member of 'std' Can you please help on this. I am running my code on vs code and minGW compiler installed
thank you!
You're welcome!
please add a video of image processing using posix threads
thank you a lot, you're a life saver
Glad it helped!
can you explain what might be happening when I calculate the sums using the main thread print time(comes out to be 8 seconds), when I do it using the 2 threads in the same execution of the program without commenting out the main thread calculation without use of threading it takes me aroudn 11 to 15 seconds via multiple threads then weirdly .
But if i comment out the calculation of main thread and then execute just the multiple thread calculation it does almost half the processing time for the calculation.
So basically when I execute these two methods exclusively the multithreading results in efficiency else if I execute the 2 scenarios together in the same run the multithreading actually takes more time weirdly ??
This was all when done with debug configuration was debug though
when i change config to release I get almost half the execution time with multithreading based approach even when running in the same run
Text is not visible properly.it is blur
Bro please make more videos during this period ?
I have to do work from home, and i get more tired. But today one video is coming. And you are right i should try more during this period.
finally got it!! thank you :)
You are most welcome..
Hello sir could u plss share some resources from where u get these things..that will be helpful
My knowledge is from stackoverlfow. i was involved in stackoverflow and used to give answers to people and see the answers for my doubts and there were so many discussion, i learned alot from there.
@@CppNuts Thanks for reply..but is there any specific book or sites u visited earlier in ur initial learnubg phase..if u could throw some light definetly it wud help beginners like me and all
Duede trust me there is nomagic stick, i can tell u one thing only which might be of help.
Decide a topic what ever it is and then fuck the internet for hunting more and more about it, thats it you are now master at that topic. Thats All!!
There is only one website.
Google.com
😊😊
Ravi Correct!!
video starts at 8:30
Really.. 😂
I had to check and found that i was giving insight before.
my M1 Mac is taking 8 see without thread and 2 sec with thread why?
It is good right?
this video was helpful
Thanks
thanks, very clear !
Glad it helped!
Sir What happens if a Thread finishes before OS calls Join( ). Does Thread joins the main program even in that situation ?
I'm vietnammess, I not good english. But your vidio is very nice.
Thanks
ok.. but after we make threading, how many thread did the program made? is it parent thread main() and 2 child threads?
Your content is good but the ads in between breaks concentration and it becomes irritating. I am posting this comment after watching 2 ads in just first 5 mins. That is ridiculous.
Thanks for making this video. It was very helpful.
However, when I ran the code, shown in the video as well as mentioned in the above link, it takes more time when use thread compared to when I don't. For example, for the code mentioned at the above link, it takes 7 seconds to execute the code with threading; while it takes 6 seconds when I'm not using threading!! (I'm running the code using VSCode in Ubuntu 16.04)
Do you have any idea? Why it's happening?
Thanks again :)
same prob with me...
and its also giving this output OddSum 950000000
EvenSum 950000001
Do you have any video on thread that explains thread scheduling techniques
No, i may have in future, thanks for the topic.
Alexa wake up 😂
Haaeee this is Alexa.. what do you want...
can anyone help to debug this below error
error: 'thread' is not a member of 'std'
How r you compiling?
Command ?
Hi Sir, which IDE you used in video's? I am looking the same IDE ? is it available for Windows or Mac?
I have used Sublime text editor 3.
But this is editor you will have to link compiler with it.
And it available in mac n windows both.
question, what happens when the second thread finishes first before the first thread? will the t1.join() wait for the t1 to finsh before it can t2.join() wait for the t2? or.... both of this join() will run parallel too?
Yes it will wait.
Can you please tell me which IDE this is?
Sir what is idle trigger
C++ threading class may be cool, but nothing like using the native Linux pthread_create() like POSIX APIs to manage your threads, also note that not all thread related services are in the c++ class, for example, pthread_cond_wait(), signal and broadcast APIs which helps a producer thread to wake up waiting consumer threads. Perhaps in the case of writing highly portable c++ code, might be useful to use the thread class. any thoughts?
If portability is what you need the most then you can go with C++ otherwise POSIX is great choice.
which compiler are you using?
I am using MinGW and it's showing an error :(
on Windows 10
Error?
Can you paste it?
Did you got the error?
I am also getting error on using thread
sir how to get these code?
Where can I get all these sample?
Hi, I have an issue program without use of thread takes 8sec and the one with thread takes 13-17 sec. My laptop have an i5-9300H processor.
There are so many questions about the program not behaving correct on different systems, so i double checked everything and found that if we pass by std::ref which i did then it is some how behaving different in some system, so i have made some changes in program, now instead of std::ref i am sending the old school address and it is working fine.
check it out: ideone.com/ypngpe
let me know if link is not working i will create another one.
@@CppNuts This link is not working
You're running in debug mode. Switch to release
Can you Share the link (GitHub/any other URL) for the code used in the vedio. Is there any vedio for C++20?
I have started writing, will post ASAP it is done.
No i don't have c++20 video for now but will cover it in near future.