Thread: Threads in POSIX, can I detach them from the main program?

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    121

    Threads in POSIX, can I detach them from the main program?

    Hello,
    I am wondering, can I detach a pthread from a main function? Let`s say: I have a simple UI interface, that terminates after a button click, but it starts a pthread which performs a logging in a file, but the destruction of the UI comes before the thread, and the thread must remain running in the background, like it was forked? The algorithm is simple - press button, destroy UI, init a background thread. Is it possible with pthreads and if - how to achieve it.
    P.S. pthread_detach() does not work that way.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    You can call pthread_exit() instead of returning from main(). This will allow detached threads to continue running after main() has called pthread_exit().

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MYSQL + POSIX Threads
    By laimaretto in forum Linux Programming
    Replies: 3
    Last Post: 11-28-2012, 10:08 AM
  2. A Qn on Posix threads
    By sangamesh in forum C Programming
    Replies: 3
    Last Post: 06-11-2011, 11:04 AM
  3. Posix Threads
    By tsiknas in forum Linux Programming
    Replies: 2
    Last Post: 11-28-2009, 04:07 AM
  4. POSIX threads
    By Boylett in forum Linux Programming
    Replies: 2
    Last Post: 09-10-2008, 01:33 PM
  5. POSIX-Threads
    By posixunil in forum Linux Programming
    Replies: 2
    Last Post: 04-17-2007, 06:43 AM

Tags for this Thread