-2

I want to know As we all know how asynchronous task are necessary for concurrency but Wanted to know why we need the synchronous tasks. while we can achieve the same with the normal usage of function.

Thanks & regards

Rohit

user1099178
  • 83
  • 3
  • 6
  • 1
    Take a minute and read up on synchronous vs asynchronous. http://stackoverflow.com/questions/748175/asynchronous-vs-synchronous-execution-what-does-it-really-mean – Micah Wilson Jan 10 '17 at 05:35
  • As I have read but my question is different .... my question is when my instruction is being executed serially in main thread then why you need sync operation while same thing can be done in many ways.... – user1099178 Jan 10 '17 at 07:12
  • @michah It is not about synchronous vs asynchronous – user1099178 Jan 10 '17 at 07:13

3 Answers3

1
  • When you calls something synchronously, it means that 'the thread that initiated that operation will wait for the task to finish before continuing'. Asynchronous means that it will not wait for finish the task.
  • synchronous calls stops your current action and returns when the call returned. with asynchronous calls you can continue.
  • synchronous is the opposite of asynchronous code, and therefore is ordinary code.
  • At the end, if asynchronous is totally out of scope then you will not emphasize the word synchronous.
Suraj Sukale
  • 1,778
  • 1
  • 12
  • 19
0

It helps to synchronise threads, as the name suggests.

consider a typical usage of GCD async and sync (pseudo)

async background_thread {
    //1 call webservice or other long task that would block the main thread

    sync main_thread {
        //2 update UI with results from 1
    }

    //3 do something else that relies on 2
}

now if 2 was in an async and you needed to do something at 3 that relies on the updates at 2 to have happened, then you are not guaranteed (and most likely wont) get the behaviour you are expecting. instead, you use a sync to make sure that the task is completed before continuing the execution in the background thread.

If you are asking now, why not just take out the sync/async around 2 so it executes in order anyway? the problem is, the UI must not be updated on a background thread otherwise the behaviour is undefined (which usually means the UI lags a lot). So in essence what happens is the background thread waits at 2's sync until the main thread gets round to executing that block, then it will continue with the rest of the execution on the background thread.

If you were dealing with a task that doesnt require the main thread (or some other thread) to execute properly, then yes you may as well take out the sync at 2.

This is just one example of how a sync is useful, there are others if you are doing advanced threading in your app.

Hope this helps

Fonix
  • 11,447
  • 3
  • 45
  • 74
0

Typically it's because you want to do an operation on a specific different thread but you need the result of that operation. You cannot do the operation asynchronously because your code will proceed before the operation on the other thread completes.

Apple has a very nice example:

func asset() -> AVAsset? {
    var theAsset : AVAsset!
    self.assetQueue.sync {
        theAsset = self.getAssetInternal().copy() as! AVAsset
    }
    return theAsset
}

Any thread might call the asset method; but to avoid problems with shared data, we require that only functions that are executed from a particular queue (self.assetQueue) may touch an AVAsset, so when we call getAssetInternal we do it on self.assetQueue. But we also need the result returned by our call to getAssetInternal; hence the call to sync rather than async.

matt
  • 515,959
  • 87
  • 875
  • 1,141