[SERVER-60059] Add a mongo packaged_task Created: 17/Sep/21  Updated: 29/Oct/23  Resolved: 13/Jan/22

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: 5.3.0

Type: Improvement Priority: Major - P3
Reporter: George Wangensteen Assignee: George Wangensteen
Resolution: Fixed Votes: 1
Labels: servicearch-wfbf-day
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Fully Compatible
Sprint: Service Arch 2021-11-15, Service Arch 2021-11-22, Service Arch 2021-12-13, Service Arch 2022-1-10, Service Arch 2022-1-24
Participants:
Story Points: 4

 Description   

Sometimes, you have some code that will be run asynchronously at some point in the future. You want to know when that code is done running. Here's what you can do now:

auto task = makeMyTask(); // task is some Callable
auto fut = ExecutorFuture(_exec).then(task()); 
// fut will resolve once task is done running

But what if you want to have access to `fut` before you actually schedule task? For example:

class MyLittleService { 
public:
    SharedSemiFuture<void> isTaskDoneFuture( return _isDoneFuture)
    void start() { 
         _isDoneFuture = ExecutorFuture(_exec).then(_task().share());
     }
private:
    TaskType _task = makeMyTask();
    SharedSemiFuture<void> _isDoneFuture
}

But this code won't compile, because SharedSemiFuture has no default constructor. Instead, we would need to make _isDoneFuture an optional, and only give it a value when the task is actually scheduled. (Yuck!) What we want is a future that is readied when task is run, that can safely be created and used even before task is scheduled.

This is what packaged_task does :https://en.cppreference.com/w/cpp/thread/packaged_task

We could rewrite our above example as follows:

class MyLittleService { 
public:
 SharedSemiFuture<void> isTaskDoneFuture( return _task.getFuture())
 void start() { 
     ExecutorFuture(_exec).then(_task().share());
 }
private:
 PackegedTask _task = makePackagedTask(makeMyTask());
}

Futures extracted via _task.getFuture() will be readied when task has executed and contain the value returned by task (or exceptions it threw).

 



 Comments   
Comment by Githook User [ 13/Jan/22 ]

Author:

{'name': 'George Wangensteen', 'email': 'george.wangensteen@mongodb.com', 'username': 'gewa24'}

Message: SERVER-60059 Add a Mongo PackagedTask
Branch: master
https://github.com/mongodb/mongo/commit/7aa8af0297f66f827d093b31de311174236c859a

Generated at Thu Feb 08 05:48:51 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.