Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Jan 26th, 2006, 10:21 AM   #1
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
Locking a thread

Hi,

I'm currently working on a program with multiple threads. All of the threads print to the screen, so the problem is that the messages are often mixed. Creating the function to print into this way:
int lock = 0;
void blah() {
 while(lock != 0);
 lock++;

 // code here

 lock--;
}

wouldn't work: when two threads are pending they will 50% of the time print at the same time.

My question is: what is the best way to make sure the messages won't be mixed? I was thinking of locking the thread in a safe way - but what is a safe way?

TIA,
Poly.
Polyphemus_ is offline   Reply With Quote
Old Jan 26th, 2006, 10:35 AM   #2
MBirchmeier
Hobbyist Programmer
 
Join Date: Oct 2005
Posts: 211
Rep Power: 3 MBirchmeier is on a distinguished road
I'm a big fan of mutexes

Simple, easy to use, OS level implementation.

(note: this might not be the best library, it's just what came in first in google for C++ mutex)

-MBirchmeier
MBirchmeier is offline   Reply With Quote
Old Jan 26th, 2006, 11:14 AM   #3
Symptom
Newbie
 
Symptom's Avatar
 
Join Date: Sep 2005
Posts: 28
Rep Power: 0 Symptom is on a distinguished road
Take a look at semaphores as well. It's a way to handle data control in multithreaded programs.

I don't know if you can use it in C++ but C has a data type that, access to it is atomic. It is defined by :
sig_atomic_t varname;
Atomic access means that reading/writing to the variable takes one single instruction, and therefore, your process cannot be interupted while in the middle of such a task. It can be used to access shared data or to implement manual locks for them.

If you are interested in the whole concept of multithreading and synchronizing processes, I know that the book of Tannenbaum about Operating Systems has some notes and algorithms which may help...

More info on sig_atomic_t can be found here.
__________________
The geeks shall inherit the earth.
Symptom is offline   Reply With Quote
Old Jan 26th, 2006, 11:35 AM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Note that many things (such as signals) are not portable between OSes. Polyphemus may or not may be interested, but his user info indicates that he uses Win and Linux.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jan 26th, 2006, 11:54 AM   #5
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
Quote:
Originally Posted by DaWei
Note that many things (such as signals) are not portable between OSes. Polyphemus may or not may be interested, but his user info indicates that he uses Win and Linux.
Yeah, I'm interested in creating it cross-platform, but some wrappers and #ifdefs will do .


Thanks guys, I'll check them out.

Quote:
Originally Posted by Symptom
If you are interested in the whole concept of multithreading and synchronizing processes, I know that the book of Tannenbaum about Operating Systems has some notes and algorithms which may help...
Thanks - I forgot about that, and that book is 3 meters away from my computer .
Polyphemus_ is offline   Reply With Quote
Old Jan 26th, 2006, 1:06 PM   #6
clsk
Newbie
 
Join Date: Jan 2006
Posts: 13
Rep Power: 0 clsk is on a distinguished road
Maybe what you're looking for are conditional variables. POSIX threads include this so it's postable as far as systems that are POSIX complaints.

You might want too look at:
http://users.actcom.co.il/~choo/lupg...thread_condvar

Maybe even read the whole article . This is what I used when I first started learning threads.
clsk is offline   Reply With Quote
Old Jan 26th, 2006, 2:33 PM   #7
Kaja Fumei
Hobbyist Programmer
 
Join Date: Oct 2005
Posts: 134
Rep Power: 3 Kaja Fumei is on a distinguished road
Quote:
Originally Posted by Polyphemus_
Yeah, I'm interested in creating it cross-platform, but some wrappers and #ifdefs will do .
I had to deal with the #ifdef and all that fun stuff a bunbch of time when I made multithreaded things. But I made one that supports a bunch of systems if you're interested. The file is here. It has threads, mutexes, semaphores, events (aka pthread condition variables), read-write locks, and a couple other useful things. Read the README to see how to build (it's easy). There's no real documentation yet, but if you want to use it and have questions about the API, feel free to let me know.

Quote:
Originally Posted by clsk
Maybe what you're looking for are conditional variables. POSIX threads include this so it's postable as far as systems that are POSIX complaints.

You might want too look at:
http://users.actcom.co.il/~choo/lup...#thread_condvar

Maybe even read the whole article . This is what I used when I first started learning threads.
Conditional variables (I call them events) would work if you signalled them one at a time. However, it would have to be pre-signalled once so the first one can get in, or else it'll be stuck waiting. I would just use a semaphore with an initial value of 1, but mutexes would work too.
Kaja Fumei 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




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

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