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.
- 12-24-2005 #1
Multithreading in C
- 12-25-2005 #2
- Join Date
- Aug 2005
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)
- 12-25-2005 #3
adter a quick google I found this:
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.
- 12-25-2005 #4
- Join Date
- Oct 2001
- Täby, Sweden
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.
- 12-26-2005 #5
- Join Date
- Oct 2005
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