In a macOS Application, I have a background thread executing with one single line of code taking a lot of...
In a Cocoa Objective-C Application that I am developing, I have a NSTableView with a delegate that implements tableViewSelectionDidChange. In the delegate implementation of the method, I have put a background serial queue implemented with GCD that handles loading the currently selected object in the table for it to display in a details NSView that is to the right of the table. My desired behaviour would be that if the background thread has not finished and there is a new selection in the left table, I would like to cancel it and start a new thread for the new selection. I have found a very elegant solution using GCD here that uses:
dispatch_block_cancel(work);
The problem is that, like it is said in the link:
"One thing to note is that dispatch_block_cancel is not pre-emptive. If the worker block is in the middle of a long-running operation, dispatch_block_cancel won't force-terminate it. To do this, we have to periodically test for cancellation with dispatch_block_testcancel. Here's an example:"
for (...) {
/* do some work */
[NSThread sleepForTimeInterval:0.2];
if (dispatch_block_testcancel(work) != 0) {
/* exit gracefully */
return;
}
}
The problem is that in my application, the most lengthy operation performed in the tableViewSelectionDidChange implementation is in a single line of code and I cannot refactor my code to change this because that statement is calling an external dylib that I am using. In this case I cannot check if the block is cancelled with:
if (dispatch_block_testcancel(work) != 0) {
/* exit gracefully */
return;
}
because the thread is blocked on a lengthy one line operation. Can somebody hopefully suggest a strategy of some kind, preferably still using GCD ? I am aware of NSOperation and NSOperationQueue, but before learning to use them, I would like to be certain they do not have problems cancelling operations in the middle of a 1 line statement like GCD apparently does. Thanks a lot for any help.
objective-c cocoa grand-central-dispatch
add a comment |
In a Cocoa Objective-C Application that I am developing, I have a NSTableView with a delegate that implements tableViewSelectionDidChange. In the delegate implementation of the method, I have put a background serial queue implemented with GCD that handles loading the currently selected object in the table for it to display in a details NSView that is to the right of the table. My desired behaviour would be that if the background thread has not finished and there is a new selection in the left table, I would like to cancel it and start a new thread for the new selection. I have found a very elegant solution using GCD here that uses:
dispatch_block_cancel(work);
The problem is that, like it is said in the link:
"One thing to note is that dispatch_block_cancel is not pre-emptive. If the worker block is in the middle of a long-running operation, dispatch_block_cancel won't force-terminate it. To do this, we have to periodically test for cancellation with dispatch_block_testcancel. Here's an example:"
for (...) {
/* do some work */
[NSThread sleepForTimeInterval:0.2];
if (dispatch_block_testcancel(work) != 0) {
/* exit gracefully */
return;
}
}
The problem is that in my application, the most lengthy operation performed in the tableViewSelectionDidChange implementation is in a single line of code and I cannot refactor my code to change this because that statement is calling an external dylib that I am using. In this case I cannot check if the block is cancelled with:
if (dispatch_block_testcancel(work) != 0) {
/* exit gracefully */
return;
}
because the thread is blocked on a lengthy one line operation. Can somebody hopefully suggest a strategy of some kind, preferably still using GCD ? I am aware of NSOperation and NSOperationQueue, but before learning to use them, I would like to be certain they do not have problems cancelling operations in the middle of a 1 line statement like GCD apparently does. Thanks a lot for any help.
objective-c cocoa grand-central-dispatch
I hope you are not thinking of leaving[NSThread sleepForTimeInterval:0.2]
in your shipping code. You should never sleep.
– matt
Jan 2 at 22:30
1
The usual strategy is to break the work up into short bits and test for cancellation both before and after the line of code that does a bit. That way, you give yourself at least the best chance of intercepting the cancellation. That is what you would do in NSOperation as well.
– matt
Jan 2 at 22:31
add a comment |
In a Cocoa Objective-C Application that I am developing, I have a NSTableView with a delegate that implements tableViewSelectionDidChange. In the delegate implementation of the method, I have put a background serial queue implemented with GCD that handles loading the currently selected object in the table for it to display in a details NSView that is to the right of the table. My desired behaviour would be that if the background thread has not finished and there is a new selection in the left table, I would like to cancel it and start a new thread for the new selection. I have found a very elegant solution using GCD here that uses:
dispatch_block_cancel(work);
The problem is that, like it is said in the link:
"One thing to note is that dispatch_block_cancel is not pre-emptive. If the worker block is in the middle of a long-running operation, dispatch_block_cancel won't force-terminate it. To do this, we have to periodically test for cancellation with dispatch_block_testcancel. Here's an example:"
for (...) {
/* do some work */
[NSThread sleepForTimeInterval:0.2];
if (dispatch_block_testcancel(work) != 0) {
/* exit gracefully */
return;
}
}
The problem is that in my application, the most lengthy operation performed in the tableViewSelectionDidChange implementation is in a single line of code and I cannot refactor my code to change this because that statement is calling an external dylib that I am using. In this case I cannot check if the block is cancelled with:
if (dispatch_block_testcancel(work) != 0) {
/* exit gracefully */
return;
}
because the thread is blocked on a lengthy one line operation. Can somebody hopefully suggest a strategy of some kind, preferably still using GCD ? I am aware of NSOperation and NSOperationQueue, but before learning to use them, I would like to be certain they do not have problems cancelling operations in the middle of a 1 line statement like GCD apparently does. Thanks a lot for any help.
objective-c cocoa grand-central-dispatch
In a Cocoa Objective-C Application that I am developing, I have a NSTableView with a delegate that implements tableViewSelectionDidChange. In the delegate implementation of the method, I have put a background serial queue implemented with GCD that handles loading the currently selected object in the table for it to display in a details NSView that is to the right of the table. My desired behaviour would be that if the background thread has not finished and there is a new selection in the left table, I would like to cancel it and start a new thread for the new selection. I have found a very elegant solution using GCD here that uses:
dispatch_block_cancel(work);
The problem is that, like it is said in the link:
"One thing to note is that dispatch_block_cancel is not pre-emptive. If the worker block is in the middle of a long-running operation, dispatch_block_cancel won't force-terminate it. To do this, we have to periodically test for cancellation with dispatch_block_testcancel. Here's an example:"
for (...) {
/* do some work */
[NSThread sleepForTimeInterval:0.2];
if (dispatch_block_testcancel(work) != 0) {
/* exit gracefully */
return;
}
}
The problem is that in my application, the most lengthy operation performed in the tableViewSelectionDidChange implementation is in a single line of code and I cannot refactor my code to change this because that statement is calling an external dylib that I am using. In this case I cannot check if the block is cancelled with:
if (dispatch_block_testcancel(work) != 0) {
/* exit gracefully */
return;
}
because the thread is blocked on a lengthy one line operation. Can somebody hopefully suggest a strategy of some kind, preferably still using GCD ? I am aware of NSOperation and NSOperationQueue, but before learning to use them, I would like to be certain they do not have problems cancelling operations in the middle of a 1 line statement like GCD apparently does. Thanks a lot for any help.
objective-c cocoa grand-central-dispatch
objective-c cocoa grand-central-dispatch
asked Jan 2 at 22:24


Alfonso TesauroAlfonso Tesauro
355314
355314
I hope you are not thinking of leaving[NSThread sleepForTimeInterval:0.2]
in your shipping code. You should never sleep.
– matt
Jan 2 at 22:30
1
The usual strategy is to break the work up into short bits and test for cancellation both before and after the line of code that does a bit. That way, you give yourself at least the best chance of intercepting the cancellation. That is what you would do in NSOperation as well.
– matt
Jan 2 at 22:31
add a comment |
I hope you are not thinking of leaving[NSThread sleepForTimeInterval:0.2]
in your shipping code. You should never sleep.
– matt
Jan 2 at 22:30
1
The usual strategy is to break the work up into short bits and test for cancellation both before and after the line of code that does a bit. That way, you give yourself at least the best chance of intercepting the cancellation. That is what you would do in NSOperation as well.
– matt
Jan 2 at 22:31
I hope you are not thinking of leaving
[NSThread sleepForTimeInterval:0.2]
in your shipping code. You should never sleep.– matt
Jan 2 at 22:30
I hope you are not thinking of leaving
[NSThread sleepForTimeInterval:0.2]
in your shipping code. You should never sleep.– matt
Jan 2 at 22:30
1
1
The usual strategy is to break the work up into short bits and test for cancellation both before and after the line of code that does a bit. That way, you give yourself at least the best chance of intercepting the cancellation. That is what you would do in NSOperation as well.
– matt
Jan 2 at 22:31
The usual strategy is to break the work up into short bits and test for cancellation both before and after the line of code that does a bit. That way, you give yourself at least the best chance of intercepting the cancellation. That is what you would do in NSOperation as well.
– matt
Jan 2 at 22:31
add a comment |
1 Answer
1
active
oldest
votes
This is not solvable. It doesn't matter which tool you use (GCD vs NSOperation). It is not possible to forcibly stop an operation in progress. It would leave memory in an unknown state. The solution is to wait for the line of code to complete, and then if the block was cancelled, drop the result. If the line of code is very costly such that you really need to terminate it (rather than just ignore its result), then you will have to replace the library with code that checks a cancel flag.
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%2f54013959%2fin-a-macos-application-i-have-a-background-thread-executing-with-one-single-lin%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
This is not solvable. It doesn't matter which tool you use (GCD vs NSOperation). It is not possible to forcibly stop an operation in progress. It would leave memory in an unknown state. The solution is to wait for the line of code to complete, and then if the block was cancelled, drop the result. If the line of code is very costly such that you really need to terminate it (rather than just ignore its result), then you will have to replace the library with code that checks a cancel flag.
add a comment |
This is not solvable. It doesn't matter which tool you use (GCD vs NSOperation). It is not possible to forcibly stop an operation in progress. It would leave memory in an unknown state. The solution is to wait for the line of code to complete, and then if the block was cancelled, drop the result. If the line of code is very costly such that you really need to terminate it (rather than just ignore its result), then you will have to replace the library with code that checks a cancel flag.
add a comment |
This is not solvable. It doesn't matter which tool you use (GCD vs NSOperation). It is not possible to forcibly stop an operation in progress. It would leave memory in an unknown state. The solution is to wait for the line of code to complete, and then if the block was cancelled, drop the result. If the line of code is very costly such that you really need to terminate it (rather than just ignore its result), then you will have to replace the library with code that checks a cancel flag.
This is not solvable. It doesn't matter which tool you use (GCD vs NSOperation). It is not possible to forcibly stop an operation in progress. It would leave memory in an unknown state. The solution is to wait for the line of code to complete, and then if the block was cancelled, drop the result. If the line of code is very costly such that you really need to terminate it (rather than just ignore its result), then you will have to replace the library with code that checks a cancel flag.
edited Jan 3 at 0:06
answered Jan 2 at 22:46
Rob NapierRob Napier
206k28305433
206k28305433
add a comment |
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%2f54013959%2fin-a-macos-application-i-have-a-background-thread-executing-with-one-single-lin%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
I hope you are not thinking of leaving
[NSThread sleepForTimeInterval:0.2]
in your shipping code. You should never sleep.– matt
Jan 2 at 22:30
1
The usual strategy is to break the work up into short bits and test for cancellation both before and after the line of code that does a bit. That way, you give yourself at least the best chance of intercepting the cancellation. That is what you would do in NSOperation as well.
– matt
Jan 2 at 22:31