ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

GCD Basic Task Submission

Objective-C

Goal -- WPM

Ready
Exercise Algorithm Area
1// Objective-C GCD Basic Task Submission
2
3#import <Foundation/Foundation.h>
4
5void submitBasicTasks() {
6NSLog(@"Main thread: Starting basic GCD task submission.");
7
8// Submit a task to the default global concurrent queue
9dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // Use a global concurrent queue
10@autoreleasepool { // Good practice for background tasks
11NSLog(@"Task 1: Executing on a background thread.");
12[NSThread sleepForTimeInterval:0.5]; // Simulate work
13NSLog(@"Task 1: Finished.");
14}
15});
16
17// Submit another task
18dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // Use the same global concurrent queue
19@autoreleasepool { // Good practice for background tasks
20NSLog(@"Task 2: Executing on a background thread.");
21[NSThread sleepForTimeInterval:0.3]; // Simulate work
22NSLog(@"Task 2: Finished.");
23}
24});
25
26// Submit a task to the main queue for UI updates
27dispatch_async(dispatch_get_main_queue(), ^{ // Use the main queue
28NSLog(@"Task 3: Executing on the main thread.");
29// This is where UI updates would typically happen.
30NSLog(@"Task 3: Finished.");
31});
32
33NSLog(@"Main thread: All tasks submitted. Execution order of tasks is not guaranteed.");
34}
35
36// Example usage:
37// int main(int argc, const char * argv[]) {
38// @autoreleasepool {
39// submitBasicTasks();
40// // Keep the main run loop alive to allow background tasks to complete.
41// [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:2.0]];
42// }
43// return 0;
44// }
Algorithm description viewbox

GCD Basic Task Submission

Algorithm description:

This scenario demonstrates the fundamental use of Grand Central Dispatch (GCD) for submitting asynchronous tasks in Objective-C. It shows how to dispatch blocks of code to both a global concurrent queue (for background work) and the main queue (for UI-related tasks). The focus is on understanding that `dispatch_async` does not block the calling thread and that the execution order of tasks on concurrent queues is not guaranteed. This is a foundational concept for any iOS or macOS developer needing to perform non-blocking operations.

Algorithm explanation:

The `submitBasicTasks` function illustrates basic GCD usage. `dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)` retrieves a default priority global concurrent queue, allowing multiple tasks to run in parallel. `dispatch_async` submits a block of code to this queue to be executed asynchronously. The `@autoreleasepool` within each background task block is essential for managing memory for objects created during the task's execution, preventing potential leaks. `dispatch_get_main_queue()` retrieves the serial queue associated with the application's main thread, which is crucial for updating the UI. Submitting a task to the main queue using `dispatch_async` ensures it will be executed on the main thread when it becomes available. The key takeaway is that `dispatch_async` returns immediately, and the submitted blocks execute at a later time, potentially on different threads. The order of execution for tasks submitted to a concurrent queue is not guaranteed, while tasks on a serial queue (like the main queue) are executed in the order they are submitted. Time complexity for submitting N tasks is O(N), as each submission is a constant-time operation. Space complexity is O(1) for the queue management itself.

Pseudocode:

1. Define a function.
2. Log a starting message on the main thread.
3. Submit Task 1 to a global concurrent queue using `dispatch_async`:
   a. Use an autorelease pool.
   b. Log start, simulate work, log finish.
4. Submit Task 2 to the same global concurrent queue using `dispatch_async`:
   a. Use an autorelease pool.
   b. Log start, simulate work, log finish.
5. Submit Task 3 to the main queue using `dispatch_async`:
   a. Log start, indicate UI context, log finish.
6. Log a message on the main thread indicating all tasks have been submitted.