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 6813229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:32:44+00:00 2026-05-26T20:32:44+00:00

I have got this problem: Find the first element in a list, for which

  • 0

I have got this problem:

  • Find the first element in a list, for which a given condition holds.

Unfortunately, the list is quite long (100.000 elements), and evaluation the condition for each element takes in total about 30 seconds using one single Thread.

Is there a way to cleanly parallelize this problem? I have looked through all the tbb patterns, but could not find any fitting.

UPDATE: for performance reason, I want to stop as early as possible when an item is found and stop processing the rest of the list. That’s why I believe I cannot use parallel_while or parallel_do.

  • 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-05-26T20:32:45+00:00Added an answer on May 26, 2026 at 8:32 pm

    ok, I have done it this way:

    1. Put all elements into a tbb::concurrent_bounded_queue<Element> elements.
    2. Create an empty tbb::concurrent_vector<Element> results.
    3. Create a boost::thread_group, and create several threads that run this logic:

    logic to run in parallel:

    Element e;
    while (results.empty() && elements.try_pop(e) {
        if (slow_and_painfull_check(e)) {
             results.push_back(e);
        }
    }
    

    So when the first element is found, all other threads will stop processing the next time they check results.empty().

    It is possible that two or more threads are working on an element for which slow_and_painfull_check returns true, so I just put the result into a vector and deal with this outside of the parallel loop.

    After all threads in the thread group have finished, I check all elements in the results and use the one that comes first.

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

Sidebar

Related Questions

The responses I've got to this question have solved the problem I had in
I have got this code: XDocument xdoc = XDocument.Load(URI); XElement root = xdoc.Element(forecast); //get
I have got an XML document which looks something like this. <Root> <Info>....</Info> <Info>....</Info>
I have this but I got an error: -- test if a list contains
We've got this very annoying problem with Scriptaculous and Internet Explorer 7/8. We have
I got this problem that I can't just solve algorithmically. Let's say i have
I must be the first to face this problem because I can't find even
I cannot find a solution with this problem. I have to join TWO MVC3
I have got this error when using Enterprise Library 3.1 May 2007 version. We
I have got this code and I am trying to understand the convention followed,

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.