Talk:POSIX Threads/Archive 1

Latest comment: 13 years ago by DevSolar in topic Forum discussion

Renaming

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:
  1. Do you write a multi-threaded program with one thread? Nope.
  2. Intro: "pthread is an abbreviation for POSIX threads..." which naturally leads to pthreads not pthread since it's "POSIX threads".
  3. 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)
The actual file is pthread.h, though 81.110.208.171 (talk) 16:36, 28 September 2009 (UTC)

AFD result - keep

Robert 03:52, 15 January 2006 (UTC)


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

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*/
}

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)


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)

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)

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)
sleep is the way to go. the while loop has to potential to mislead novice coders and is just plain bad form as threads are really about cooperative execution. meaning threads should suspend execution when they're not doing anything. unistd.h is a standard POSIX header too. (216.246.230.12 (talk) 18:03, 21 August 2009 (UTC))

Citation

Whoever, put citation is needed for the following, you obviously don't understand how programming works. Object that is written with casing consideration is present in almost every single type of programming language (except in low level programming & Assembly Language). Every single client/server side programming & OOP related programming textbook generally will explain, if they don't usually in the beginngin of book their will be an intro of what you should know before reading (aka expectation).

(Note: in text, Pthreads is written with an upper-case P.)

--Ramu50 (talk) 01:57, 14 July 2008 (UTC)

C Example

I realize the example C code is for sake of example, but if all the code wants is to wait 1 second, why not sleep instead of calling the silly wait_thread() function? There is no need for it to just chew cpu cycles, why not allow other threads and processes do something? Isn't that the point of using threads in the first place? I'll update the code example. If people aren't happy, feel free to revert. And unistd.h is a standard POSIX header. (216.246.230.12 (talk) 17:57, 21 August 2009 (UTC))

Where is the standard?

In which document is POSIX Threads defined? Searching for "POSIX Threads" on Google Scholar gives a few 90s publications mentioning POSIX 1003.4a and POSIX 1003.4. But having a look at POSIX' IEEE website, I cannot find a single source about these specifications. --Abdull (talk) 19:48, 12 January 2010 (UTC)

From IEEE: IEEE 1003.1-2008 - Standard for Information Technology – Portable Operating System Interface (POSIX®) Base Specifications, Issue 7:
"POSIX.1-2008 is simultaneously IEEE Std 1003.1™-2008 and The Open Group Technical Standard Base Specifications, Issue 7."
Further:
"POSIX.1c – Although this term is not used in the normative text of POSIX.1-2008, it is used in this volume to refer to the POSIX Threads Extension amendment. (This was earlier referred to as POSIX.4a during the standard development process.)"
POSIX.1-2008 then is organized in several "components", each with its own "volume". It is within these volumes that all things POSIX Threads is defined.
--Abdull (talk) 22:09, 12 January 2010 (UTC)

Other systems

Both HP-UX and AIX support pthreads. Any reason why they're not listed here? -- Dougher (talk) 05:28, 3 February 2010 (UTC)

Linux extensions

It's worth to note about Linux extensions to pthreads - I mean all these pthread_*_np functions listed on http://www.kernel.org/doc/man-pages/online/dir_section_3.html Wojciech mula (talk) 09:58, 6 March 2010 (UTC)

Wikipedia is not a manpage and it's not the pthread documentation

Please clean this up, someone. Remove the enumerations and the functions and the source code. Who would think it would be a good idea to simply dump them on here, anyway? The code is also personal research and should be either removed or sourced. I sent a friend to this page to read up on what pthreads are (it was on one of his midterms) and by the looks of it, it did him a disservice since he's more clueless than ever.68.101.112.135 (talk) 01:07, 28 April 2010 (UTC)

Forum discussion

The [forum discussion] linked as reference is about libusb, and touches pthread for 64bit Windows only as an aside. Bad, IMHO. -- DevSolar (talk) 07:06, 8 September 2010 (UTC)