Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thread safety. #1

Closed
SmileGobo opened this issue Apr 11, 2024 · 1 comment
Closed

Thread safety. #1

SmileGobo opened this issue Apr 11, 2024 · 1 comment

Comments

@SmileGobo
Copy link

SmileGobo commented Apr 11, 2024

The implementation has no mutexes. When calling resolve or reject, you must ensure that either then|fail will be called before or after.
Or was a different usage strategy intended?
I have synchronization failure by assertion

assert((mSharedObj->mResolved == kNotResolved));

promise::Promise<int> loadDataFromSomeThread() {
    
     promise::Promise<int> pms;
     std::thread t{[pms](){
           //do some ....
     }};
     t.deatach();
     return pms;
}
int main() {
     std::promise<int> pms;     //it's waitable barier 
     auto f = pms.get_future();
     
     loadDataFromSomeThread()
         .then([&pms](int val){
             pms.set_value(val);
         })
        .fail([&pms](const promise::Error& err) { // There is a synchronization failure at this point
              pms.set_exception(new std::runtimer_error{err.msg()})
         )};
     std::cout << f.get() << std::endl;
     return 0;
}
@alxvasilev
Copy link
Owner

This promise implementation is not intended for threading - the idea is to use it in a single-threaded cooperative multitasking environment, similar to what javascript is. For inter-thread synchronization and communication, the standard C++ library already has facilities like std::promise and std::future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants