views:

50

answers:

3

How can one create a thread in unix programming?

What is difference between forking and threading?

Is threading more useful than forking?

+1  A: 
  1. pthread_create()

  2. Forking creates two processes, each having a separate thread of control. Creating a thread creates an extra thread of control within a single process.

  3. No - it is generally harder to get threaded applications right than it is to get separate processes right. And by quite a large margin.

Jonathan Leffler
+2  A: 

Forking creates a copy of the current process, while threads run in the same process and are normally used to calculate something in the background so the application does not appear to be frozen.

As for the usefulness of threads vs. forking, I would go with threads unless you have a specific need for a second process.

As for how to create a thread, I would recommend using the pthreads library. It will work on any UNIX operating system (Linux, BSD, Mac OS X), but is relatively low level. If you want something higher level, check out QThread from QT.

Mentalikryst
+3  A: 

One usually uses POSIX threads or some other technology wrapped by its API. Forking starts new processes, threading splits an existing process into pieces. Threading results in shared global state, which may or may not be useful given the specific circumstances.

Ignacio Vazquez-Abrams