How to create and join threads in C (pthreads).

How to create and join threads in C (pthreads).

Jacob Sorber

5 лет назад

243,921 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Saurav Biswas
Saurav Biswas - 14.06.2019 18:31

pthread_join(&newthread,NULL) gives me error so i used pthread_join(newthread,NULL) and it works. Can anyone explain why it works here but does not work in my case. Thankyou.

Ответить
Thiwanka Pathirana
Thiwanka Pathirana - 11.09.2023 15:20

Thank you. This made my understanding about threads so much clear.

Ответить
yuval158
yuval158 - 03.09.2023 10:36

So is this all happening in parallel? The processor doesn't switch from thread1 to thread2 because of the sleep?

Ответить
Daniel Wait
Daniel Wait - 17.08.2023 09:01

Hi Jacob,
I'm an embedded systems engineer and would like to do more work to understand threadding in Windows environement.
Any chance you could do a tutorial on MinGW / MSYS and to explain win32 vs posix threads and SEH vs SJLJ exception models, etc. that would be a guide for a newbie.
Kind regards, I love the videos

Ответить
me luobote
me luobote - 24.06.2023 14:09

I'm using your video to learning english😂😂, and programming skills

Ответить
amna taha
amna taha - 19.05.2023 01:47

Excellent work

Ответить
Muhammad Zaki Ahmad
Muhammad Zaki Ahmad - 10.04.2023 19:25

Great Video, Well Understood👍.

Ответить
sumit adep
sumit adep - 09.04.2023 08:26

Can we make a thread wait for other thread to complete it's execution by
pthread_join
Or it is specifically used in main function only

Ответить
Eirik Langseth
Eirik Langseth - 23.01.2023 16:14

kan itj å progge du jacob!

Ответить
Global Tech Mastery
Global Tech Mastery - 13.01.2023 15:29

Extremely powerful video

Ответить
yuval158
yuval158 - 09.01.2023 16:08

Is this relevant for c++ or is there a different more popular tool for it?

Ответить
Nilou
Nilou - 25.10.2022 14:33

Hey
I’m at last year of my computer science bachelor .
I just want to say Thank youuuu for your videos they help me a lot ❤️

Ответить
Daniele Padrini
Daniele Padrini - 13.10.2022 12:42

GOLD! ty so much

Ответить
Mohamad
Mohamad - 25.09.2022 16:28

Perfect, Perfect, Perfect, Perfect,Perfect, Perfect,Perfect,Perfect,Perfect,Perfect !!!

Ответить
amos
amos - 11.08.2022 14:24

Thanks, Jacob!

Ответить
Shantanu Jadhav
Shantanu Jadhav - 30.07.2022 13:56

Amazing video, simple and easy.

Ответить
anon anon
anon anon - 26.07.2022 07:40

I hope this will be a good stepping stone for me to transition to odersky's parallel programming course on Coursera. That course really went over my head

Ответить
edward mac nab
edward mac nab - 18.07.2022 21:44

If you put the pthread_create function after the your_turn function would that not allow the my_turn function inside pthread_create to terminate before main terminated ? you'd then have output like "your turn" , "my turn my turn" , etc. etc. or would you just get "your turn , your turn , your turn" followed by a string of 5 "my turn" ? btw , pthread looks like a really bad idea ! Actually you know what ? I'm going to wait till I start coding C and just try things out and isn't that always the best way anyway ? Thanks for your great videos .

Ответить
yj c
yj c - 26.05.2022 19:10

you are the best !

Ответить
Mürvət
Mürvət - 01.05.2022 08:31

what kinda font do you use? and vs code theme?

Ответить
Uerdue
Uerdue - 29.03.2022 00:49

Maybe a "cleaner" design would be to have the main thread creating 2 different threads - one running the "yourturn" function and one running "myturn" - , then joining both of them?
Somehow, the asymmetry in calling one function within the main thread and one in a seperate thread, when both are conceptually doing the same (just with different strings) feels "wrong" to me.
(Although I'm of course fully aware that this is just meant as a very basic example.)

Ответить
Paulo Bassani
Paulo Bassani - 28.03.2022 00:06

very helpful, thankyou!

Ответить
Smrt fašizmu
Smrt fašizmu - 05.03.2022 18:41

I accidentally crashed my computer once by running a program that I wrote which had an infitie loop which produced threads. This description of what I wrote isn't quite correct but you get the point of more or less what I did.

Ответить
Luiz Teixeira
Luiz Teixeira - 18.02.2022 04:12

Awesome class, thanks a lot!

Ответить
Aniruddh Arora
Aniruddh Arora - 17.02.2022 04:14

you have to go over the functions properly, what is pthread_t? what is pthread_create? what are all the parameters? that's what i came to learn and i didn't get that. you just went over one example and with bare minimum information about it. it's just critique, i hope you implement in future videos.

Ответить
Giacomo Ciaramella
Giacomo Ciaramella - 15.02.2022 01:25

Does this work on Windows?

Ответить
Daniel Skala
Daniel Skala - 04.02.2022 17:20

If this channel doesnt get 1M subs till summer, I will be sad. Great videos Jacob!

Ответить
DenebVegaAltair
DenebVegaAltair - 03.02.2022 04:35

Love how people like you make programming easier to learn for the rest of us. Thanks!

Ответить
Frank Gomez
Frank Gomez - 19.01.2022 09:43

JacobSorber is a great C language teacher. I’m learning lots of useful programming techniques. Thanks

Ответить
Khalid Saifullah Fuad
Khalid Saifullah Fuad - 04.01.2022 16:43

Thread is one the scary topic to me. Thank you for explaining it such an easy way 😊

Ответить
DwozK
DwozK - 31.12.2021 11:12

thread.c:15:22: warning: unused parameter 'arg' [-Wunused-parameter]
void* myTurn (void * arg) {

Paramater void *myTurn (void * arg) not use, Why?

Ответить
Pranav Rao
Pranav Rao - 26.11.2021 01:22

How do I stop a single thread from terminating within its threaded function until a certain condition is met or until the parent thread calls pthreads_join?

Ответить
Jeffris (Suspicious Manifold)
Jeffris (Suspicious Manifold) - 24.11.2021 23:37

Most people: "I hated math in high school, why did I have to memorize the quadratic formula? What the heck is an imaginary number?"
Computer science majors: "...I have seen things. Things I can't unsee. Things like void *(*start_routine)(void*) ."

Ответить
Pranav Rao
Pranav Rao - 23.11.2021 08:34

After creating a thread how long does it last? Until we call pthread_kill, pthread_cancel or pthread_join?

Ответить
ManuMonkey
ManuMonkey - 20.11.2021 06:38

how do I install 3rd party libraries for codeblocks? is there any tutorial?

Ответить
Brooks Burris
Brooks Burris - 06.11.2021 17:58

Hey jacob, I was trying to combine your threads and signals videos and I've run into a question.

Whenever I press <CTRL> + C, I noticed that the next iteration of the "my turn" loop starts without waiting the full 2 seconds. I hope that makes sense how I described it. In order to keep this from happening, I thought that making a separate thread with the signal handler would solve this, but it still looks like SIGINT triggers the "my turn" loop in main. I don't really have an idea why this would happen. It seems that the handler and main are two separate entities, so the signal should not have an effect on main. If you have a second I'd appreciate any explanation you have. Thanks!

Ответить
MV
MV - 20.10.2021 08:12

lol why u gotta make it badass now i wanna learn more about c now

Ответить
Luiz Fernando Rabelo
Luiz Fernando Rabelo - 18.10.2021 21:34

Great video! The second example is more intuitive.

Ответить
Pat Hayes
Pat Hayes - 19.09.2021 15:02

Excellent again

Ответить
Jim Ivie
Jim Ivie - 13.09.2021 17:55

tip: Knock speed down to .75, flys through the coding part

Ответить
Jim Ivie
Jim Ivie - 13.09.2021 17:54

i had to add the "-lpthread" to get mine to compile using gcc

Ответить
Isaac Callison
Isaac Callison - 22.07.2021 17:33

Add the -lpthread flag to your gcc command in Linux.

Ответить
Michael Espinoza
Michael Espinoza - 22.07.2021 07:41

Mom, I have fallen in love with a man...

Ответить
Shashikala Raju
Shashikala Raju - 07.07.2021 15:23

if apt had a face, that would be you. Thank you

Ответить
Youssef Gerges
Youssef Gerges - 02.07.2021 11:06

Amazing! Thanks bro. I spent 3 hours with my professor trying to explain that and he faild. You did it in 6 minutes! keep it up. Thanks

Ответить
jonaskyman
jonaskyman - 25.05.2021 19:59

I tried this, but in my case, using CYGWIN GCC, I had to flush out the stdout after printf, otherwise it did not print anything. If you can give some insight would be great.

Ответить