SoFunction
Updated on 2025-04-12

IOS creates a concurrent thread instance detailed explanation

IOS creates a concurrent thread instance detailed explanation

Create concurrent threads

The main thread generally handles UI interface and user interaction. Other things usually need to be processed by another thread, such as downloading, computing, etc. . .
Now, we will simply create 3 threads and print out 1-1000 respectively. For convenience, thread 3 is placed in the main thread to execute.

- (void) firstCounter{  
@autoreleasepool {  
NSUInteger counter = 0;  
for (counter = 0;  
counter < 1000;  
counter++){  
NSLog(@"First Counter = %lu", (unsigned long)counter);  
}  
}  
}  
- (void) secondCounter{  
@autoreleasepool {  
NSUInteger counter = 0;  
 
for (counter = 0;  
counter < 1000;  
counter++){  
NSLog(@"Second Counter = %lu", (unsigned long)counter);  
}  
}  
}  

- (void) thirdCounter{  
NSUInteger counter = 0;  
for (counter = 0;  
counter < 1000;  
counter++){  
NSLog(@"Third Counter = %lu", (unsigned long)counter);  
}  
}  

- (void)viewDidLoad {  
[super viewDidLoad];  
[NSThread detachNewThreadSelector:@selector(firstCounter)  
toTarget:self  
withObject:nil];  
[NSThread detachNewThreadSelector:@selector(secondCounter)  
toTarget:self  
withObject:nil];  
/* Run this on the main thread */  
[self thirdCounter];  
}  

Since the thirdCounter function is not running in a separate thread, there is no need to automatically release the pool (autorelease pool). This method will run in the main thread of the application, and each Cocoa Touch program will be
Create an automatic release pool for the main thread.

At the end of the code, run the first counter and the second counter in a separate thread by calling detachNewThreadSelector. Now, if you run the program, you will see the following information in the console window:

 Second Counter = 921 
Third Counter = 301 
Second Counter = 922 
Second Counter = 923 
Second Counter = 924 
First Counter = 956 
Second Counter = 925 
Counter = 957 
Second Counter = 926 
First Counter = 958 
Third Counter = 302 
Second Counter = 927 
Third Counter = 303 
Second Counter = 928

It can be seen that these three timers run at the same time, and the contents they output are randomly alternating. Each thread must create an autorelease pool. Until the autorelease pool is released, the autorelease pool will always hold a reference to the object that is autoreleased. This is a very important mechanism in the reference counting memory management environment. For example, objects in Cocoa Touch can be autoreleased. Whenever an object instance is created, the reference count of the object is 1, but when the created autorelease pool object is released, the autorelease object will also send a release message. If its reference count is still 1, the object will be destroyed.

Each thread needs to create an autorelease pool as the first object to be created by the thread. If you don't do this, if you don't do this, when the thread exits, you allocate objects in the thread, memory leaks will occur. For a better understanding, let's take a look at the following code:

- (void) autoreleaseThread:(id)paramSender{  
NSBundle *mainBundle = [NSBundle mainBundle];  
NSString *filePath = [mainBundle pathForResource:@"AnImage"  
ofType:@"png"];  
UIImage *image = [UIImage imageWithContentsOfFile:filePath];  
/* Do something with the image */  
NSLog(@"Image = %@", image);  
}  
- (void)viewDidLoad {  
[super viewDidLoad];  
[NSThread detachNewThreadSelector:@selector(autoreleaseThread:)  
toTarget:self  
withObject:self];  
}  
If you run this code,,You will see this output in the console window:
*** __NSAutoreleaseNoPool(): Object 0x5b2c990 of 
class NSCFString autoreleased with no pool in place - just leaking 
*** __NSAutoreleaseNoPool(): Object 0x5b2ca30 of 
class NSPathStore2 autoreleased with no pool in place - just leaking 
*** __NSAutoreleaseNoPool(): Object 0x5b205c0 of 
class NSPathStore2 autoreleased with no pool in place - just leaking 
*** __NSAutoreleaseNoPool(): Object 0x5b2d650 of 
class UIImage autoreleased with no pool in place - just leaking

The above information shows that the UIImage instance of autorelease we created has caused a memory leak, and FilePath and other objects have also been leaked. This is because in our thread, an autorelease pool is not created and initialized at the beginning. Here is the correct code, you can test it to make sure it has no memory leaks:

- (void) autoreleaseThread:(id)paramSender{  
@autoreleasepool {  
NSBundle *mainBundle = [NSBundle mainBundle];  
NSString *filePath = [mainBundle pathForResource:@"AnImage"  
ofType:@"png"];  
UIImage *image = [UIImage imageWithContentsOfFile:filePath];  
/* Do something with the image */  
NSLog(@"Image = %@", image);  
}  
}  

The above examples of using IOS concurrent threads. If you have any questions, you can leave a message to discuss and make progress together. Thank you for reading. I hope it can help you. Thank you for your support for this site!