Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5994543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:49:57+00:00 2026-05-22T23:49:57+00:00

I am a newbie to Objective-C. I’m currently working on threads. I have to

  • 0

I am a newbie to Objective-C. I’m currently working on threads.

I have to make a synchronous execution of threads. I’m using NSInvocationOperaion to spawn a thread.

I have two threads. I need to wait for the 1st thread to signal a event or the timeout.
Signalling a event can be done by NSConditionLock. How to signal a timeout. I could not use waitUntilDate method here as the timeout is not a fixed value.
Is there any way to do this?

EDITED

main.m
------
#import "PseudoSerialQueue.h"
#import "PseudoTask.h"

int main()
{
    PseudoSerialQueue* q = [[[PseudoSerialQueue alloc] init] autorelease];
    [q addTask:self selector:@selector(test0)];
    [q addTask:self selector:@selector(test1)];
    [q addTask:self selector:@selector(test2)];
    [q quit];
    return 0;
}

PseudoTask.h
-----------------

#import <Foundation/Foundation.h>


@interface PseudoTask : NSObject {

    id target_;
    SEL selector_;
    id queue_;

}

@property(nonatomic,readonly)id target;

-(id)initWithTarget:(id)target selector:(SEL)selector queue:(id)queue;
-(void)exec;

@end

PseudoTask.m
-----------------

#import "PseudoTask.h"


@implementation PseudoTask

@synthesize target = target_;

-(id)initWithTarget:(id)target selector:(SEL)selector queue:(id)queue
{
    self = [super init];
    if (self) {
        target_ = [target retain];
        selector_ = selector;
        queue_ = [queue retain];
    }
    return self;
}

-(void)exec
{
    [target_ performSelector:selector_];
}

-(void)dealloc
{
    [super dealloc];
    [target_ release];
    [queue_ release];
}

@end


PseudoSerialQueue.h
----------------------------

#import <Foundation/Foundation.h>
#import "PseudoTask.h"

@interface PseudoSerialQueue : NSObject {

    NSCondition* condition_;
    NSMutableArray* array_;
    NSThread* thread_;

}

-(void)addTask:(id)target selector:(SEL)selector;

@end

PseudoSerialQueue.m
----------------------------

#import "PseudoSerialQueue.h"

@implementation PseudoSerialQueue

-(id)init
{
    self = [super init];
    if (self) {
        array_ = [[NSMutableArray alloc]init];
        condition_ = [[NSCondition alloc]init];
        thread_ = [[NSThread alloc] initWithTarget:self selector:@selector(execQueue) object:nil];
        [thread_ start];
    }
    return self;
}

-(void)addTask:(id)target selector:(SEL)selector
{
    [condition_ lock];
    PseudoTask* task = [[PseudoTask alloc] initWithTarget:target selector:selector queue:self];
    [array_ addObject:task];
    [condition_ signal];
    [condition_ unlock];
}

-(void)quit
{
    [self addTask:nil selector:nil];
}

-(void)execQueue
{
    for(;;)
    {
        NSAutoreleasePool* pool = [[NSAutoreleasePool alloc]init];

        [condition_ lock];

        if (array_.count == 0) {
            [condition_ wait];
        }

        PseudoTask* task = [array_ objectAtIndex:0];
        [array_ removeObjectAtIndex:0];

        [condition_ unlock];

        if (!task.target) {
            [pool drain];
            break;
        }

        [task exec];
        [task release];

        [pool drain];
    }
}

-(void)dealloc
{
    [array_ release];
    [condition_ release];
    [super dealloc];
}

@end

I could not pass self from main.Hope i’m mistakenly calling it.
Error:’self’ undeclared is coming.

I could not understand
-(void)exec
{
[target_ performSelector:selector_];
}
in PseudoTask.m

target_ is not a method and its an ivar.
I am not getting any error or warning.But i could not understand that code.

I am writing what i have understood from your program.Please correct me if i my way of understanding the program is wrong.

The Thread execQueue is spawned when the PseudoSerialQueue is initialised and it waits for the signal from the addTask method.
The addTask method is called in the quit method and the parameters passed are nil.I could not understand why to pass a nil parameter.

It would be helpful if you explain about it.Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T23:49:58+00:00Added an answer on May 22, 2026 at 11:49 pm

    You mean NSCondition? You can use waitUntilDate: as relative time.

    [condition lock];
    // wait 5 seconds. 
    [condition waitUntilDate:[NSDate dateWithTimeIntervalSinceNow:5]];
    [condition unlock];
    

    EDITED:

    My PseudoSerialQueue class requires to be called from a class that is derived from NSObject like the following.

    @interface Test : NSObject
    @end
    
    @implementation Test
    - (void)test0
    {
    }
    
    - (void)test1
    {
    }
    
    - (id)init
    {
        self = [super init];
        return self;
    }
    
    - (void)exec
    {
        PseudoSerialQueue *q = [[PseudoSerialQueue alloc] init];
        [q addTask:self selector:@selector(test0)];
        [q addTask:self selector:@selector(test1)];
        [q addTask:self selector:@selector(test0)];
        [q quit];
    }
    @end
    

    You can call it from main function.

    Test *test = [[Test alloc] init];
    [test exec];
    

    I could not understand why to pass a nil parameter.

    I just only chose it for the message of quitting the loop in the PseudoSerialQueue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Newbie question... The objective: I intend to have an HTML text input field as
I'm a newbie doing Objective-C, coming from Flex/Actionscript development. I have an iPhone app
I came from objective-c and I am an Android newbie. I am using following
I am an Objective-C newbie, who still does not have a Mac, but still
Excuse my iPhone/Objective-C newbie status please! I've found my HTTP server using NSNetServiceBrowser, but
Newbie Objective C/Cocoa question: I have an application with some data entry fields and
(I am a newbie on Objective-C, and) I have been trying to pass a
I'm a Objective-C newbie and I'm currently trying to build an app which has
Hi have an newbie issue with objective-c, cocoa & iPhone. I've declared mapView in
REAL newbie here to Objective C and Cocoa. I have this 'if statement' if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.