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

  • SEARCH
  • Home
  • 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 8362255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:54:00+00:00 2026-06-09T11:54:00+00:00

I would like to implement branch and bound search in a multithreaded manner. In

  • 0

I would like to implement branch and bound search in a multithreaded manner. In particular, I want to use async to wrap the search calls at each branch, then just wait until some thread comes up with the answer, and just exit. (Ideally, I would want to cancel the other threads, but thread cancelling is not in the standard). Here is some simplified code :

#include <iostream>
#include <random>
#include <future>
#include <thread>

using namespace std;

mt19937 rng;
uniform_int_distribution<unsigned> random_binary(0, 1);

bool search() {
  return static_cast<bool>(random_binary(rng));
}

#define N 10000

int main()
{
  rng.seed(42);

  std::vector<future<bool>> tasks;

  for (unsigned i=0; i<N; ++i)
    tasks.push_back(async(launch::async, search));

  // Don't want to wait sequentially here.
  for (unsigned i=0; i<N; ++i) {
    tasks[i].wait();
    if (tasks[i].get()) {
      cout << "i = " << i << "\n";
      break;
    }
  }
  return 0;
}

search() is the search function. It returns true/false based on whether it found the answer or not. I return a random answer for illustration. But the crux of the problem is in the for loop that calls tasks[i].wait(). Right now, I am waiting sequentially for tasks to finish. Instead I want to do something like this :

auto x = wait_for_any(tasks.begin(), tasks.end());
x.get();
// cancel other threads.
// Profit?

What is a good way to achieve this?

  • 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-06-09T11:54:01+00:00Added an answer on June 9, 2026 at 11:54 am

    std::future provides a valid() function that lets you check if the result is available without blocking, so you can just use that, e.g. in a busy-wait loop:

    std::future<bool>* res_future = 0;
    for(size_t i = 0; ; i==tasks.size()?i=0:++i){
      // could add a timeout period to not completely busy-wait the CPU
      if(tasks[i].wait_for(std::chrono::seconds(0)) == std::future_status::ready){
        res = &tasks[i];
        break;
      }
    }
    
    bool res = res_future->get();
    

    A proposed addition to std::future, to make tasks like this easier, is a .then(func_obj) method that asynchronously calls the func_obj when the result is available, where you could set a flag or something.

    I sadly don’t know of a way to possibly implement wait_for_any in any other way than above. :/

    template<class FwdIt>
    std::future<bool> wait_for_any(FwdIt first, FwdIt last)
    {
      return std::async([=]{
        for(FwdIt cur(first); ; cur==last?cur=first:++cur){
        // could add a timeout period to not completely busy-wait the CPU
        if(cur->wait_for(std::chrono::seconds(0)) == std::future_status::ready)
          return cur->get();
      });
    }
    

    Thread destruction is usually done with cooperative cancellation.

    P. S.: std::future<T>::get() will automatically wait() if the result is not available.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to implement a distinct thread for each route in apache camel.I
I would like to implement a search bar with the exact same behavior as
I would like to implement the method User.calculate_hashed_password . I'm trying to use the
I would like to implement Factory Pattern in CSLA. I can use an abstract
I would like to implement a very simple way to store a variable containing
I would like to implement a function with R that removes repeated characters in
I would like to implement a simple queueing service specific to a project. Where
I would like to implement a switch button, android.widget.Switch (available from API v.14). <Switch
I would like to implement a simple AR desktop application. This application should first
I would like to implement something similar to 37Signals's Yellow Fade effect. I am

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.