This link has been bookmarked by 35 people . It was first bookmarked on 26 Oct 2006, by WittyGuy.
-
17 May 12
-
24 Nov 10
-
Another advantage of using processes is that they can run on different machines, while all the threads have to run on the same machine (at least normally).
-
In order to compile a multi-threaded program using
gcc, we need to link it with the pthreads library. Assuming you have this library already installed on your system, here is how to compile our first program:
gcc pthread_create.c -o pthread_create -lpthread
Note that for some of the programs later on this tutorial, one may need to add a '-D_GNU_SOURCE' flag to this compile line, to get the source compiled. -
it is important to enclose all the code that accesses these variables in a small set of functions, and always use only these functions to access these variables.
-
This type of initialization creates a mutex called 'fast mutex'. This means that if a thread locks the mutex and then tries to lock it again, it'll get stuck - it will be in a deadlock.
-
recursive mutex
-
-
02 Oct 10
-
A Thread Group is a set of threads all executing inside the same process. They all share the same memory, and thus can access the same global variables, same heap memory, same set of file descriptors, etc. All these threads execute in parallel (i.e. using time slices, or if the system has several processors, then really in parallel).
-
The advantage of using a thread group over using a process group is that context switching between threads is much faster than context switching between processes
-
Another advantage of using processes is that they can run on different machines, while all the threads have to run on the same machine
-
pthread_create()gets 4 parameters. The first parameter is used bypthread_create()to supply the program with information about the thread. The second parameter is used to set some attributes for the new thread. In our case we supplied a NULL pointer to tellpthread_create()to use the default values. The third parameter is the name of the function that the thread will start executing. The forth parameter is an argument to pass to this function. Note the cast to a 'void*'. It is not required by ANSI-C syntax, but is placed here for clarification. -
The call to
pthread_exit()Causes the current thread to exit and free any thread-specific resources it is taking. There is no need to use this call at the end of the thread's top function, since when it returns, the thread would exit automatically anyway. This function is useful if we want to exit a thread in the middle of its execution. -
gcc pthread_create.c -o pthread_create -lpthread
-
- Atomicity - Locking a mutex is an atomic operation, meaning that the operating system (or threads library) assures you that if you locked a mutex, no other thread succeeded in locking this mutex at the same time.
- Singularity - If a thread managed to lock a mutex, it is assured that no other thread will be able to lock the thread until the original thread releases the lock.
- Non-Busy Wait - If a thread attempts to lock a thread that was locked by a second thread, the first thread will be suspended (and will not consume any CPU resources) until the lock is freed by the second thread. At this time, the first thread will wake up and continue execution, having the mutex locked by it.
A mutex is a lock that guarantees three things:
-
Note that a condition variable does not provide locking. Thus, a mutex is used along with the condition variable, to provide the necessary locking when accessing this condition variable.
-
-
13 Jan 09
-
03 Sep 08
-
10 Feb 08
-
02 Jan 08
-
09 Oct 07
-
03 Oct 07
-
16 May 07
-
11 Jan 07
-
19 Oct 06
-
28 Jul 06
-
05 Mar 06
-
02 Mar 06
-
27 Dec 05
-
23 Aug 05
-
26 Jan 05
-
04 Jan 05
Would you like to comment?
Join Diigo for a free account, or sign in if you are already a member.