calling pthread_self (on ubuntu), expensive?


 
Thread Tools Search this Thread
Top Forums Programming calling pthread_self (on ubuntu), expensive?
# 8  
Old 06-07-2010
You could just call it once and save the value.
# 9  
Old 06-08-2010
Quote:
Originally Posted by Corona688
You could just call it once and save the value.
No. That wouldn't work unfortunately. I'd have to save it in something that I would then need to tag along to every function as a parameter. I don't want to do that because I'm building 2 applications in layers, the application in the upper layer shouldn't know about the mechanism of the lower layer, it just calls its header functions.

---------- Post updated 08-06-10 at 02:17 PM ---------- Previous update was 07-06-10 at 10:04 PM ----------

Dunno if this is of interest to anyone but I found a way of retrieving information without resorting to pthread_self as I was describing in this thread...

Code:
pthread_key_t key; /* global key */
key_ret_code = pthread_key_create(&key, NULL); /* initialises the key */

This creates a global key which all threads can access. Then each thread can store a value with that key...(in the form of a void*)

Code:
void* value_data;
ret_code = pthread_setspecific(key, value_data);

Now any thread can call...

Code:
void* value_data = pthread_getspecific(key);

And get access to that data. Just what I was looking for!

Lots more in this useful document...

http://dlc.sun.com/pdf/816-5137/816-5137.pdf
# 10  
Old 06-08-2010
As I suspected it would, pthread_getspecific uses THREAD_SELF anyway -- and that's only in the best case. Worst case it has to plunge in and access other thread memory other ways. Stop trying to be so clever and just use pthread_self, that's what it's there for Smilie
# 11  
Old 06-08-2010
Quote:
Originally Posted by Corona688
As I suspected it would, pthread_getspecific uses THREAD_SELF anyway -- and that's only in the best case. Worst case it has to plunge in and access other thread memory other ways.
Thanks for the heads-up Corona688,

You might be right, but the thing is I had to store the "pthread_t" values in an array anyway, so in my application, after calling pthread_self() I would then always have to search the array for the corresponding "pthread_t" value. (At the moment this search is O(n) but I had planned to get it down to O(log n) using a binary-search algorithm if the number of threads is "high").

The good thing about "pthread_getspecific" is it returns the actual data I wanted, but as you pointed out, it looks a bit "involved" when it's not the best case. Maybe I'll try both approaches and see which is quicker.

Using "pthread_getspecific" might be better in avoiding a cache colision though, rather than threads accessing data in a global table.

Quote:
Stop trying to be so clever and just use pthread_self, that's what it's there for Smilie
Trying to be clever ain't so bad if I can manage to pull it off now and againSmilie
# 12  
Old 06-08-2010
Ah. If you're looking up data, then you're probably right. Helps to know your actual question. Smilie
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