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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:23:24+00:00 2026-05-22T16:23:24+00:00

Im executing a thread that keeps looking for updates from a web site. It

  • 0

Im executing a thread that keeps looking for updates from a web site. It should be possible to set the refresh rate in some view.

The update thread keeps checking for a updated interval. But I would like to avoid race conditions. (Do I even have to worry for this with GCD?)

//This variable is used to avoid race conditions, refreshRate is a instance variable
int threadRefreshRate = refreshRate;
BOOL autoRefresh = YES;


dispatch_async(autoUpdateQueue, ^ { 
    while(YES){
        NSLog(@"Runs autoupdate thread");
        dispatch_async(dispatch_get_main_queue(), ^{

            if(autoRefresh){
                [self checkForUpdate];
                //Trying to set thread variable to avoid race condition
                threadRefreshRate = refreshRate;
            }
            else
                NSLog(@"Should not auto refresh");
        });
        sleep(threadRefreshRate);
    }

});

I tried to implement this code. However it doesn’t work to assing the ‘thread’-variable within a block.

  • 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-22T16:23:25+00:00Added an answer on May 22, 2026 at 4:23 pm

    For the kind of code you have given, i would use the timer events raised on the queue instead doing explicit sleep in the code. This way you dont have to worry about the race conditions etc..

    queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0);
    self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    if (!timer) return;
    dispatch_source_set_timer(timer, dispatch_walltime(NULL, 0), refreshRate * NSEC_PER_SEC, 5 * NSEC_PER_SEC);
    dispatch_source_t timer = self.timer;
    //initialize self to blockSelf with __block
    self.timerAction = ^{
    [blockSelf checkForUpdate];
    };
    
    dispatch_source_set_event_handler(timer, timerAction);
    dispatch_resume(timer);
    

    When the autoRefresh is set to NO, you can cancel it by

    dispatch_source_cancel(timer);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have about 4 threads. One thread keeps checking some data that the other
Without: ATL MFC Note: Client is executing in a different thread to that of
My program has two threads: Main execution thread that handles user input and queues
For some part of my project I need a process-local scheduling system that will
I have a basic queue of tasks executing (c# WinForms App that talks to
Ok.. here is some background on the issue. I have some 'critical' code that
I am conducting some throughput testing. My application has to read from JMS do
I need to create an method invoker that any thread (Thread B for example
*I'm using Java. I have this thread, agent, that explores a room to determine
suppose i want to start the execution of the thread at some specific time,at

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.