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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:00:26+00:00 2026-05-24T02:00:26+00:00

I am programming with C and pthreads. I have a long running function which

  • 0

I am programming with C and pthreads. I have a long running function which I want to run in a seperate thread:

void long_running_function(void * arg) {  
  ...
}


void start_long_running_function(void * arg) {  
  pthread_t thread;  
  pthread_create( &thread , NULL , long_running_function , arg);
  /* What about the thread variable? */  
}  

When leaving the start_long_running_function() function the local variable ‘thread’ will go out of scope. Is this OK – or can I risk problems e.g. when the long_running_function() is complete?

I have tried the approach illustrated in my code, and it seems to work – but maybe that is only luck?

Regards Joakim

  • 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-24T02:00:27+00:00Added an answer on May 24, 2026 at 2:00 am

    Yes — it’s safe to let the variable go out of scope. But remember you have to at some point do one of two things:

    1) pthread_detach() it so the kernel will free some kind of stuff that’s associated with it.

    2) pthread_join() it which has as a side-effect detaching it.

    If you don’t, I think this will be a resource leak.

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

Sidebar

Related Questions

I have a thread datatype in the interpreter implementation for a programming language I
I am working on a network programming and I have created a thread pool.
I am working on a network programming and I have this code void WorkHandler::workLoop(){
In the C programming language and Pthreads as the threading library; do variables/structures that
Programming has come a long way. I am still relatively young (first Computer: C64),
I'm programming an application(client/server) in C++ for linux using epoll y pthreads but I
(programming in iPhone objective-C) I have a class level NSString* that I create, add
While programming I have come to an unusual error. When I initialize an integer
When programming my app, I get to the point where I have to update
When programming in C++03, we can't pass an unnamed temporary T() to a function

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.