Skip to main content
edited title
Source Link
Anon
  • 3.6k
  • 3
  • 35
  • 52

If you run a function in a different thread, but you wait for it to finish in your main thread, is that function asyncrhonousasynchronous?

Backstory

Probably a stupid question, but I just have a sneaking suspicion that "asynchronous" is the wrong terminology to us for naming my template function here:

template <class T>
auto asynchronous( auto &&lambda )
{
    QEventLoop wait;
    QFutureWatcher<T> fw;
    fw.setFuture( QtConcurrent::run(lambda) );
    QObject::connect( &fw, &QFutureWatcher<T>::finished, &wait, &QEventLoop::quit );
    wait.exec();
    QObject::disconnect( &fw, &QFutureWatcher<T>::finished, &wait, &QEventLoop::quit );
    return fw.result();
}

in practice it looks like this:

int n(0);
ct_Start(n); // 0 - Running in main thread
n = asynchronous<int>( [&n](){
    // Running in background thread.
    // Mainthread is waiting for this lambda to finish
    // but mainthread is not locked. 
    // User Interface will still function.
    for ( int i = 0; i < 100000; i++ ){
        ct_Debug(n++); 
    };
    return n;
});
ct_Finish(n); // 100000

In short,

  • Main thread runs lambda on seperate thread
  • Waits for it to finish before continuing
  • Does not thread block, because it uses an event loop instead.

Question

So generally when you place a function on a seperate thread, allowing the main thread to continue, it is usually called asynchronous. However, while I am not blocking my main thread, it is still waiting for the lambda to finish, so I feel calling it asynchronous is misleading.

  1. Is naming this function "asynchronous" misleading?
  2. Is this example considered synchronous, asynchronous, both, or neither?
  3. Is there better more specific terminology that could be used instead?

Thanks.

If you run a function in a different thread, but you wait for it to finish in your main thread, is that function asyncrhonous?

Backstory

Probably a stupid question, but I just have a sneaking suspicion that "asynchronous" is the wrong terminology to us for naming my template function here:

template <class T>
auto asynchronous( auto &&lambda )
{
    QEventLoop wait;
    QFutureWatcher<T> fw;
    fw.setFuture( QtConcurrent::run(lambda) );
    QObject::connect( &fw, &QFutureWatcher<T>::finished, &wait, &QEventLoop::quit );
    wait.exec();
    QObject::disconnect( &fw, &QFutureWatcher<T>::finished, &wait, &QEventLoop::quit );
    return fw.result();
}

in practice it looks like this:

int n(0);
ct_Start(n); // 0 - Running in main thread
n = asynchronous<int>( [&n](){
    // Running in background thread.
    // Mainthread is waiting for this lambda to finish
    // but mainthread is not locked. 
    // User Interface will still function.
    for ( int i = 0; i < 100000; i++ ){
        ct_Debug(n++); 
    };
    return n;
});
ct_Finish(n); // 100000

In short,

  • Main thread runs lambda on seperate thread
  • Waits for it to finish before continuing
  • Does not thread block, because it uses an event loop instead.

Question

So generally when you place a function on a seperate thread, allowing the main thread to continue, it is usually called asynchronous. However, while I am not blocking my main thread, it is still waiting for the lambda to finish, so I feel calling it asynchronous is misleading.

  1. Is naming this function "asynchronous" misleading?
  2. Is this example considered synchronous, asynchronous, both, or neither?
  3. Is there better more specific terminology that could be used instead?

Thanks.

If you run a function in a different thread, but you wait for it to finish in your main thread, is that function asynchronous?

Backstory

Probably a stupid question, but I just have a sneaking suspicion that "asynchronous" is the wrong terminology to us for naming my template function here:

template <class T>
auto asynchronous( auto &&lambda )
{
    QEventLoop wait;
    QFutureWatcher<T> fw;
    fw.setFuture( QtConcurrent::run(lambda) );
    QObject::connect( &fw, &QFutureWatcher<T>::finished, &wait, &QEventLoop::quit );
    wait.exec();
    QObject::disconnect( &fw, &QFutureWatcher<T>::finished, &wait, &QEventLoop::quit );
    return fw.result();
}

in practice it looks like this:

int n(0);
ct_Start(n); // 0 - Running in main thread
n = asynchronous<int>( [&n](){
    // Running in background thread.
    // Mainthread is waiting for this lambda to finish
    // but mainthread is not locked. 
    // User Interface will still function.
    for ( int i = 0; i < 100000; i++ ){
        ct_Debug(n++); 
    };
    return n;
});
ct_Finish(n); // 100000

Question

So generally when you place a function on a seperate thread, allowing the main thread to continue, it is usually called asynchronous. However, while I am not blocking my main thread, it is still waiting for the lambda to finish.

  1. Is naming this function "asynchronous" misleading?
  2. Is this example considered synchronous, asynchronous, both, or neither?
  3. Is there better more specific terminology that could be used instead?

Thanks.

Source Link
Anon
  • 3.6k
  • 3
  • 35
  • 52

If you run a function in a different thread, but you wait for it to finish in your main thread, is that function asyncrhonous?

Backstory

Probably a stupid question, but I just have a sneaking suspicion that "asynchronous" is the wrong terminology to us for naming my template function here:

template <class T>
auto asynchronous( auto &&lambda )
{
    QEventLoop wait;
    QFutureWatcher<T> fw;
    fw.setFuture( QtConcurrent::run(lambda) );
    QObject::connect( &fw, &QFutureWatcher<T>::finished, &wait, &QEventLoop::quit );
    wait.exec();
    QObject::disconnect( &fw, &QFutureWatcher<T>::finished, &wait, &QEventLoop::quit );
    return fw.result();
}

in practice it looks like this:

int n(0);
ct_Start(n); // 0 - Running in main thread
n = asynchronous<int>( [&n](){
    // Running in background thread.
    // Mainthread is waiting for this lambda to finish
    // but mainthread is not locked. 
    // User Interface will still function.
    for ( int i = 0; i < 100000; i++ ){
        ct_Debug(n++); 
    };
    return n;
});
ct_Finish(n); // 100000

In short,

  • Main thread runs lambda on seperate thread
  • Waits for it to finish before continuing
  • Does not thread block, because it uses an event loop instead.

Question

So generally when you place a function on a seperate thread, allowing the main thread to continue, it is usually called asynchronous. However, while I am not blocking my main thread, it is still waiting for the lambda to finish, so I feel calling it asynchronous is misleading.

  1. Is naming this function "asynchronous" misleading?
  2. Is this example considered synchronous, asynchronous, both, or neither?
  3. Is there better more specific terminology that could be used instead?

Thanks.