![]() |
| | #1 |
| Registered User Join Date: May 2007 Location: Berkeley, CA
Posts: 140
| A Question about Locks in Linux Threads 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*/
}
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 | |
| | #2 |
| Cat without Hat 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 | |
| | #3 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Note also that Code: pthread_rwlock_init() 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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |