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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:00:02+00:00 2026-05-23T13:00:02+00:00

I just started trying the new C++0x threading library, and I finally got several

  • 0

I just started trying the new C++0x threading library, and I finally got several computation tasks run in parallel. The example takes one second to run, which is what I expected, but it crashes on exit. Any idea of what I’m doing wrong?

#include <future>
#include <vector>
#include <iostream>

int main() {
    std::vector<std::thread> workingTasks;
    std::vector<std::future<int>> output;
    for (int i = 0; i < 6; ++i) {
        std::packaged_task<int()> pt(std::bind([](int data){ sleep(1); return data*data;}, i));
        output.push_back(pt.get_future());
        std::thread task(std::move(pt)); // launch task on a thread
        workingTasks.push_back(std::move(task));
    }

    for (int i = 0; i < output.size(); ++i) {
        std::cout << i << ": " << output[i].get() << std::endl;
    } 
}

The gdb backtrace is as follows:

Program received signal SIGABRT, Aborted. 0x0000003e45e330c5 in raise (sig=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:64 64        return INLINE_SYSCALL (tgkill, 3, pid, selftid, sig); 
(gdb) bt
#0  0x0000003e45e330c5 in raise (sig=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:64
#1  0x0000003e45e34a76 in abort () at abort.c:92
#2  0x0000003e496bc08d in __gnu_cxx::__verbose_terminate_handler ()
    at ../../../../libstdc++-v3/libsupc++/vterminate.cc:93
#3  0x0000003e496ba2a6 in __cxxabiv1::__terminate (handler=<value optimized out>)
    at ../../../../libstdc++-v3/libsupc++/eh_terminate.cc:39
#4  0x0000003e496ba2d3 in std::terminate () at ../../../../libstdc++-v3/libsupc++/eh_terminate.cc:49
#5  0x0000000000402d71 in std::thread::~thread (this=0x612d50,
__in_chrg=<value optimized out>)
    at /usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/thread:146
#6  0x0000000000407052 in std::_Destroy<std::thread> (__pointer=0x612d50)
    at /usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_construct.h:89
#7  0x0000000000406468 in std::_Destroy_aux<false>::__destroy<std::thread*> (__first=0x612d50, 
    __last=0x612d80)
    at /usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_construct.h:99
#8  0x00000000004053fd in std::_Destroy<std::thread*> (__first=0x612d50, __last=0x612d80)
    at /usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_construct.h:122
#9  0x0000000000404963 in std::_Destroy<std::thread*, std::thread> (__first=0x612d50,
__last=0x612d80)
    at /usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_construct.h:148
#10 0x0000000000403caa in std::vector<std::thread, std::allocator<std::thread> >::~vector (
    this=0x7fffffffddd0, __in_chrg=<value optimized out>)
    at /usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_vector.h:313
#11 0x0000000000401f9c in main () at main.cpp:18
  • 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-23T13:00:03+00:00Added an answer on May 23, 2026 at 1:00 pm

    I found the problem. std::thread is a really low-level primitive, and has to be join()’ed in order to work properly. Adding the following code after the output part fixed the crash:

    for (int i = 0; i < workingTasks.size(); ++i) {
        workingTasks[i].join();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm very new to Android development and just started to study. What I'm trying
I'm very new to python (just started yesterday), and I'm trying to create permutations
I just started trying out RavenDB and it seems that the new/easy way to
I'm new to using vectors...I just started studying them today. I'm trying to create
I´ve just started learning GWT and I´m trying to implement http://gwt.google.com/samples/Showcase/Showcase.html#!CwFileUpload and but failing
I have just started learning Erlang and am trying out some Project Euler problems
I just started with Spring Web MVC. I'm trying to avoid file extenstions in
I just started working using Google Maps API yesterday, and trying to set up
I just started learning Common Lisp a few days ago, and I'm trying to
I am trying to use this in my page class. I only just started

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.