Find the answer to your Linux question:
Results 1 to 5 of 5
How do you accomplish multithreading in C? Is the only good solution to use fork()? Thanks!...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Engineer Javasnob's Avatar
    Join Date
    Jul 2005
    Location
    Wisconsin
    Posts
    942

    Multithreading in C


    How do you accomplish multithreading in C? Is the only good solution to use fork()?

    Thanks!

  2. #2
    Linux User
    Join Date
    Aug 2005
    Location
    Italy
    Posts
    401

    libpthread

    You can use libpthreads shipped with glibc. You are able to create a multithreading application without using fork, wich double your process.
    When using Windows, have you ever told "Ehi... do your business?"
    Linux user #396597 (http://counter.li.org)

  3. #3
    Linux Enthusiast scientica's Avatar
    Join Date
    Sep 2003
    Location
    South- or "Mid-" Sweden
    Posts
    742
    adter a quick google I found this:
    http://pthreads.iqnaut.net/
    I'm goona book mark it for now and read it later - at first glance it looks like what you're looking for (at least it's enought for me to get started, and I wouldn't claim to be an über-geek when it comes to writing threaded code in linux (I've only written trheaded code for win32 in assembly sometime ago (in the "dark ages" ) ))
    Regards Scienitca (registered user #335819 - http://counter.li.org )
    --
    A master is nothing more than a student who knows something of which he can teach to other students.

  4. #4
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    Indeed, fork() does not create multiple threads, in the normal sense of the word. It is true that fork does create a new thread, but only as part of an entirely new (although duplicated) process, which means that neither the address space nor the file descriptors will be shared between the two processes.

    While you can use the clone() syscall directly to create threads, that would be rather stupid, since it would require you to manually set up stack spaces and all sorts of things for the new threads. The good (and portable to other Unix systems) way to create threads in Linux is, as burnit said, to use pthreads.

    They used to be documented in glibc's texinfo, but that section seems to be gone now, for some weird reason. Anyway, the manpages still exist. Check the manpages for pthread_create and pthread_mutex_init, and the manpages they refer to in the "SEE ALSO" section.

  5. #5
    Just Joined!
    Join Date
    Oct 2005
    Location
    Mass
    Posts
    52
    here's my two cents (cuz i have nothing better to do of course ^_^)

    The Linux philosophy towards threads is as such: threads are not meant to be light weight processes, instead they are simply a means in which multiple processes can communicate with each other through shared resources. In the end all the threading libraries end up calling clone() which splits the program in two according to the number of shared resources specified through it's parameters.

    If you'd like your program to be portable then use the p-threads library subroutines for creating threads, but if you don't care about portability then you can look up the clone() system call in the man pages

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •