While loop within thread is causing program to block (C)












-2















The point of threads is to be able to run multiple blocks of code at once. The first thing I think of when this possibility comes into my head is having two infinite loops running at once. Before I tried to get this working with two threads, I tried to get it to work with one, with no success.



So I have a thread like this:



void *myThread(void *args) {
sleep(0.1);
while (1) {

}
return NULL;
}


And I am creating the thread like so:



pthread_t thread_id;
printf("Before Threadn");
pthread_create(&thread_id, NULL, myThread, args);
pthread_join(thread_id, NULL);
printf("After Threadn");


When I run this, "After Thread" does not print! Why is this happening? This makes no sense to me.










share|improve this question























  • you might want to look up asynchronous functions

    – Brian
    Nov 21 '18 at 21:27











  • @Brian no. i dont want an async func here

    – Matt X
    Nov 21 '18 at 21:28
















-2















The point of threads is to be able to run multiple blocks of code at once. The first thing I think of when this possibility comes into my head is having two infinite loops running at once. Before I tried to get this working with two threads, I tried to get it to work with one, with no success.



So I have a thread like this:



void *myThread(void *args) {
sleep(0.1);
while (1) {

}
return NULL;
}


And I am creating the thread like so:



pthread_t thread_id;
printf("Before Threadn");
pthread_create(&thread_id, NULL, myThread, args);
pthread_join(thread_id, NULL);
printf("After Threadn");


When I run this, "After Thread" does not print! Why is this happening? This makes no sense to me.










share|improve this question























  • you might want to look up asynchronous functions

    – Brian
    Nov 21 '18 at 21:27











  • @Brian no. i dont want an async func here

    – Matt X
    Nov 21 '18 at 21:28














-2












-2








-2








The point of threads is to be able to run multiple blocks of code at once. The first thing I think of when this possibility comes into my head is having two infinite loops running at once. Before I tried to get this working with two threads, I tried to get it to work with one, with no success.



So I have a thread like this:



void *myThread(void *args) {
sleep(0.1);
while (1) {

}
return NULL;
}


And I am creating the thread like so:



pthread_t thread_id;
printf("Before Threadn");
pthread_create(&thread_id, NULL, myThread, args);
pthread_join(thread_id, NULL);
printf("After Threadn");


When I run this, "After Thread" does not print! Why is this happening? This makes no sense to me.










share|improve this question














The point of threads is to be able to run multiple blocks of code at once. The first thing I think of when this possibility comes into my head is having two infinite loops running at once. Before I tried to get this working with two threads, I tried to get it to work with one, with no success.



So I have a thread like this:



void *myThread(void *args) {
sleep(0.1);
while (1) {

}
return NULL;
}


And I am creating the thread like so:



pthread_t thread_id;
printf("Before Threadn");
pthread_create(&thread_id, NULL, myThread, args);
pthread_join(thread_id, NULL);
printf("After Threadn");


When I run this, "After Thread" does not print! Why is this happening? This makes no sense to me.







c multithreading






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 21:25









Matt XMatt X

1098




1098













  • you might want to look up asynchronous functions

    – Brian
    Nov 21 '18 at 21:27











  • @Brian no. i dont want an async func here

    – Matt X
    Nov 21 '18 at 21:28



















  • you might want to look up asynchronous functions

    – Brian
    Nov 21 '18 at 21:27











  • @Brian no. i dont want an async func here

    – Matt X
    Nov 21 '18 at 21:28

















you might want to look up asynchronous functions

– Brian
Nov 21 '18 at 21:27





you might want to look up asynchronous functions

– Brian
Nov 21 '18 at 21:27













@Brian no. i dont want an async func here

– Matt X
Nov 21 '18 at 21:28





@Brian no. i dont want an async func here

– Matt X
Nov 21 '18 at 21:28












1 Answer
1






active

oldest

votes


















2














Because pthread_join(thread_id, NULL); waits for the thread to finish. And a thread running a while (1) {} loop will never finish.






share|improve this answer
























  • do I need the pthread_join(thread_id, NULL); line then? @MartinHeralecký

    – Matt X
    Nov 21 '18 at 21:29













  • @MattX If you don't put it there, the After Thread message will print immediately after starting the thread, which will keep running.

    – Martin Heralecký
    Nov 21 '18 at 21:31













  • If you changed the code in myThread to loop 10 times and print something out or sleep and then return, you'd wait for it to finish with the call to pthread_join in main. And after that your program could exit. If you just leave myThread as it is and take out the call to pthread_join, your program won't wait for the thread to finish and exit right away.

    – bruceg
    Nov 21 '18 at 21:36











  • @MattX Whether or not you need it depends on exactly what you're trying to do, which we do not know. What you should do depends on exactly what this thread is doing and how it relates to what the program as a whole is doing.

    – David Schwartz
    Nov 21 '18 at 21:39













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53420666%2fwhile-loop-within-thread-is-causing-program-to-block-c%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Because pthread_join(thread_id, NULL); waits for the thread to finish. And a thread running a while (1) {} loop will never finish.






share|improve this answer
























  • do I need the pthread_join(thread_id, NULL); line then? @MartinHeralecký

    – Matt X
    Nov 21 '18 at 21:29













  • @MattX If you don't put it there, the After Thread message will print immediately after starting the thread, which will keep running.

    – Martin Heralecký
    Nov 21 '18 at 21:31













  • If you changed the code in myThread to loop 10 times and print something out or sleep and then return, you'd wait for it to finish with the call to pthread_join in main. And after that your program could exit. If you just leave myThread as it is and take out the call to pthread_join, your program won't wait for the thread to finish and exit right away.

    – bruceg
    Nov 21 '18 at 21:36











  • @MattX Whether or not you need it depends on exactly what you're trying to do, which we do not know. What you should do depends on exactly what this thread is doing and how it relates to what the program as a whole is doing.

    – David Schwartz
    Nov 21 '18 at 21:39


















2














Because pthread_join(thread_id, NULL); waits for the thread to finish. And a thread running a while (1) {} loop will never finish.






share|improve this answer
























  • do I need the pthread_join(thread_id, NULL); line then? @MartinHeralecký

    – Matt X
    Nov 21 '18 at 21:29













  • @MattX If you don't put it there, the After Thread message will print immediately after starting the thread, which will keep running.

    – Martin Heralecký
    Nov 21 '18 at 21:31













  • If you changed the code in myThread to loop 10 times and print something out or sleep and then return, you'd wait for it to finish with the call to pthread_join in main. And after that your program could exit. If you just leave myThread as it is and take out the call to pthread_join, your program won't wait for the thread to finish and exit right away.

    – bruceg
    Nov 21 '18 at 21:36











  • @MattX Whether or not you need it depends on exactly what you're trying to do, which we do not know. What you should do depends on exactly what this thread is doing and how it relates to what the program as a whole is doing.

    – David Schwartz
    Nov 21 '18 at 21:39
















2












2








2







Because pthread_join(thread_id, NULL); waits for the thread to finish. And a thread running a while (1) {} loop will never finish.






share|improve this answer













Because pthread_join(thread_id, NULL); waits for the thread to finish. And a thread running a while (1) {} loop will never finish.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 21:27









Martin HeraleckýMartin Heralecký

3,11721134




3,11721134













  • do I need the pthread_join(thread_id, NULL); line then? @MartinHeralecký

    – Matt X
    Nov 21 '18 at 21:29













  • @MattX If you don't put it there, the After Thread message will print immediately after starting the thread, which will keep running.

    – Martin Heralecký
    Nov 21 '18 at 21:31













  • If you changed the code in myThread to loop 10 times and print something out or sleep and then return, you'd wait for it to finish with the call to pthread_join in main. And after that your program could exit. If you just leave myThread as it is and take out the call to pthread_join, your program won't wait for the thread to finish and exit right away.

    – bruceg
    Nov 21 '18 at 21:36











  • @MattX Whether or not you need it depends on exactly what you're trying to do, which we do not know. What you should do depends on exactly what this thread is doing and how it relates to what the program as a whole is doing.

    – David Schwartz
    Nov 21 '18 at 21:39





















  • do I need the pthread_join(thread_id, NULL); line then? @MartinHeralecký

    – Matt X
    Nov 21 '18 at 21:29













  • @MattX If you don't put it there, the After Thread message will print immediately after starting the thread, which will keep running.

    – Martin Heralecký
    Nov 21 '18 at 21:31













  • If you changed the code in myThread to loop 10 times and print something out or sleep and then return, you'd wait for it to finish with the call to pthread_join in main. And after that your program could exit. If you just leave myThread as it is and take out the call to pthread_join, your program won't wait for the thread to finish and exit right away.

    – bruceg
    Nov 21 '18 at 21:36











  • @MattX Whether or not you need it depends on exactly what you're trying to do, which we do not know. What you should do depends on exactly what this thread is doing and how it relates to what the program as a whole is doing.

    – David Schwartz
    Nov 21 '18 at 21:39



















do I need the pthread_join(thread_id, NULL); line then? @MartinHeralecký

– Matt X
Nov 21 '18 at 21:29







do I need the pthread_join(thread_id, NULL); line then? @MartinHeralecký

– Matt X
Nov 21 '18 at 21:29















@MattX If you don't put it there, the After Thread message will print immediately after starting the thread, which will keep running.

– Martin Heralecký
Nov 21 '18 at 21:31







@MattX If you don't put it there, the After Thread message will print immediately after starting the thread, which will keep running.

– Martin Heralecký
Nov 21 '18 at 21:31















If you changed the code in myThread to loop 10 times and print something out or sleep and then return, you'd wait for it to finish with the call to pthread_join in main. And after that your program could exit. If you just leave myThread as it is and take out the call to pthread_join, your program won't wait for the thread to finish and exit right away.

– bruceg
Nov 21 '18 at 21:36





If you changed the code in myThread to loop 10 times and print something out or sleep and then return, you'd wait for it to finish with the call to pthread_join in main. And after that your program could exit. If you just leave myThread as it is and take out the call to pthread_join, your program won't wait for the thread to finish and exit right away.

– bruceg
Nov 21 '18 at 21:36













@MattX Whether or not you need it depends on exactly what you're trying to do, which we do not know. What you should do depends on exactly what this thread is doing and how it relates to what the program as a whole is doing.

– David Schwartz
Nov 21 '18 at 21:39







@MattX Whether or not you need it depends on exactly what you're trying to do, which we do not know. What you should do depends on exactly what this thread is doing and how it relates to what the program as a whole is doing.

– David Schwartz
Nov 21 '18 at 21:39






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53420666%2fwhile-loop-within-thread-is-causing-program-to-block-c%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory