| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | Mutex semaphores |
PC> a second thread calls this function and completes it before the first
PC> function has returned a value.
PC> ULONG someFn()
PC> {
PC> request.MutexSem();
PC> ULONG rc = 0;
PC> rc = something;
PC> str = somethingtoo; // this also needs protecting because str is
PC> // used in another function that is also
PC> // called by two or more threads.
PC> release.MutexSem();
PC> return rc; // A calling thread is pre-empted here before returning rc.
PC> // A second threads comes along and completes the
PC> // function before the first thread has finished,
PC> // thus causing rc to contain invalid data.
PC> // Is this possible?
No.
You only require semaphores to protect global and static data, not local
data. Since RC is a ULONG allocated on the stack, it will be only on the
thread's local stack. So when thread A calls someFn(), a ULONG will be
placed on thread A's stack. OS/2 pre-empts thread A, and thread B
continues, calling someFn(). This time the arguments say not to call
DosSleep(), so it gets all the way through - but this time the ULONG is on
thread B's stack - seperate from thread A's stack.
However, global and static data are stored in another place entirely, with
one instance being visible to both (all) threads. If there is any danger
of global data being written to by one thread while another is trying to
read it (or write), then you need a semaphore to protect it. (If everyone
just reads it, then no semaphore is required, obviously.)
If str is a global pointer, then yes, you need to protect it.
--- Maximus/2 3.01
* Origin: Tanktalus' Tower BBS (PVT) (1:342/708)SEEN-BY: 50/99 270/101 620/243 625/160 711/401 409 410 413 430 808 809 934 SEEN-BY: 711/955 712/407 515 624 628 713/317 800/1 @PATH: 342/5015 61 3615/50 396/1 270/101 712/515 711/808 934 |
|
| SOURCE: echomail via fidonet.ozzmosis.com | |
Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.