calling pthread_self (on ubuntu), expensive?


 
Thread Tools Search this Thread
Top Forums Programming calling pthread_self (on ubuntu), expensive?
# 1  
Old 06-03-2010
calling pthread_self (on ubuntu), expensive?

Hi all,

Is anyone aware of what operations are involved when a call to pthread_self() is made, obtaining the unique thread ID on a Ubuntu system (or even any Linux flavour)?

Specifically, to retrieve the thread id, is there any locking required or atomic operations?

I'm building an application for multi-core systems (in C using pthreads) which needs to be scalable and I'm planning to call this function quite often, hence I need some idea how "expensive" it is to do so.

Any help much appreciated.

Thanks.
# 2  
Old 06-03-2010
The internals of this function are some pretty hairy inline assembly, differing wildly from architecture to architecture. I can't tell precisely what it's doing. But from the comments, it sure looks like they're trying hard to make it minimal(down to the instruction-level, even) and nonblocking.
Code:
/* Return the thread descriptor for the current thread.

   The contained asm must *not* be marked volatile since otherwise
   assignments like
        pthread_descr self = thread_self();
   do not get optimized away.  */
# define THREAD_SELF \
  ({ struct pthread *__self;                                                  \
     asm ("movq %%fs:%c1,%q0" : "=r" (__self)                                 \
          : "i" (offsetof (struct pthread, header.self)));                    \
     __self;})


Last edited by Corona688; 06-03-2010 at 07:46 PM..
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 06-03-2010
Corona beat me to it.

The only thing I can add - pthread_self is not atomic.
This User Gave Thanks to jim mcnamara For This Post:
# 4  
Old 06-03-2010
Thanks for your replay Corona688.

I found similar code in glibc (tls.h)...

Code:
/* Return the TCB address of the current thread.  */
# define THREAD_SELF                                   \
  ({ tcbhead_t *__tcb;                                  \
     __asm__ ("movl %%gs:%c1,%0" : "=r" (__tcb)                      \
          : "i" (offsetof (tcbhead_t, tcb)));                  \
     __tcb;})

Which I believe is accessing the Thread Control Block of the running thread which resides here in the gs register, specifically the offset containing the tcb address, I think.

This should result in a pretty quick, "inexpensive" and scalable operation then. Wondered if there was a chance that another thread could overwrite the register where this value is written before the calling thread retrieved it, (the volatile keyword isn't used) but I suppose this function would be useless if that was a possibility. This function must be thread safe, right?

Cheers,
# 5  
Old 06-04-2010
There's a different tls.h for every architecture.

Of course it's thread-safe, not much point having it otherwise. It may be using registers that it wouldn't have write-access to, as well; unlike the old linuxthreads, NPTL operates with pretty low-level support by the kernel. (not that NPTL runs as root -- more that things more fundamental than system calls exist in the kernel for it. On MIPS for example, it monopolizes one of only four special hardware registers a MIPS chip has.)
# 6  
Old 06-07-2010
I can't think of a good reason to call pthread_self a lot.
why?
I doubt it's atomic, why should it need to be, it's unique to the thread.
it's only used for detaching or scheduling.
i would start detached if need be. scheduling I haven't tried.
"keep it simple".
# 7  
Old 06-07-2010
"I can't think of a good reason to call pthread_self a lot.
why?"

It's a long story, but basically I need a way of accessing application specific "context" information I have stored which is different depending on which thread is running. Using pthread_self() provides a way of having a unique key to that information regardless of where within a program, the execution is. Because pthread_self doesn't require any arguments, I don't need to carry around a parameter everywhere with that key, hence the application doesn't even have to be aware of the mechanism.

Hopefully that makes sort-of sense.
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. Ubuntu

Re-install Ubuntu 14.04 from system with Ubuntu on it

I need to re-install ubuntu on a system with ubuntu 14.04 already installed. I have the cd but can not seem to boot from it or find the installer. Is there a way to re-install from the command line or how do I do a fresh re-install? Thank you :) ---------- Post updated at 10:13 AM... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. What is on Your Mind?

Very Expensive Running Shoes

You really should not need one third of the entire US budget to buy a pair of running shoes... even if they are name brand. What have these guys been smoking? It reminds me of the old joke... Customer: At those prices you aren't going to sell many shoes. Salesman: Ah, but all we need to do is... (4 Replies)
Discussion started by: Perderabo
4 Replies

3. Ubuntu

[UBUNTU] mount.nfs fails in Ubuntu / Works on Red Hat!!!

Gurus, I want log in locally to my Lucid (10.04) workstation and have my code saved over the network on my samba account At work, all developers have samba user ids and when we were running Red Hat, we went thru the following procedure to get setup. * open a shell session to NFS server... (2 Replies)
Discussion started by: alan
2 Replies

4. Ubuntu

Ubuntu / Ubuntu File Manager / Config

I am using Ubuntu 9.10 with Gnome 2.28. I use the default Nautilus File Manager to view / manage files. Is there a way to add icons or customize the icons that are above the location bar and below the menus? There is a bar that has icons for "Back" "Forward" "Parent" above the location bar. I... (6 Replies)
Discussion started by: drewk
6 Replies

5. UNIX for Dummies Questions & Answers

Would like to install x86 desktop Ubuntu over AMD64 Ubuntu server

My intention was to build a dual boot XP Pro 64 and Ubuntu media server. I had installed the AMD64 version of Ubuntu 8.10 server and thought that I would be able to install Apache server. I need a GUI to work in. I tried to boot and install Mythbuntu 32 bit 8.10, but my machine now won't recognize... (0 Replies)
Discussion started by: docflyboy
0 Replies

6. Programming

Which is more expensive ?

I have the following code snippet's. Which one among these would be more expensive ? #1 for (int fd = 0; fd <= 1024; ++fd) close(fd); #2 for (int fd = 0; fd <= 1024; fd += 8) { close(fd); close(fd+1); close(fd+2);... (6 Replies)
Discussion started by: vino
6 Replies
Login or Register to Ask a Question