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

  • Home
  • SEARCH
  • 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 6003277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:05:55+00:00 2026-05-23T01:05:55+00:00

I am new to Objective C programming. I have created two threads called add

  • 0

I am new to Objective C programming.

I have created two threads called add and display using the NSInvocationOperation and added it on to the NSOperationQueue.

I make the display thread to run first and then run the add thread. The display thread after printing the “Welcome to display” has to wait for the results to print from the add method.

So i have set the waitUntilFinished method.

Both the Operations are on the same queue. If i use waitUntilFinished for operations on the same queue there may be a situation for deadlock to happen(from apples developer documentation). Is it so?

To wait for particular time interval there is a method called waitUntilDate:
But if i need to like this wait(min(100,dmax)); let dmax = 20; How to do i wait for these conditions?

It would be much helpful if anyone can explain with an example.

EDITED:

threadss.h
------------

#import <Foundation/Foundation.h>


@interface threadss : NSObject {

    BOOL m_bRunThread;
    int a,b,c;
    NSOperationQueue* queue;
    NSInvocationOperation* operation;
    NSInvocationOperation* operation1;
    NSConditionLock* theConditionLock;

}
-(void)Thread;
-(void)add;
-(void)display;
@end

threadss.m
------------

#import "threadss.h"

@implementation threadss

-(id)init
{
    if (self = [super init]) {
     queue = [[NSOperationQueue alloc]init];
     operation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(display) object:nil];
     operation1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(add) object:nil];
     theConditionLock = [[NSConditionLock alloc]init];
    }
    return self;
}
-(void)Thread
{
    m_bRunThread = YES;

    //[operation addDependency:operation1];
    if (m_bRunThread) {
    [queue addOperation:operation];
    }
    //[operation addDependency:operation1];
    [queue addOperation:operation1];

    //[self performSelectorOnMainThread:@selector(display) withObject:nil waitUntilDone:YES];
    //NSLog(@"I'm going to do the asynchronous communication btwn the threads!!");
    //[self add];
    //[operation addDependency:self];
    sleep(1);
    [queue release];
    [operation release];
    //[operation1 release];
}
-(void)add
{
    NSLog(@"Going to add a and b!!");
    a=1;
    b=2;
    c = a + b;
    NSLog(@"Finished adding!!");
}
-(void)display
{
    NSLog(@"Into the display method");
    [operation1 waitUntilFinished];
    NSLog(@"The Result is:%d",c);

}
@end


main.m
-------
#import <Foundation/Foundation.h>
#import "threadss.h"

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    threadss* thread = [[threadss alloc]init];
    [thread Thread];

    [pool drain];
    return 0;
}

This is what i have tried with a sample program.

output

2011-06-03 19:40:47.898 threads_NSOperationQueue[3812:1503] Going to add a and b!!
2011-06-03 19:40:47.898 threads_NSOperationQueue[3812:1303] Into the display method
2011-06-03 19:40:47.902 threads_NSOperationQueue[3812:1503] Finished adding!!
2011-06-03 19:40:47.904 threads_NSOperationQueue[3812:1303] The Result is:3

Is the way of invoking the thread is correct.

1.Will there be any deadlock condition?

2.How to do wait(min(100,dmax)) where dmax = 50.

  • 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-23T01:05:56+00:00Added an answer on May 23, 2026 at 1:05 am

    Assuming I’m understanding your question correctly, you have two operations:

    1. Operation A: prints a message, waits for operation B to finish, continues
    2. Operation B: prints a message

    If this is the case, can you just print the first message, start operation B, then start operation A?

    Also, when you are using NSOperationQueue you don’t directly manage threads, it does all the thread management for you. So in your question when you said ‘thread’ you actually meant to say ‘operation’.


    To directly answer your question, “Can this cause a deadlock”, yes it could. If you change the queue to be sequential instead of concurrent or if you make operation 2 dependent on operation 1 you will probably lock up. I’d recommend not trying to do what you’re doing, refactor your code so that one operation doesn’t need to pause while the other one works. Based on the code you’ve posted, there’s no reason to structure your code like that.

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

Sidebar

Related Questions

Hey there, I'm a designer thats really new to programming with xcode or Objective-C
Hey Guys .. I am new to programming in Objective C . I checked
Hey there, I'm sort of new to Objective-C, and well, programming in general. I
I am fairly new to programming and I am working with Objective C. I
I`m really new in Objective-C and Mac OSX programming. Can someone give me simple
I'm new to objective-C programming for iOS. I'm struggling with a really simple task,
I'm really new to programming in Objective-C, my background is in labview which is
Hey, I'm new to Objective-C and as well as programming with Cocoa at all..
I'm new to Objective-C programming so sorry for the lame question. I'm trying to
I'm new to objective-c and programming in general. I'm a paramedic and I've decided

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.