How to none blocked join to std thread
I want to keep my code clean and do the things right, to any std::thread
I need to do join or detach, but how can I wait (at the main thread) for another thread without blocking the execution of the main thread?
void do_computation()
{
// Calculate 1000 digits of Pi.
}
int main()
{
std::thread td1(&do_computation);
while (running)
{
// Check if thread td1 finish and if yes print a message
// Here are some stuff of the main to do...
// Print to UI, update timer etc..
}
// If the thread has not finished yet here, just kill it.
}
c++ stdthread
|
show 4 more comments
I want to keep my code clean and do the things right, to any std::thread
I need to do join or detach, but how can I wait (at the main thread) for another thread without blocking the execution of the main thread?
void do_computation()
{
// Calculate 1000 digits of Pi.
}
int main()
{
std::thread td1(&do_computation);
while (running)
{
// Check if thread td1 finish and if yes print a message
// Here are some stuff of the main to do...
// Print to UI, update timer etc..
}
// If the thread has not finished yet here, just kill it.
}
c++ stdthread
1
If, as you confess, you can communicate with the thread to see if it finished, communicate in reverse and tell it to stop...
– StoryTeller
Jan 1 at 10:09
The only way a thread can "wait" for another is by blocking. Those words are synonymous in this context, so it's not clear what exactly do you want to achieve. Or do you want to stop the thread instead of waiting for it?
– r3mus n0x
Jan 1 at 10:28
@Ron - what you mean under "not to block" ?
– RbMm
Jan 1 at 10:35
I want to check if a thread has been finished, but not to block the checker thread. As i see that can be achieved using mutex/semaphore that the worker thread will set and the main thread will check, this seems to be a good idea. But what if I got an exception at the worker thread? As i see in this case the mutex will never set, so i will just want to do join in order to clean any resource and continue with execution of the main. How can I achieve that?
– Ron
Jan 1 at 10:43
but not to block the checker thread - what you mean under this, what is checker thread doing else ?
– RbMm
Jan 1 at 10:48
|
show 4 more comments
I want to keep my code clean and do the things right, to any std::thread
I need to do join or detach, but how can I wait (at the main thread) for another thread without blocking the execution of the main thread?
void do_computation()
{
// Calculate 1000 digits of Pi.
}
int main()
{
std::thread td1(&do_computation);
while (running)
{
// Check if thread td1 finish and if yes print a message
// Here are some stuff of the main to do...
// Print to UI, update timer etc..
}
// If the thread has not finished yet here, just kill it.
}
c++ stdthread
I want to keep my code clean and do the things right, to any std::thread
I need to do join or detach, but how can I wait (at the main thread) for another thread without blocking the execution of the main thread?
void do_computation()
{
// Calculate 1000 digits of Pi.
}
int main()
{
std::thread td1(&do_computation);
while (running)
{
// Check if thread td1 finish and if yes print a message
// Here are some stuff of the main to do...
// Print to UI, update timer etc..
}
// If the thread has not finished yet here, just kill it.
}
c++ stdthread
c++ stdthread
edited Jan 1 at 10:55
Ron
asked Jan 1 at 10:03
RonRon
6819
6819
1
If, as you confess, you can communicate with the thread to see if it finished, communicate in reverse and tell it to stop...
– StoryTeller
Jan 1 at 10:09
The only way a thread can "wait" for another is by blocking. Those words are synonymous in this context, so it's not clear what exactly do you want to achieve. Or do you want to stop the thread instead of waiting for it?
– r3mus n0x
Jan 1 at 10:28
@Ron - what you mean under "not to block" ?
– RbMm
Jan 1 at 10:35
I want to check if a thread has been finished, but not to block the checker thread. As i see that can be achieved using mutex/semaphore that the worker thread will set and the main thread will check, this seems to be a good idea. But what if I got an exception at the worker thread? As i see in this case the mutex will never set, so i will just want to do join in order to clean any resource and continue with execution of the main. How can I achieve that?
– Ron
Jan 1 at 10:43
but not to block the checker thread - what you mean under this, what is checker thread doing else ?
– RbMm
Jan 1 at 10:48
|
show 4 more comments
1
If, as you confess, you can communicate with the thread to see if it finished, communicate in reverse and tell it to stop...
– StoryTeller
Jan 1 at 10:09
The only way a thread can "wait" for another is by blocking. Those words are synonymous in this context, so it's not clear what exactly do you want to achieve. Or do you want to stop the thread instead of waiting for it?
– r3mus n0x
Jan 1 at 10:28
@Ron - what you mean under "not to block" ?
– RbMm
Jan 1 at 10:35
I want to check if a thread has been finished, but not to block the checker thread. As i see that can be achieved using mutex/semaphore that the worker thread will set and the main thread will check, this seems to be a good idea. But what if I got an exception at the worker thread? As i see in this case the mutex will never set, so i will just want to do join in order to clean any resource and continue with execution of the main. How can I achieve that?
– Ron
Jan 1 at 10:43
but not to block the checker thread - what you mean under this, what is checker thread doing else ?
– RbMm
Jan 1 at 10:48
1
1
If, as you confess, you can communicate with the thread to see if it finished, communicate in reverse and tell it to stop...
– StoryTeller
Jan 1 at 10:09
If, as you confess, you can communicate with the thread to see if it finished, communicate in reverse and tell it to stop...
– StoryTeller
Jan 1 at 10:09
The only way a thread can "wait" for another is by blocking. Those words are synonymous in this context, so it's not clear what exactly do you want to achieve. Or do you want to stop the thread instead of waiting for it?
– r3mus n0x
Jan 1 at 10:28
The only way a thread can "wait" for another is by blocking. Those words are synonymous in this context, so it's not clear what exactly do you want to achieve. Or do you want to stop the thread instead of waiting for it?
– r3mus n0x
Jan 1 at 10:28
@Ron - what you mean under "not to block" ?
– RbMm
Jan 1 at 10:35
@Ron - what you mean under "not to block" ?
– RbMm
Jan 1 at 10:35
I want to check if a thread has been finished, but not to block the checker thread. As i see that can be achieved using mutex/semaphore that the worker thread will set and the main thread will check, this seems to be a good idea. But what if I got an exception at the worker thread? As i see in this case the mutex will never set, so i will just want to do join in order to clean any resource and continue with execution of the main. How can I achieve that?
– Ron
Jan 1 at 10:43
I want to check if a thread has been finished, but not to block the checker thread. As i see that can be achieved using mutex/semaphore that the worker thread will set and the main thread will check, this seems to be a good idea. But what if I got an exception at the worker thread? As i see in this case the mutex will never set, so i will just want to do join in order to clean any resource and continue with execution of the main. How can I achieve that?
– Ron
Jan 1 at 10:43
but not to block the checker thread - what you mean under this, what is checker thread doing else ?
– RbMm
Jan 1 at 10:48
but not to block the checker thread - what you mean under this, what is checker thread doing else ?
– RbMm
Jan 1 at 10:48
|
show 4 more comments
2 Answers
2
active
oldest
votes
The answer is semaphores. You can use a binary semaphore to synchronize your threads.
You may use System V semaphores or pthread mutexes, but they are somehow legacy in C++. Using Tsuneo Yoshioka's answer, we could implement a C++ way of semaphore, though.
#include <mutex>
#include <condition_variable>
class Semaphore {
public:
Semaphore (int count_ = 0)
: count(count_) {}
inline void notify()
{
std::unique_lock<std::mutex> lock(mtx);
count++;
cv.notify_one();
}
inline void wait()
{
std::unique_lock<std::mutex> lock(mtx);
while(count == 0){
cv.wait(lock);
}
count--;
}
private:
std::mutex mtx;
std::condition_variable cv;
int count;
};
Your implementation may make use of the Semaphore
class, like so.
void do_computation()
{
//calculate 1000 digits of Pi.
semaphore.notify();
}
int main()
{
Semaphore semaphore(0);
std::thread td1(&do_computation);
semaphore.wait();
}
As i see the linesemaphore.wait();
is blocking. Am I right?
– Ron
Jan 1 at 10:30
I actually thought you were asking for a solution without busy waiting. You may use atry_wait
implementation to do that, or moreover, head over tostd::promise
andstd::future
.
– Leviathlon
Jan 1 at 10:48
add a comment |
You can use std::promise and std::future. More info here and here.
#include <vector>
#include <thread>
#include <future>
#include <numeric>
#include <iostream>
#include <chrono>
void accumulate(std::vector<int>::iterator first,
std::vector<int>::iterator last,
std::promise<int> accumulate_promise)
{
int sum = std::accumulate(first, last, 0);
accumulate_promise.set_value(sum); // Notify future
}
void do_work(std::promise<void> barrier)
{
std::this_thread::sleep_for(std::chrono::seconds(1));
barrier.set_value();
}
int main()
{
// Demonstrate using promise<int> to transmit a result between threads.
std::vector<int> numbers = { 1, 2, 3, 4, 5, 6 };
std::promise<int> accumulate_promise;
std::future<int> accumulate_future = accumulate_promise.get_future();
std::thread work_thread(accumulate, numbers.begin(), numbers.end(),
std::move(accumulate_promise));
accumulate_future.wait(); // wait for result
std::cout << "result=" << accumulate_future.get() << 'n';
work_thread.join(); // wait for thread completion
// Demonstrate using promise<void> to signal state between threads.
std::promise<void> barrier;
std::future<void> barrier_future = barrier.get_future();
std::thread new_work_thread(do_work, std::move(barrier));
barrier_future.wait();
new_work_thread.join();
}
Please improve your answer showing a practical example, instead of only providing external links.
– πάντα ῥεῖ
Jan 1 at 12:20
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%2f53994567%2fhow-to-none-blocked-join-to-std-thread%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The answer is semaphores. You can use a binary semaphore to synchronize your threads.
You may use System V semaphores or pthread mutexes, but they are somehow legacy in C++. Using Tsuneo Yoshioka's answer, we could implement a C++ way of semaphore, though.
#include <mutex>
#include <condition_variable>
class Semaphore {
public:
Semaphore (int count_ = 0)
: count(count_) {}
inline void notify()
{
std::unique_lock<std::mutex> lock(mtx);
count++;
cv.notify_one();
}
inline void wait()
{
std::unique_lock<std::mutex> lock(mtx);
while(count == 0){
cv.wait(lock);
}
count--;
}
private:
std::mutex mtx;
std::condition_variable cv;
int count;
};
Your implementation may make use of the Semaphore
class, like so.
void do_computation()
{
//calculate 1000 digits of Pi.
semaphore.notify();
}
int main()
{
Semaphore semaphore(0);
std::thread td1(&do_computation);
semaphore.wait();
}
As i see the linesemaphore.wait();
is blocking. Am I right?
– Ron
Jan 1 at 10:30
I actually thought you were asking for a solution without busy waiting. You may use atry_wait
implementation to do that, or moreover, head over tostd::promise
andstd::future
.
– Leviathlon
Jan 1 at 10:48
add a comment |
The answer is semaphores. You can use a binary semaphore to synchronize your threads.
You may use System V semaphores or pthread mutexes, but they are somehow legacy in C++. Using Tsuneo Yoshioka's answer, we could implement a C++ way of semaphore, though.
#include <mutex>
#include <condition_variable>
class Semaphore {
public:
Semaphore (int count_ = 0)
: count(count_) {}
inline void notify()
{
std::unique_lock<std::mutex> lock(mtx);
count++;
cv.notify_one();
}
inline void wait()
{
std::unique_lock<std::mutex> lock(mtx);
while(count == 0){
cv.wait(lock);
}
count--;
}
private:
std::mutex mtx;
std::condition_variable cv;
int count;
};
Your implementation may make use of the Semaphore
class, like so.
void do_computation()
{
//calculate 1000 digits of Pi.
semaphore.notify();
}
int main()
{
Semaphore semaphore(0);
std::thread td1(&do_computation);
semaphore.wait();
}
As i see the linesemaphore.wait();
is blocking. Am I right?
– Ron
Jan 1 at 10:30
I actually thought you were asking for a solution without busy waiting. You may use atry_wait
implementation to do that, or moreover, head over tostd::promise
andstd::future
.
– Leviathlon
Jan 1 at 10:48
add a comment |
The answer is semaphores. You can use a binary semaphore to synchronize your threads.
You may use System V semaphores or pthread mutexes, but they are somehow legacy in C++. Using Tsuneo Yoshioka's answer, we could implement a C++ way of semaphore, though.
#include <mutex>
#include <condition_variable>
class Semaphore {
public:
Semaphore (int count_ = 0)
: count(count_) {}
inline void notify()
{
std::unique_lock<std::mutex> lock(mtx);
count++;
cv.notify_one();
}
inline void wait()
{
std::unique_lock<std::mutex> lock(mtx);
while(count == 0){
cv.wait(lock);
}
count--;
}
private:
std::mutex mtx;
std::condition_variable cv;
int count;
};
Your implementation may make use of the Semaphore
class, like so.
void do_computation()
{
//calculate 1000 digits of Pi.
semaphore.notify();
}
int main()
{
Semaphore semaphore(0);
std::thread td1(&do_computation);
semaphore.wait();
}
The answer is semaphores. You can use a binary semaphore to synchronize your threads.
You may use System V semaphores or pthread mutexes, but they are somehow legacy in C++. Using Tsuneo Yoshioka's answer, we could implement a C++ way of semaphore, though.
#include <mutex>
#include <condition_variable>
class Semaphore {
public:
Semaphore (int count_ = 0)
: count(count_) {}
inline void notify()
{
std::unique_lock<std::mutex> lock(mtx);
count++;
cv.notify_one();
}
inline void wait()
{
std::unique_lock<std::mutex> lock(mtx);
while(count == 0){
cv.wait(lock);
}
count--;
}
private:
std::mutex mtx;
std::condition_variable cv;
int count;
};
Your implementation may make use of the Semaphore
class, like so.
void do_computation()
{
//calculate 1000 digits of Pi.
semaphore.notify();
}
int main()
{
Semaphore semaphore(0);
std::thread td1(&do_computation);
semaphore.wait();
}
answered Jan 1 at 10:11
LeviathlonLeviathlon
533619
533619
As i see the linesemaphore.wait();
is blocking. Am I right?
– Ron
Jan 1 at 10:30
I actually thought you were asking for a solution without busy waiting. You may use atry_wait
implementation to do that, or moreover, head over tostd::promise
andstd::future
.
– Leviathlon
Jan 1 at 10:48
add a comment |
As i see the linesemaphore.wait();
is blocking. Am I right?
– Ron
Jan 1 at 10:30
I actually thought you were asking for a solution without busy waiting. You may use atry_wait
implementation to do that, or moreover, head over tostd::promise
andstd::future
.
– Leviathlon
Jan 1 at 10:48
As i see the line
semaphore.wait();
is blocking. Am I right?– Ron
Jan 1 at 10:30
As i see the line
semaphore.wait();
is blocking. Am I right?– Ron
Jan 1 at 10:30
I actually thought you were asking for a solution without busy waiting. You may use a
try_wait
implementation to do that, or moreover, head over to std::promise
and std::future
.– Leviathlon
Jan 1 at 10:48
I actually thought you were asking for a solution without busy waiting. You may use a
try_wait
implementation to do that, or moreover, head over to std::promise
and std::future
.– Leviathlon
Jan 1 at 10:48
add a comment |
You can use std::promise and std::future. More info here and here.
#include <vector>
#include <thread>
#include <future>
#include <numeric>
#include <iostream>
#include <chrono>
void accumulate(std::vector<int>::iterator first,
std::vector<int>::iterator last,
std::promise<int> accumulate_promise)
{
int sum = std::accumulate(first, last, 0);
accumulate_promise.set_value(sum); // Notify future
}
void do_work(std::promise<void> barrier)
{
std::this_thread::sleep_for(std::chrono::seconds(1));
barrier.set_value();
}
int main()
{
// Demonstrate using promise<int> to transmit a result between threads.
std::vector<int> numbers = { 1, 2, 3, 4, 5, 6 };
std::promise<int> accumulate_promise;
std::future<int> accumulate_future = accumulate_promise.get_future();
std::thread work_thread(accumulate, numbers.begin(), numbers.end(),
std::move(accumulate_promise));
accumulate_future.wait(); // wait for result
std::cout << "result=" << accumulate_future.get() << 'n';
work_thread.join(); // wait for thread completion
// Demonstrate using promise<void> to signal state between threads.
std::promise<void> barrier;
std::future<void> barrier_future = barrier.get_future();
std::thread new_work_thread(do_work, std::move(barrier));
barrier_future.wait();
new_work_thread.join();
}
Please improve your answer showing a practical example, instead of only providing external links.
– πάντα ῥεῖ
Jan 1 at 12:20
add a comment |
You can use std::promise and std::future. More info here and here.
#include <vector>
#include <thread>
#include <future>
#include <numeric>
#include <iostream>
#include <chrono>
void accumulate(std::vector<int>::iterator first,
std::vector<int>::iterator last,
std::promise<int> accumulate_promise)
{
int sum = std::accumulate(first, last, 0);
accumulate_promise.set_value(sum); // Notify future
}
void do_work(std::promise<void> barrier)
{
std::this_thread::sleep_for(std::chrono::seconds(1));
barrier.set_value();
}
int main()
{
// Demonstrate using promise<int> to transmit a result between threads.
std::vector<int> numbers = { 1, 2, 3, 4, 5, 6 };
std::promise<int> accumulate_promise;
std::future<int> accumulate_future = accumulate_promise.get_future();
std::thread work_thread(accumulate, numbers.begin(), numbers.end(),
std::move(accumulate_promise));
accumulate_future.wait(); // wait for result
std::cout << "result=" << accumulate_future.get() << 'n';
work_thread.join(); // wait for thread completion
// Demonstrate using promise<void> to signal state between threads.
std::promise<void> barrier;
std::future<void> barrier_future = barrier.get_future();
std::thread new_work_thread(do_work, std::move(barrier));
barrier_future.wait();
new_work_thread.join();
}
Please improve your answer showing a practical example, instead of only providing external links.
– πάντα ῥεῖ
Jan 1 at 12:20
add a comment |
You can use std::promise and std::future. More info here and here.
#include <vector>
#include <thread>
#include <future>
#include <numeric>
#include <iostream>
#include <chrono>
void accumulate(std::vector<int>::iterator first,
std::vector<int>::iterator last,
std::promise<int> accumulate_promise)
{
int sum = std::accumulate(first, last, 0);
accumulate_promise.set_value(sum); // Notify future
}
void do_work(std::promise<void> barrier)
{
std::this_thread::sleep_for(std::chrono::seconds(1));
barrier.set_value();
}
int main()
{
// Demonstrate using promise<int> to transmit a result between threads.
std::vector<int> numbers = { 1, 2, 3, 4, 5, 6 };
std::promise<int> accumulate_promise;
std::future<int> accumulate_future = accumulate_promise.get_future();
std::thread work_thread(accumulate, numbers.begin(), numbers.end(),
std::move(accumulate_promise));
accumulate_future.wait(); // wait for result
std::cout << "result=" << accumulate_future.get() << 'n';
work_thread.join(); // wait for thread completion
// Demonstrate using promise<void> to signal state between threads.
std::promise<void> barrier;
std::future<void> barrier_future = barrier.get_future();
std::thread new_work_thread(do_work, std::move(barrier));
barrier_future.wait();
new_work_thread.join();
}
You can use std::promise and std::future. More info here and here.
#include <vector>
#include <thread>
#include <future>
#include <numeric>
#include <iostream>
#include <chrono>
void accumulate(std::vector<int>::iterator first,
std::vector<int>::iterator last,
std::promise<int> accumulate_promise)
{
int sum = std::accumulate(first, last, 0);
accumulate_promise.set_value(sum); // Notify future
}
void do_work(std::promise<void> barrier)
{
std::this_thread::sleep_for(std::chrono::seconds(1));
barrier.set_value();
}
int main()
{
// Demonstrate using promise<int> to transmit a result between threads.
std::vector<int> numbers = { 1, 2, 3, 4, 5, 6 };
std::promise<int> accumulate_promise;
std::future<int> accumulate_future = accumulate_promise.get_future();
std::thread work_thread(accumulate, numbers.begin(), numbers.end(),
std::move(accumulate_promise));
accumulate_future.wait(); // wait for result
std::cout << "result=" << accumulate_future.get() << 'n';
work_thread.join(); // wait for thread completion
// Demonstrate using promise<void> to signal state between threads.
std::promise<void> barrier;
std::future<void> barrier_future = barrier.get_future();
std::thread new_work_thread(do_work, std::move(barrier));
barrier_future.wait();
new_work_thread.join();
}
edited Jan 1 at 13:11
answered Jan 1 at 10:20
MichaelMichael
1,4231836
1,4231836
Please improve your answer showing a practical example, instead of only providing external links.
– πάντα ῥεῖ
Jan 1 at 12:20
add a comment |
Please improve your answer showing a practical example, instead of only providing external links.
– πάντα ῥεῖ
Jan 1 at 12:20
Please improve your answer showing a practical example, instead of only providing external links.
– πάντα ῥεῖ
Jan 1 at 12:20
Please improve your answer showing a practical example, instead of only providing external links.
– πάντα ῥεῖ
Jan 1 at 12:20
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%2f53994567%2fhow-to-none-blocked-join-to-std-thread%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
1
If, as you confess, you can communicate with the thread to see if it finished, communicate in reverse and tell it to stop...
– StoryTeller
Jan 1 at 10:09
The only way a thread can "wait" for another is by blocking. Those words are synonymous in this context, so it's not clear what exactly do you want to achieve. Or do you want to stop the thread instead of waiting for it?
– r3mus n0x
Jan 1 at 10:28
@Ron - what you mean under "not to block" ?
– RbMm
Jan 1 at 10:35
I want to check if a thread has been finished, but not to block the checker thread. As i see that can be achieved using mutex/semaphore that the worker thread will set and the main thread will check, this seems to be a good idea. But what if I got an exception at the worker thread? As i see in this case the mutex will never set, so i will just want to do join in order to clean any resource and continue with execution of the main. How can I achieve that?
– Ron
Jan 1 at 10:43
but not to block the checker thread - what you mean under this, what is checker thread doing else ?
– RbMm
Jan 1 at 10:48