If it walks like a duck, talks like a duck, but isn't covered by POSIX it probably is a penguin.

Time and again I tell all my friends who happen to develop for UNIX-like OSes to ditch the man pages and always read POSIX standard instead.

Case in point -- just a couple of days ago I had to make an internal library behave on Linux. The reason it did misbehave was because of the following code snippet:
   pthread_create(&id, NULL, do_stuff, NULL);
   workers[id].started = 1;
Of course, on sensible systems even the manpage for pthread_create(3) tells you that you shouldn't be expecting anything from id. It is, after all, an opaque datatype 'pthread_t'.

However, on Solaris it also has an additional property of being akin to PID. IOW -- an ever increasing integer counting your threads.

Now, don't get me wrong -- as the title suggests if it ain't covered by POSIX don't expect anything from it. However, the more I think about it the more I like the way Solaris does it. And here's why:

I believe its to be a good programming practice to always make it easier for consumer to enumerate objects coming out of producers like pthred_create. It is easy enough for me to do on Solaris, but on Linux I have to resort to keeping track of these objects myself -- I need a global counter, locks around it and so forth. On Solaris -- it's just an index.

Now, the reason they did it the other way on Linux (or better yet in glibc) seems to be that they wanted to simply return the memory address of the actual datastructure representing one particular thread. Nothing wrong with that except that small ever-increasing integers are kind of like addresses anyway, but they have an additional advantage of being limited to an "address space" of an array which lets them cross the border of different address spaces much easier. Without increasing a complexity on the consumer side. And that's a biggie in my opinion.

I guess I have to chalk one up for Solaris this time around.
Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

rvs

Search

Top Tags
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today