C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-04-2007, 06:33 PM   #1
Registered User
 
Join Date: May 2007
Location: Berkeley, CA
Posts: 140
A Question about Locks in Linux Threads

In a lot of the thread code, I see something like the following

Code:
int queue_int(struct queue *qp)
{
     int err;
     qp->head = NULL;
     qp->tail = NULL;

    err  = pthread_rwlock_init(&qp->qp->lock, NULL);

   /*more code*/

}
How come the the lock is placed AFTER head and tail are set to NULL? I
guess I don't understand the reason why you wouldn't place the lock
BEFORE setting head and tail to NULL.
Overworked_PhD is offline   Reply With Quote
Old 11-05-2007, 12:12 AM   #2
Cat without Hat
 
CornedBee's Avatar
 
Join Date: Apr 2003
Posts: 8,492
It's not a locking operation. It merely initializes the lock that is used later. Because this is an initialization operation, there is no possibility of another thread using the same data structure yet, so no need for synchronization.
__________________
All the buzzt!
CornedBee

"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
CornedBee is offline   Reply With Quote
Old 11-05-2007, 03:20 AM   #3
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
Note also that
Code:
pthread_rwlock_init()
is just setting up the lock, not locking anything. It needs to be done before any code can USE the lock.

So, as part of "creating" (or initializing) the queue, it's a good idea to create the lock. Should this be the FIRST ever thing, or some time after the first few lines? As long as no one tries to use the lock (or any other part of the queue, really) before the initialization is finished, it really doesn't matter. My suspicion is that you'll find that this initialization is done while the main thread is the only thread in the system, thus avoiding any races - that is of course just a guess.

--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Threads question tezcatlipooca C++ Programming 1 04-07-2007 05:10 AM
Question about Multiple threads and ring buffer. qingxing2005 C Programming 2 01-15-2007 12:30 AM
callbacks and threads - technical question Jumper Windows Programming 2 07-11-2004 12:09 AM
question from linux board ( not os dependant ) crypto C Programming 4 11-15-2002 02:09 AM
Question about LINUX River21 A Brief History of Cprogramming.com 0 09-17-2001 06:39 PM


All times are GMT -6. The time now is 05:09 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22