Introduction To Threads (pthreads) | C Programming Tutorial

  Рет қаралды 119,590

Portfolio Courses

Portfolio Courses

Күн бұрын

Пікірлер: 130
@Shadowll101
@Shadowll101 2 жыл бұрын
I just learned about threads about 4 days ago. You can’t believe how much better a 13+ min video is than my 1 hour lecture in explaining threads.
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
That's awesome feedback thank you, I'm glad you enjoyed the video Leo! :-)
@waynec369
@waynec369 Жыл бұрын
Yes... yes I can...
@happygofishing
@happygofishing Жыл бұрын
university lectures are always overenginneered.
@wizardatmath
@wizardatmath 11 ай бұрын
I believe it.
@bill-cipher000
@bill-cipher000 8 ай бұрын
​@@happygofishingunderengineered too as one might say.
@mansbjork5721
@mansbjork5721 Жыл бұрын
We're studying this at university right now and I have to say this is very much useful. It's almost clear after the first watch only.
@dianafarhat9479
@dianafarhat9479 2 жыл бұрын
There are very few tutorials on this topic. Thank you for your work!
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
You're welcome Diana! 🙂
@ButteryCowEgg
@ButteryCowEgg 2 жыл бұрын
Just wanted to say that I found your channel today and you have done some amazing work on your channel
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Thank you very much Jack! I'm glad you enjoy the content. :-D
@AgarOther
@AgarOther 2 ай бұрын
42 Student here, you helped me greatly to understand the concept of threads!
@theyayaa
@theyayaa Жыл бұрын
I am listing this channel as a reference in my school project because I have learned a ton of c from here. Thank you!
@safarmaihoon4585
@safarmaihoon4585 Жыл бұрын
You know what!! This is better than hours of lectures. So crisp and to the damn point! 👏👏👏👏 Now I can start reading other references. Thanks for the video.
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're welcome Safar, I'm glad you enjoyed the video! :-)
@hengyili4662
@hengyili4662 9 ай бұрын
You are better than my professor in terms of explaining things. Thank you !!!
@JacemHaggui
@JacemHaggui 11 ай бұрын
You Sir are an angel, I've been stuck trying to understand how to use threads in C for SOOOO LONG and this tutorial makes so much sense, i was actually able to code along and it works and i get it ! AAAAH I'm so relieved, it finally makes sense, THANK YOU SO MUCH !
@genetechagency
@genetechagency 2 жыл бұрын
They way you explains with examples...Marvelous sir
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Thanks for the kind words Junaid! I'm glad you enjoyed the video. 🙂
@alvarobarboza5730
@alvarobarboza5730 Жыл бұрын
something that I particularly liked is .. the comparison you do at the end, this shows how useful multi-threading is
@p_molnar
@p_molnar 2 жыл бұрын
best ASCII presentation I've seen so far.
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Thank you Peter! :-)
@AibekDandaev
@AibekDandaev 2 жыл бұрын
wanted to say that you've done some amazing work on your channel
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Thank you very much! 😀
@atchau6466
@atchau6466 7 ай бұрын
13mins video is more valuable than 2 hours lecture on my university :D
@emmadhamid6578
@emmadhamid6578 2 жыл бұрын
Defined greatly n precisely ✨ best single shortest video to learn whole fkn threads topic …
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Thank you very much for the kind positive feedback Emmad! I'm glad you enjoyed the video. :-)
@M3t4lik
@M3t4lik 2 жыл бұрын
I've been playing around with OpenMP in VS and it works great for optimizing loops for parallel execution, where the threads seem to execute in random order each time the program is run. However, after looking at the documentation a little more attentively, there are a number of command parameters associated with the pragmas that would make life easier rather than trying to play around delay loops, ordering and placements. Great intro to threading.
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Cool, thanks for sharing that, I'm glad you enjoyed it! :-) I think I'm due to cover threading some more but there are so many topics I want to cover haha...
@adityak7877
@adityak7877 2 жыл бұрын
I think i have clear understanding about pthreads post watching this video....thanks for the free tuts.
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
You're welcome Aditya, I'm glad the video was helpful for you! :-)
@heitorheitorheitor8158
@heitorheitorheitor8158 2 жыл бұрын
you teach so well man, great job! :p
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Thank you! :-)
@tejasmarvel2023
@tejasmarvel2023 Ай бұрын
best explanation. needed for my exam.
@rishiaman2
@rishiaman2 Ай бұрын
cs288?
@bharadwajm7950
@bharadwajm7950 2 жыл бұрын
Why are you passing address as argument while calling function in pthread_create ? Why can't you directly pass the value ? Why (void*) & value1 ? Why not (void*) value1 ? Near 7:17
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Good question Bharadwaj! Ultimately, we do that because it's the way that the pthread library was created. :-) One thing to keep in mind is that we don't call the function in pthread_create. What we're doing is calling pthread_create, and we give it as arguments the function to call, and a pointer to the value we want to use as an argument. pthread_create() will at some point later "call our function in a new thread", and at that time the function is supplied with that pointer value as an argument. Because the pointer is a void pointer, we can use it to essentially "pass anything we want" to the function.... e.g. a pointer to something like a struct or array that contains all the data needed by the function. Hopefully this helps! :-)
@djoykeabyah5904
@djoykeabyah5904 Жыл бұрын
thank you for the explanation I was only wondering why my single thread execution is faster in the example?
@pietrocallandrone6012
@pietrocallandrone6012 Жыл бұрын
This video is a gem, thanks man!
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You’re welcome Pietro, I’m glad you enjoyed it! :-)
@berktopbas1496
@berktopbas1496 2 жыл бұрын
Hello, great tutorial! I just wanted to ask that as you explained difference between the concepts of concurrent and parallel execution at the beginning in the video, i suppose, to that explanation, at 12:12, you should have said that "running parallel" instead of "running concurrently". In my opninon, if they were to run concurrently, it could even take more than 4.5-5sec(run-time for single thread) because of context switch. Of course, i assume that your machine has multiple CPUs and multithreading-supporting software, as most modern PCs have today.
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
No, the description is accurate as it is, I said that they were running concurrently and added it looks like they were running in parallel too (i.e. due to the execution time): "We can see that the multithreaded program is running roughly twice as fast as the single threaded program, and that makes sense because we have two threads now running concurrently, and in this case it looks like they're running in parallel as well". All threads run concurrently, and in this case, on my machine, they are also running in parallel based on the execution time. Parallelism implies concurrency, it's a "subset" of concurrency, but concurrency does not imply parallelism. Maybe I'll make a video on concurrency vs parallelism though, it's an interesting topic. Concurrency won't help in a situation like this with a computation occurring in each threads, it could even be worse due to the context switch that you mention. But it can improve performance drastically if the threads are working on I/O bound operations for example, which is something I think a lot of people don't realize.
@friendsclub4887
@friendsclub4887 Жыл бұрын
Mm 🎂🎂 mm mmm 🎂🎂🎂🎂 mm🎂mm9
@friendsclub4887
@friendsclub4887 Жыл бұрын
🔥🔥🔥m9 🔥🔥🔥 mm 🔥m 🔥mm 🔥 okk 🔥🔥🔥🔥
@friendsclub4887
@friendsclub4887 Жыл бұрын
99 😍 mom 😍 mm 😍 mm 😍mm 😍😍m 😍😍 mm 😍😍 mm. 9
@friendsclub4887
@friendsclub4887 Жыл бұрын
🎉🎉 mo 🎉 mmmm 🎉m9 🎉🎉🎉 mmmm 🎉mm 🎉
@RahulYadav-df9sp
@RahulYadav-df9sp 4 ай бұрын
Best video ever !! on threads topic
@alvarobarboza5730
@alvarobarboza5730 Жыл бұрын
Hi. Just watched this video. Very very cool and interesting. I started learning C due to my interest in numerical methods. In the video you mention that certain hardware and software specs must be met, in order to run threads in C? can you elaborate on this please? it seems a regular PC won't be able to run multi-threads thank you !!
@abanoub7002
@abanoub7002 Жыл бұрын
A regular PC would most likely be able to run multiple threads, as the 'pthread_t' type doesn't reference the actual CPU thread directly, but instead references OS threads, which are created on demand by the OS itself depending on things such as available execution resources such as RAM and CPU among other things. Most modern OS's can make up to tens of thousands of threads (depending on the hardware itself of course, on average hardware you would be having around 12), so not meeting hardware requirements shouldn't be a problem. But yeah, as long as the amount of OS threads created is less than or equal to the amount of physical threads you have, you should be able to run anything you want in parallel.
@juliannafotheringham7101
@juliannafotheringham7101 Жыл бұрын
great video, so clear!
@PortfolioCourses
@PortfolioCourses Жыл бұрын
I'm glad you enjoyed it Julianna! :-)
@pral4206
@pral4206 11 ай бұрын
Did you make a video about semaphores also? My teacher is covering that right now and it might be helpful if there was a video about that also.
@tecoberg
@tecoberg 10 ай бұрын
It is not clear to me why when pthread_join commands were together the program didn't stop in the first one until the computarion is finished and after that it went to the second pthread_join like shown if you put them immediately after the pthread_creates
@depsylon4235
@depsylon4235 2 жыл бұрын
great and easy to follow explanation
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Awesome, I'm glad you enjoyed it! :-)
@MSDelhi2
@MSDelhi2 Жыл бұрын
nice explanation with easy example🙂
@PortfolioCourses
@PortfolioCourses Жыл бұрын
I’m glad you enjoyed it Manish! :-)
@therealajmelmuadz
@therealajmelmuadz 9 ай бұрын
Amazing video mate
@PortfolioCourses
@PortfolioCourses 9 ай бұрын
I’m glad you liked it! :-)
@lowground9345
@lowground9345 2 жыл бұрын
man i love you thanks so much for this vid helped A LOT
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
You're very welcome, I'm so glad to hear the video helped you out! :-)
@raffayylsamuill9164
@raffayylsamuill9164 10 ай бұрын
Really thank you for this great explanation
@ShopperPlug
@ShopperPlug 2 жыл бұрын
12:15 - I wonder how is it possible for the program to run faster when it is executing code concurrently with only one core, the physics behind it doesn't make sense... Does code do indeed run faster simply due to concurrency with one core?
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
The code in the video is running in parallel in addition to running concurrently, that’s why it’s faster. :-)
@OdinRu1es
@OdinRu1es 11 ай бұрын
Multithreading it’s not about executing multiple instructions at the same time. It’s about sharing resource beetween them.
@bofa-zi4fj
@bofa-zi4fj Жыл бұрын
What are the main differences between threads and the fork() system call? And why would you choose one over the other?
@PortfolioCourses
@PortfolioCourses Жыл бұрын
This is a really good question. :-) It's a big topic though, I definitely want to do a video on exactly this topic one day. For now though I can just point to some resources online such as these ones to explain things: stackoverflow.com/a/8514943 stackoverflow.com/questions/11662781/when-is-clone-and-fork-better-than-pthreads
@zoquevil4792
@zoquevil4792 Жыл бұрын
Hi ! I just started this subject when I was reading about the "Dining philosophers problem" and how to avoid the deadlock , starvation in this case .... you'll learn about mutex, semaphore/signal , the synchronization process, a huge among of information to learn...the call system fonctions are very tricky when you think about them and at the same time it's a very exiting subject to be familiar with because it's full of suspense!!! .. Thanks Kevin and as always your videos are very amazing!!! @@PortfolioCourses​
@lucasf.v.n.4197
@lucasf.v.n.4197 4 ай бұрын
very well explained sir;
@lawniczakjohn
@lawniczakjohn Жыл бұрын
Another awesome video! I'm getting into threads now. Is multi-threading much different in C++? I'm sure they are very similar...
@PortfolioCourses
@PortfolioCourses Жыл бұрын
I'm glad you enjoyed it John! :-) So prior to C++11 we just used the pthreads library in C++, but now there is the thread library we can use instead: www.geeksforgeeks.org/multithreading-in-cpp/.
@lawniczakjohn
@lawniczakjohn Жыл бұрын
@@PortfolioCourses That's great info thanks!
@PortfolioCourses
@PortfolioCourses Жыл бұрын
@@lawniczakjohn you're welcome! 🙂
@tracetv8115
@tracetv8115 Жыл бұрын
I am a little bit confused now. Ur example runs concurrently and not parallel right? If yes, how I have to code that it runs parallel then or the os decides what to do and I can not control that? Great video anyways!
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Yes it runs concurrently but not in parallel. To get parallel execution you would use another solution, for example: curc.readthedocs.io/en/latest/programming/OpenMP-C.html. :-)
@st1ers1
@st1ers1 2 жыл бұрын
Very well explained!
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
I'm glad you enjoyed it! :-)
@audunoklevik4435
@audunoklevik4435 2 жыл бұрын
Good introduction! Thanks :)
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
You're welcome Audun! :-)
@paulalucena2170
@paulalucena2170 Жыл бұрын
great explanation! Thanks
@subtainmushtaq3237
@subtainmushtaq3237 2 жыл бұрын
What if we have to pass multiple parameters in a function?
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
That's a great question Subtain, I think I'm going to make a video on exactly this question. :-) We can use a struct to pass multiple parameters by using multiple members of the struct, like this example: www.cse.cuhk.edu.hk/~ericlo/teaching/os/lab/9-PThread/Pass.html.
@subtainmushtaq3237
@subtainmushtaq3237 2 жыл бұрын
@@PortfolioCourses thank you sir !
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
@@subtainmushtaq3237 You're welcome! :-D
@moadelhaddad4376
@moadelhaddad4376 2 жыл бұрын
Hello , Thanks for the video. It is possible to pass 2 arguments to a thread?
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Great question Moad! :-) We can if we use a struct as a way to pass both arguments: www.cse.cuhk.edu.hk/~ericlo/teaching/os/lab/9-PThread/Pass.html.
@moadelhaddad4376
@moadelhaddad4376 2 жыл бұрын
@@PortfolioCourses thanks for answering :)
@aaronderby2581
@aaronderby2581 Жыл бұрын
Is it necessary to always "join the threads" or is it just for when you need to be sure a thread has finished before moving on? For instance, if i want s thread to continuously run until the program dies, would I ever really need to join it?
@fridaaa0
@fridaaa0 Жыл бұрын
Thank you, super digestible!
@HuntingKingYT
@HuntingKingYT Жыл бұрын
You can also get the return value, you just have to pass a pointer to the place where the return value of the function will be put at (the result is also statically a void pointer!) as the second parameter to pthread_join(): void* add(void* param) { long* result = (long*)malloc(sizeof(long)); *result = *(long*)param + 1; return result; } int main() { pthread_t thread1; long value = 10; pthread_create(&thread1, NULL, &add, &value); long* result; pthread_join(thread1, (void**)&result); // output pointer to a `long` pointer! printf("result: %ld ", *result); // 11 free(result); return 0; }
@gabriele4567-t4r
@gabriele4567-t4r 8 ай бұрын
great example, very usefull ty
@spurthidantu6890
@spurthidantu6890 2 жыл бұрын
How do i prevent memory leaks in Pthreads? The first time i execute the program, answer is right but if i add a while loop, it is providing incorrect values.Even after adding pthread_join, same thing is happening.
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
If we dynamically allocate memory using a pthread, either as part of creating the thread or in the function called by the thread, we need to use free() to free the dynamically allocated memory the same as usual. 🙂 In this example, we don't use any dynamically allocated memory. A while loop could produce incorrect results for a number of reasons, such as variables being re-used, etc.
@pareus7251
@pareus7251 Жыл бұрын
Sir I respect you so much
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Thank you for leaving this kind feedback! :-)
@anthony6841
@anthony6841 Жыл бұрын
Very helpful, thank you
@mastermax7777
@mastermax7777 Жыл бұрын
why dont you show how to get a return value from the thread function?
@PortfolioCourses
@PortfolioCourses Жыл бұрын
I think I may make more videos covering more aspects of pthreads as these videos have become popular. :-)
@baruchben-david4196
@baruchben-david4196 Жыл бұрын
For some of us, we also have to add a flag to the linker: -lpthread.
@向宇-d4b
@向宇-d4b 2 жыл бұрын
in fact , its gonna take more time compared to the single threaded version , create pthread is big overhead for this single program , user time and sys time has actually increased
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
The way user and sys time works in multithreaded programs isn't straightforward: stackoverflow.com/a/556411. In particular: "On a multi-processor machine, a multi-threaded process or a process forking children could have an elapsed time smaller than the total CPU time - as different threads or processes may run in parallel. ". So yes user time can go up, but the elapsed time can go down.
@arunasishsom9190
@arunasishsom9190 2 ай бұрын
Wow . Thanks ❤
@ryannn-u8j
@ryannn-u8j Жыл бұрын
Thank you man.
@undeadpresident
@undeadpresident Жыл бұрын
Anyone know how to use a C file that uses pthreads to work with Python's ctypes library? Anything that has the line "pthread_create" Python says it can't find it. Something to do with dependency...I don't get it.
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Unfortunately I don't have a video on this topic. Hopefully someone else is able to help you out. Eventually I'd love to cover ctypes in Python. Sometimes putting the error message into Google along with the text "stackoverflow" or "reddit" can lead to helpful conversations online that help to resolve the issue.
@undeadpresident
@undeadpresident Жыл бұрын
@@PortfolioCourses that was a good idea to go to stack overflow, they pointed me in the right direction and I got it solved. I had to specify a path in Python to the dependencies in mingw. First time I had to do that, I had thought I was doing something wrong with gcc. I've used ctypes a fair bit as a way to speed up my python code and learn C at the same time. I've just passed arrays to it but haven't got into any of the more hairy stuff like making python objects into ctypes structs or w/e though. On stack overflow I came across a library I hadn't heard of called omp which does multiprocessing in C also. I've only been programming less than a year so I'd be interested in hearing from more experienced people the pros/cons between pthreads and omp.
@PortfolioCourses
@PortfolioCourses Жыл бұрын
@@undeadpresident I'm glad you got that figured out. 🙂 Maybe someone else viewing the video can address your other question.
@undeadpresident
@undeadpresident Жыл бұрын
@@PortfolioCourses I was playing with omp a little bit and it looks like instead of passing arguments in, you use the thread ID to differentiate what the threads do, and that in pthreads the ID just continues to count up and up each time you loop through the code that splits the program, but in omp it restarts the count each time. Not trying to talk you to death, just thought it might be interesting if you haven't messed with it.
@PortfolioCourses
@PortfolioCourses Жыл бұрын
@@undeadpresident Cool, thanks for sharing, I want to play around with more myself next month sometime.
@nabanitabanerjee6570
@nabanitabanerjee6570 2 жыл бұрын
Very useful.
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
I’m really glad you found it useful Nabanita! :-)
@chaitanyapawar5483
@chaitanyapawar5483 Жыл бұрын
Thank you sir
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You’re welcome Chaitanya! :-)
@thomaslindell5448
@thomaslindell5448 11 ай бұрын
Hahn great material but I’m blind. How bout dark mode
@antoinebguitar2869
@antoinebguitar2869 3 ай бұрын
Here we again.. no satisfied with Async/await keywords, i need to know how the assembly make sense of a thread... the curiosity is very time consuming lmao
@liathecat7757
@liathecat7757 5 ай бұрын
Bro pls use dark theme
@jayantvarma7052
@jayantvarma7052 2 жыл бұрын
there are so many ads in a 13 min video :(
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Thank you for the feedback!
@FatimazahraeBazaz
@FatimazahraeBazaz 8 ай бұрын
please use dark mode !!! my eyes screaming haha
@EipsteinNorton
@EipsteinNorton 4 ай бұрын
36236 Bashirian Summit
@Awwe12675
@Awwe12675 2 жыл бұрын
I hate it 😂 I need focus in library to understand how it implemented
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Oh you need to know how to implement pthreads, from scratch? I don't know how to do that sorry, that's getting into some lower level programming that I don't really know enough about to talk about off the top of my head. :-)
@Awwe12675
@Awwe12675 2 жыл бұрын
@@PortfolioCourses yeah I believe it is system call so I downloaded the gun c library reference manual to figure out
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Good luck! :-)
@JeromeJefferson-t6q
@JeromeJefferson-t6q 3 ай бұрын
Hickle Mount
@NarongTantayanon
@NarongTantayanon 8 ай бұрын
why my result turn to be single thread is faster than multi thread? running in WSL ubuntu ❯ gcc -o s main.c ❯ time ./s Add: 1 Add: 2 Add: 3 Add: 4 Add: 5 Add: 6 ./s 55.02s user 0.01s system 99% cpu 55.032 total ❯ gcc -o m main.c ❯ time ./m Add: 5 Add: 4 Add: 3 Add: 6 Add: 2 Add: 1 ./m 68.42s user 0.01s system 596% cpu 11.464 total
@fathimamiznathajuddeen9175
@fathimamiznathajuddeen9175 Жыл бұрын
Thank you sir
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're welcome! :-)
Function Pointers | C Programming Tutorial
18:31
Portfolio Courses
Рет қаралды 72 М.
Mutex Introduction (pthreads) | C Programming Tutorial
10:19
Portfolio Courses
Рет қаралды 25 М.
Сестра обхитрила!
00:17
Victoria Portfolio
Рет қаралды 958 М.
REAL or FAKE? #beatbox #tiktok
01:03
BeatboxJCOP
Рет қаралды 18 МЛН
Introduction to Threads
14:06
Neso Academy
Рет қаралды 777 М.
Producer - Consumer Problem in Multi-Threading
25:18
CodeVault
Рет қаралды 121 М.
Arenas, strings and Scuffed Templates in C
12:28
VoxelRifts
Рет қаралды 104 М.
Multithreading Code - Computerphile
15:54
Computerphile
Рет қаралды 392 М.
How to create threads in a loop (pthread_create)
8:50
CodeVault
Рет қаралды 92 М.
Writing Code That Runs FAST on a GPU
15:32
Low Level
Рет қаралды 579 М.
When Optimisations Work, But for the Wrong Reasons
22:19
SimonDev
Рет қаралды 1,2 МЛН
Let's Create a Compiler (Pt.1)
1:11:03
Pixeled
Рет қаралды 604 М.
Introduction to semaphores in C
12:24
CodeVault
Рет қаралды 135 М.