Talk:POSIX Threads
From Wikipedia, the free encyclopedia
I thinks pthreads is a better name for this. --129.7.248.159 19:32, 19 Jan 2005 (UTC)
- pthread wins the Google test against pthreads, as does libpthread vs libpthreads. I always call it pthreads myself in programming discussion, but I think 'pthread' is a more appropriate entry. I'll make a pthreads article with a REDIRECT directive, though, as a lot of people do indeed also call it pthreads. --I am not good at running 02:09, 25 Apr 2005 (UTC)
-
- Typically you create an article at the singular and redirect the plural, but this really should be plural:
- Do you write a multi-threaded program with one thread? Nope.
- Intro: "pthread is an abbreviation for POSIX threads..." which naturally leads to pthreads not pthread since it's "POSIX threads".
- I've never cared for the google test, but if you restrict your search to "pthread -pthread.h" and "pthreads -pthread.h" you get 267,000 vs. 275,000, respectively. Cburnett 02:25, Apr 25, 2005 (UTC)
- Typically you create an article at the singular and redirect the plural, but this really should be plural:
Contents |
[edit] AFD result - keep
Robert 03:52, 15 January 2006 (UTC)
[edit] cleanup
I'm cleaning this up; renamed from pthreads to POSIX Threads per AFD. I'm removing the text below as unencyclopedic and probably what prompted the AFD. —Quarl (talk) 2006-01-15 13:13Z
[edit] Code example
pthreads might be used in the following manner to prevent two threads from accessing the same data structure simultaneously:
pthread_cond_t cond_var; pthread_mutex_t mutex_var; int *array; /*both threads will access and modify this array, so we don't want one reading while the other is writing*/ int valid = 1; /* used to test if the data in the array is valid*/ void init() { pthread_cond_init(&cond_var); pthread_mutex_init(&mutex_var); } void thread1() { pthread_mutex_lock(&mutex_var); if(!valid) {/*must check in case the thread is context switched before it gets the mutex*/ /*make the thread wait without blocking the processor*/ pthread_cond_wait(&cond_var, &mutex_var); } valid = 0; /*edit and read from array*/ valid = 1; pthread_mutex_unlock(&mutex_var); pthread_cond_signal(&cond_var); /*tell the other thread to start working*/ } void thread2() { pthread_mutex_lock(&mutex_var); if(!valid) {/*must check in case the thread is context switched before it gets the mutex*/ /*make the thread wait without blocking the processor*/ pthread_cond_wait(&cond_var, &mutex_var); } valid = 0; /*edit and read from array*/ valid = 1; pthread_mutex_unlock(&mutex_var); pthread_cond_signal(&cond_var); /*tell the other thread to start working*/ }
[edit] broken link
[[1]] seems to be boken... someone minds if I remove it or has replacement of it? --DMIax 22:15, 14 February 2006 (UTC)
This article seems more like a stub than an article proper, but I don't see why it should be deleted. Bluesprite 18:39, 5 April 2007 (UTC)
[edit] pthread_join()
Is there any sense to join second thread ? I guess, machine never reach this code as way as "return 0" (added to make compiler happy). 87.236.11.254 11:38, 19 December 2006 (UTC)
- The main thread will always reach pthread_join(). Pthread_join() waits until thread "b" calls pthread_exit() or returns from thread_func(). Then pthread_join() retrieves the value that was passed to pthread_exit() and returns to the main thread. Then the main thread exits. If thread "b" happens to finish first, it will hang around and wait for pthread_join() to be called. —Ryan 09:46, 8 June 2007 (UTC)
[edit] Pthreads vs. pthreads
There's a claim that "Pthreads is written with an upper-case P" without a citation. I maintain that "pthreads" is much more common than "Pthreads". Does anyone else agree? —75.0.180.18 07:34, 21 February 2007 (UTC)
- Agreed. Uppercasing pthread(s) is not an established way of writing it. If you try "man pthreads" on a Solaris machine, it will be referenced to as "POSIX threads" or "POSIX pthreads" throughout. 195.54.108.248 12:49, 6 November 2007 (UTC)
[edit] Ugly example
That example is definetly bad, the using cpu time thing should be avoided. For anyone that may want to compile it to try it, it may take either an eternity or occur so quick that it wont be noticeable at all. Even worst, some smart compiler may notice there isnt much in the for loop goin on and just optimize it by removing the loop altogether. Why not use some timing function instead? —Preceding unsigned comment added by 200.120.164.74 (talk) 21:16, 11 September 2007 (UTC)
- I've cleaned up the example code a bit. As it stands, the code should execute for 20 seconds, with up to a one second pause between each iteration. You're right about "while (time(NULL) == start_time)" eating CPU. You could avoid this by using Sleep(1000) in Windows or sleep(1) in Linux/BSD, but I think the point of the example was to keep things simple. --ozzmosis (talk) 17:53, 5 February 2008 (UTC)


