While loop within thread is causing program to block (C)
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
add a comment |
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
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
add a comment |
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
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
c multithreading
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
Because pthread_join(thread_id, NULL);
waits for the thread to finish. And a thread running a while (1) {}
loop will never finish.
do I need thepthread_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 inmyThread
to loop 10 times and print something out or sleep and then return, you'd wait for it to finish with the call topthread_join
in main. And after that your program could exit. If you just leavemyThread
as it is and take out the call topthread_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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Because pthread_join(thread_id, NULL);
waits for the thread to finish. And a thread running a while (1) {}
loop will never finish.
do I need thepthread_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 inmyThread
to loop 10 times and print something out or sleep and then return, you'd wait for it to finish with the call topthread_join
in main. And after that your program could exit. If you just leavemyThread
as it is and take out the call topthread_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
add a comment |
Because pthread_join(thread_id, NULL);
waits for the thread to finish. And a thread running a while (1) {}
loop will never finish.
do I need thepthread_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 inmyThread
to loop 10 times and print something out or sleep and then return, you'd wait for it to finish with the call topthread_join
in main. And after that your program could exit. If you just leavemyThread
as it is and take out the call topthread_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
add a comment |
Because pthread_join(thread_id, NULL);
waits for the thread to finish. And a thread running a while (1) {}
loop will never finish.
Because pthread_join(thread_id, NULL);
waits for the thread to finish. And a thread running a while (1) {}
loop will never finish.
answered Nov 21 '18 at 21:27


Martin HeraleckýMartin Heralecký
3,11721134
3,11721134
do I need thepthread_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 inmyThread
to loop 10 times and print something out or sleep and then return, you'd wait for it to finish with the call topthread_join
in main. And after that your program could exit. If you just leavemyThread
as it is and take out the call topthread_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
add a comment |
do I need thepthread_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 inmyThread
to loop 10 times and print something out or sleep and then return, you'd wait for it to finish with the call topthread_join
in main. And after that your program could exit. If you just leavemyThread
as it is and take out the call topthread_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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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