nsthread

iOS Thread safety - Complete a block of code before thread switches

人走茶凉 提交于 2019-12-11 04:45:35
问题 I'm looking for a way to make sure that some lines of code get always executed together (before the system may switch the thread). @synchronized does not work for this as far as I know since it will only block another thread to come into this certain block. In my example I have different code parts that take influence on each other and I want to make sure that each of them blocks the other. How can this be achieved? EDIT: One Use Case could be that I'm playing back a midi file. The events go

Sleep or Pause NSThread

寵の児 提交于 2019-12-10 15:30:58
问题 I am creating a new Thread which runs one of my method after every certain period of time. Now what i am doing is as follows: NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(setUpTimerThread) object:nil]; [thread start]; the above method calls my setUpTimerThraed method, and this method makes an NSTimer and calls a specific method i.e syncData after every 1 minute. Now what i want is my sync Data method is sending some files to the FTP folder. and when ever it send

Pause Main thread until background thread retrieves user input

孤街浪徒 提交于 2019-12-10 11:45:19
问题 In my main starting thread I need to pause the code and start a new thread and wait until I get user input. Then Id like to discard the new thread made and go back to where the main thread left off. But whats happening is that the new thread is called but the main thread keeps going with the code. How do I deal with this without interfering with the user being able to use the interface buttons? I think that maybe another nscondition needs to be made in my if(moveCount == 2) statement? Or that

What's wrong with this dealloc in ARC?

若如初见. 提交于 2019-12-10 09:44:41
问题 I am working on an app that does image processing and displays the resulting image. Im using UIScrollView to let user scroll all images, because the image is not a standard jpg or png, it takes time to load. I use GCD to load asynchronously, when finished dispatch to main queue to display. the snippet is as follows: - (void)loadImage:(NSString *)name { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ UIImage *image = [Reader loadImage:name]; dispatch_sync

dispatch_semaphore_t reuse - What am I missing here?

二次信任 提交于 2019-12-10 03:37:57
问题 I have some code where I am using dispatch_semaphore_t to signal operation completion. When the semaphore is a member variable, it does not seem to behave correctly. I will show example code that works and an example that does not seem to work: @implementation someClass { dispatch_semaphore_t memberSem; dispatch_semaphore_t* semPtr; NSThread* worker; BOOL taskDone; } - (id)init { // Set up the worker thread and launch it - not shown here. memberSem= dispatch_semaphore_create(0); semPtr= NULL;

Risk Assessment: Using Pthreads (vs. GCD or NSThread)

一世执手 提交于 2019-12-09 11:31:06
问题 A colleague suggested recently that I use pthreads instead of GCD because it's, "way faster." I don't disagree that it's faster, but what's the risk with pthreads? My feeling is that they will ultimately not be anywhere nearly as idiot-proof as GCD (and my team of one is 50% idiots). Are pthreads hard to get right? 回答1: GCD and pthreads are both ways of doing work asynchronously, but they are significantly different. Most descriptions of GCD describe it in terms of threads and of thread

How to wait in NSThread until some event occur in iOS?

喜夏-厌秋 提交于 2019-12-09 09:20:15
问题 How to wait inside the NSThread until some event occur in iOS? eg, We created a NSThread and started a thread loop. Inside the thread loop, there is condition to check whether the message queue has any messages. If there is a message, then it will call the corresponding method to do some operation, else it should wait until the message queue gets populated with a new message. Is there any API or methods available to wait until some event occur? For Example NSThread *thread = [NSThread alloc].

how to stop nsthread

a 夏天 提交于 2019-12-09 01:21:31
问题 I am using a thread to update messages in background in my application. The thread is started in my messages class. Messages.m timerThread = [[NSThread alloc] initWithTarget:self selector:@selector(startTimerThread:) object:nil]; [timerThread start]; now I want the thread to stop when the user signout of the application. For that in the signout method (in another class) I added Messages *msg=[[Messages alloc]init]; [msg.timerThread cancel]; [msg release]; But even after signing out of the

Thread-safe NSMutableArray question

淺唱寂寞╮ 提交于 2019-12-08 13:46:11
问题 I am developing a RSS reader which uses a NSMutableArray ( _stories ) to store the contents of the RSS Feed. This array is being used by two different threads in the application and may be accessed simultaneously in two occasions, since: It is the data-source of the UITableViewController (which reads it's content and shows the desired information to the user) It is used by the XMLParser (which downloads the content from the Internet, parses the XML Data and adds the contents to it). Some

How to stop a NSThread in iphone? [duplicate]

筅森魡賤 提交于 2019-12-08 12:57:55
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: How to exit NSThread I have called a method using NSthread. I want to stop the thread on a click event of button. How can I achieve that? [NSThread detachNewThreadSelector:@selector(playVideo) toTarget:self withObject:nil]; 回答1: Try the the documentation for Threading, or better yet use NSOperation to perform the playVideo maintain a reference to the operation in your viewController and send a cancel message to