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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:16:49+00:00 2026-06-02T22:16:49+00:00

I have a block of code where I am inside a pthread (lets call

  • 0

I have a block of code where I am inside a pthread (lets call this thread a), and I wish to spawn a new pthread (lets call this thread b). Thread b needs to be passed a deque and I have the following code:

void* process_thread_b(void* arg)
{              
  deque<string> *ptr = (deque<string>*)arg;
  cout << "Size -" << ptr->size() << endl;

  deque<string>::iterator it;
  for(it = ptr->begin(); it != ptr->end(); it++)
  {
    cout <<(*it) << endl;
  }
}

The above code is thread b's code. It is passed a deque and it prints out the size correctly. The moment I try printing out any of it’s elements, I get:

terminate called after throwing an instance of 'std::bad_alloc'
 what():  std::bad_alloc
Abort (core dumped)

When I am spawning the pthread, I use the below code…

 deque<string> myDeque;

 // Add strings to deque here...

 pthread_t dispatchCommands;
 pthread_create(&dispatchCommands, NULL, &process_thread_b, (void*)&myDeque);

The bottom code takes place in thread a. Why is it when I try to print out an element of the deque, I am getting an error, yet I can get it’s size?

  • 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-02T22:16:56+00:00Added an answer on June 2, 2026 at 10:16 pm

    pthread_create will return long before your thread function begins to execute. Your deque will have been destroyed long ago. You need to create it on the heap.

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

Sidebar

Related Questions

Let's say that I have a block of code like this: keysPressed = new
I have this block of code where when I hover (click for purposes of
If I have a block of code like this: def show @post = Post.find(params[:id])
Let's say i have this block of code, <div id=id1> This is some text
I have put a line of code inside the UIAnimation block. And the next
If I have code inside an ASP.NET MVC view that looks like this: <%
I'm trying to create a Rails app template I have this block of code
Lets say I have a 300X300px block called Statistics or Friends this block requires
I have a block of code inside a try catch block (c#). The block
We have html page, it has many code inside. Sometimes it has block #container

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.