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 6747427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:26:27+00:00 2026-05-26T12:26:27+00:00

I am using dispatch_semaphore_wait to stop my current thread but it looks like it

  • 0

I am using dispatch_semaphore_wait to stop my current thread but it looks like it stops all my threads.

Code:

SampleReader *reader = [[SampleReader alloc] initWithHostname:hostname andFilePath:filepath];
reader.endHandler = endHandler;

[reader start];

dispatch_semaphore_wait(reader->mSem, DISPATCH_TIME_FOREVER);

My start method has something like:

mFileStream = [[NSOutputStream outputStreamToFileAtPath:[fileurl path] append:FALSE] retain];
[mFileStream open];

mNetworkStream = (NSInputStream *)CFReadStreamCreateWithFTPURL(NULL, (CFURLRef)ftpurl);
mNetworkStream.delegate = self;
[mNetworkStream scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
[mNetworkStream open];

I get callback in one of the delegate methods wherein I signal the semaphore

- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode{

  switch (eventCode)
  Case NSStreamEventErrorOccurred:
                    dispatch_semaphore_signal(mSem);
        break;
    case NSStreamEventEndEncountered:
        dispatch_semaphore_signal(mSem);
        break;

}

However, when i send wait on semaphore, the delegate method is not called.
Its called only when i comment out the line

 //dispatch_semaphore_signal(mSem);

Any help will be greatly appreciated.

  • 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-26T12:26:28+00:00Added an answer on May 26, 2026 at 12:26 pm

    If you’re calling [reader start] from your main thread then you are creating a deadlock. Your stream is being associated with the main thread here:

    [mNetworkStream scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
    

    That means for it to work the main run loop must be spinning. If the dispatch_semaphore_wait is on the main thread though, you’re stopping the run loop and preventing the stream from handling its events.

    I don’t see how commenting out the dispatch_semaphore_signal would do anything besides make the wait never return.

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

Sidebar

Related Questions

As i understood, the threads provided by GCD do have a runloop but no
I'm using Mate's RemoteObjectInvoker to call methods in my FluorineFX based API. However, all
I am using boost::thread_group to create(using thread_group::create_thread() ) and dispatch threads. In order to
I am testing some code that does asynchronous processing using Grand Central Dispatch. The
I am using django signals for data denormalization. Here is my code: # vote
Looking at the Time Profiler instrument in Instruments, it looks like I'm not disposing
When using glib to dispatch signals through emit , are all the listeners/handlers called
I'm testing some event dispatch code in a Flex app, using FlexUnit's addAsync method
I'm using the client-side javascript version of LESS to compile out less code, and
I'm creating a styled QTreeView using double-dispatch to resolve specific delegate for data items,

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.