473,670 Members | 2,724 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Mutex/Lock

hello group,

is there a chance for other functions to get the lock if i have
following loop:

while (running)
{
Lock local(mutex);
}

if not, is it enough to change scope:

while (running)
{
{
Lock local(mutex);
}
}

thanx & hand, chris
Jun 27 '08 #1
45 4400
Chris Forone wrote:
hello group,

is there a chance for other functions to get the lock if i have
following loop:

while (running)
{
Lock local(mutex);
}
yes - that code is the same as

while (running) {
mutex.lock();
mutex.unlock();
}
>
if not, is it enough to change scope:

while (running)
{
{
Lock local(mutex);
}
}
Makes no difference.
Jun 27 '08 #2
Chris Forone wrote:
hello group,

is there a chance for other functions to get the lock if i have
following loop:

while (running)
{
Lock local(mutex);
}

if not, is it enough to change scope:

while (running)
{
{
Lock local(mutex);
}
}
You probably want:

{
Lock local(mutex);
while(running)
{
...
}
}
Jun 27 '08 #3

"Paul Brettschneider" <pa************ *****@yahoo.frw rote in message
news:92******** *************** ***@news.chello .at...
Chris Forone wrote:
>hello group,

is there a chance for other functions to get the lock if i have
following loop:

while (running)
{
Lock local(mutex);
}

if not, is it enough to change scope:

while (running)
{
{
Lock local(mutex);
}
}

You probably want:

{
Lock local(mutex);
while(running)
{
...
}
}
Why?

Jun 27 '08 #4
"Gianni Mariani" <gi*******@mari ani.wswrote in message
news:48******** @news.mel.dft.c om.au...
Chris Forone wrote:
>hello group,

is there a chance for other functions to get the lock if i have following
loop:

while (running)
{
Lock local(mutex);
}

yes - that code is the same as

while (running) {
mutex.lock();
mutex.unlock();
}
You mean that code is the same as:
while (running) {
try {
mutex.lock();
} catch (...) {
mutex.unlock();
throw;
}
mutex.unlock();
}
Right?
[...]
Jun 27 '08 #5
Paul Brettschneider schrieb:
Chris Forone wrote:
>hello group,

is there a chance for other functions to get the lock if i have
following loop:

while (running)
{
Lock local(mutex);
}

if not, is it enough to change scope:

while (running)
{
{
Lock local(mutex);
}
}

You probably want:

{
Lock local(mutex);
while(running)
{
...
}
}
No, the first version is the one i need. its to suspend/resume in a well
defined position in code. So i had:

bool Resume()
{
Lock local(mutex);
// resume thread
}

bool Suspend()
{
Lock local(mutex);
// suspend thread
}

bool Exit()
{
Lock local(mutex);
// here was my dl, because of wait for thread termination :-)
}

void Proc()
{
while (active)
{
Lock local(mutex);
// do things
}
}

now my Exit looks:

bool Exit()
{
mutex.Acquire() ;

if (active)
{
active = false, mutex.Release() ;
// wait for thread termination :-)))
}

mutex.Release() ;
}

it functs, hope it is waterproof...

thanks & hand, chris

ps: Ideas for better doing it are very welcome!
Jun 27 '08 #6
Errata:

bool Exit()
{
mutex.Acquire() ;

if (active)
{
active = false;
mutex.Release() ;
// wait for thread termination :-)))
}

mutex.Release() ;
}

if i write:

active = false, mutex.Release()

i think, the mutex is first released -error

is this right?

thanx & hand, chris
Jun 27 '08 #7
Chris Forone wrote:
Paul Brettschneider schrieb:
>Chris Forone wrote:
>>hello group,

is there a chance for other functions to get the lock if i have
following loop:

while (running)
{
Lock local(mutex);
}

if not, is it enough to change scope:

while (running)
{
{
Lock local(mutex);
}
}

You probably want:

{
Lock local(mutex);
while(running)
{
...
}
}

No, the first version is the one i need. its to suspend/resume in a well
defined position in code. So i had:
Ah. I misread your question. I think what you want is a notify/signal
mechanism, which should be implemented for every thread package. (In
pthreads it's the pthread_cond_* functions.)
Jun 27 '08 #8
On May 31, 10:39 am, Chris Forone <4...@gmx.atwro te:
Errata:
bool Exit()
{
mutex.Acquire() ;
if (active)
{
active = false;
mutex.Release() ;
// wait for thread termination :-)))
}
mutex.Release() ;
}
if i write:
active = false, mutex.Release()
i think, the mutex is first released -error
is this right?
No. There's a sequence point at the comma, so all side effects
of the preceding expression must be finished before any side
effects of the following occur.

I do wonder about your code, however. You really need to
recover the mutex before the end of the if---otherwise, you'll
release it twice. And how do you wait for thread termination:
with a join, or with some sort of global thread counter? In the
latter case, you'll need the mutex to read it as well.

I generally use something like:

void
requestTerminat ion()
{
ScopedLock lock( terminateFlagMu tex ) ;
terminateReques ted = true ;
}

void
waitForAllThrea dsToTerminate()
{
ScopedLock lock( threadCountMute x ) ;
while ( threadCount != 0 ) {
threadCountCond ition.wait( lock ) ;
}
}

void
terminate()
{
requestTerminat ion() ;
waitForAllThrea dsToTerminate() ;
}

with:

void
endOfThread()
{
ScopedLock lock( threadCountMute x ) ;
-- threadCount ;
threadCountCond ition.notify() ;
}

at the end of each thread. (Note that this does require some
care when starting threads, since a race condition can occur
there if you're not careful. In my case, it's not a problem,
because all of the threads are always started by the main
thread, which is also the only thread which will call
terminate(), but if other running threads might start a thread
while you're calling terminate, you'll have to add some
additional logic in thread start-up to avoid the race.)

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #9
James Kanze schrieb:
On May 31, 10:39 am, Chris Forone <4...@gmx.atwro te:
>Errata:
>bool Exit()
{
mutex.Acquire() ;
> if (active)
{
active = false;
mutex.Release() ;
// wait for thread termination :-)))
}
> mutex.Release() ;
}
>if i write:
>active = false, mutex.Release()
>i think, the mutex is first released -error
>is this right?

No. There's a sequence point at the comma, so all side effects
of the preceding expression must be finished before any side
effects of the following occur.

I do wonder about your code, however. You really need to
recover the mutex before the end of the if---otherwise, you'll
release it twice. And how do you wait for thread termination:
with a join, or with some sort of global thread counter? In the
latter case, you'll need the mutex to read it as well.

Thanx for the example. The wait is done with the win32 function
WaitForSingleOb ject() with no timeout. Excuse me for the incomplete example:

bool Exit()
{
mutex.Acquire() ;

if (active)
{
active = false;
mutex.Release() ;

return WaitForSingleOb ject(thread, INFINITE) == WAIT_OBJECT_0;
}

mutex.Release() ;
}

The sideeffect-thing i dont understand...

What about:

return someVar1 == 10 &&
someVar2 != 20 &&
(someVar3 = someFunc()) == true;

and

return (someVar1 == 10) &&
(someVar2 != 20) &&
(someVar3 = someFunc()) == true;

thanx & hand, chris
Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
5669
by: Ryan Liu | last post by:
Hi, Cane someone compare Mutex, lock, Monitor, and when to use which one? And the concept of waiting query, ready query, Pulse, PulseAll, can someone explain a little bit? Thanks a lot! Ryan
0
8471
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8903
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8592
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8661
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6216
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4213
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4393
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2044
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1795
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.