Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 10th, 2007, 7:39 PM   #1
programmingnoob
Hobbyist Programmer
 
Join Date: Feb 2006
Posts: 155
Rep Power: 9 programmingnoob is on a distinguished road
posix threads

I will be coding a program in C using posix threads...
I already have a multiprocess version of this program ready, now I just have to write it the multithread version...
Here are questions...
1) Do mutex_lock(), mutex_trylock(), mutex_lock practice FIFO? If not, then is there any other synchronization method with posix threads that do practice FIFO

2) what about shared variables? My understanding is all you do is declare variables and initialize them BEFORE you create threads, and then it will be okay to use them. Right? I still probably need to synchronize them though, right?

3) what is the difference between mutex_lock() and mutex_trylock()? Is it equivalent to binary vs nonbinary semaphore.. or what?

4)Apart from shared variables and synchronization technique, what else needs to be different in my approach with thread version of my multiprocess program?
I will post more questions as they come up
programmingnoob is offline   Reply With Quote
Old Apr 11th, 2007, 1:02 AM   #2
programmingnoob
Hobbyist Programmer
 
Join Date: Feb 2006
Posts: 155
Rep Power: 9 programmingnoob is on a distinguished road
oops sorry I meant in C++
programmingnoob is offline   Reply With Quote
Old Apr 11th, 2007, 1:13 AM   #3
Kaja Fumei
Hobbyist Programmer
 
Join Date: Oct 2005
Posts: 134
Rep Power: 9 Kaja Fumei is on a distinguished road
1. i'm almost 100% certain that pthread mutexes aren't supposed to be FIFO. They may be on certain implementations on certain systems but i believe there is no official ordering defined by POSIX. Most likely its whatever thread gets awakened first by the scheduler to find the mutex unlocked gets it.

A FIFO lock is called a turnstile lock from what i've seen. Google it and you'll find some helpful stuff. There's a book online called "The Little Book of Semaphores" that talks about how to implement one using semaphores as well as lot of other helpful kinds of locks.

2. If a shared variable is only going to be read by various threads, then you don't need to lock (given that it was initialized before there were multiple threads). But if multiple threads can write to this shared variables as well, you're going to need to put a lock around it when reading and writing to it. (Actually you could make a read-write lock to allow multiple threads to read from it at the same time but one thread can write to it at time. The pthread library supports some functions for this on some systems called pthread_rwlock_*. But since you're asking for a turnstile, I dont think this will be what you want)

3. mutex_lock() causes the calling thread to wait until the lock is available and won't return until until it has the lock or is interrupted by an error. mutex_trylock() doesn't cause the thread to wait. If the lock is currently available, it immediately returns 0 and if another thread has the lock, it immediately returns the error code EBUSY.

Mutexes can be thought of as binary semaphores. I've seen discussions online bickering about whether or not they are the same because of how they are implemented but you can think as pretty much the same thing if it helps you.

4. Given we know nothing about what your program actually does, it's hard to say. But in general, you may want to lookup pthread's condition variables, read write locks, and UNIX's semaphore API to see if any of these would be useful in your program.
Kaja Fumei is offline   Reply With Quote
Old Apr 11th, 2007, 2:04 AM   #4
programmingnoob
Hobbyist Programmer
 
Join Date: Feb 2006
Posts: 155
Rep Power: 9 programmingnoob is on a distinguished road
Quote:
Originally Posted by Kaja Fumei View Post
1. i'm almost 100% certain that pthread mutexes aren't supposed to be FIFO. They may be on certain implementations on certain systems but i believe there is no official ordering defined by POSIX. Most likely its whatever thread gets awakened first by the scheduler to find the mutex unlocked gets it.

A FIFO lock is called a turnstile lock from what i've seen. Google it and you'll find some helpful stuff. There's a book online called "The Little Book of Semaphores" that talks about how to implement one using semaphores as well as lot of other helpful kinds of locks.

2. If a shared variable is only going to be read by various threads, then you don't need to lock (given that it was initialized before there were multiple threads). But if multiple threads can write to this shared variables as well, you're going to need to put a lock around it when reading and writing to it. (Actually you could make a read-write lock to allow multiple threads to read from it at the same time but one thread can write to it at time. The pthread library supports some functions for this on some systems called pthread_rwlock_*. But since you're asking for a turnstile, I dont think this will be what you want)

3. mutex_lock() causes the calling thread to wait until the lock is available and won't return until until it has the lock or is interrupted by an error. mutex_trylock() doesn't cause the thread to wait. If the lock is currently available, it immediately returns 0 and if another thread has the lock, it immediately returns the error code EBUSY.

Mutexes can be thought of as binary semaphores. I've seen discussions online bickering about whether or not they are the same because of how they are implemented but you can think as pretty much the same thing if it helps you.

4. Given we know nothing about what your program actually does, it's hard to say. But in general, you may want to lookup pthread's condition variables, read write locks, and UNIX's semaphore API to see if any of these would be useful in your program.
well, you see my IPC code is on fair version of reader and writer problem .... if that helps? I have used semaphores and shared memory to implement that
programmingnoob is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Auto-locking old threads Jimbo Community Announcements and Feedback 15 May 21st, 2006 9:31 AM
Edit time for threads nnxion Community Announcements and Feedback 16 Apr 1st, 2006 8:05 AM
Evaporating threads DaWei Coder's Corner Lounge 20 Nov 12th, 2005 9:26 AM
Unanswered Threads java_roshan Community Announcements and Feedback 1 Aug 7th, 2005 9:43 AM
Multiple server clients using sockets + Threads captainK Java 3 Mar 4th, 2005 10:10 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:00 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC