Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #14380 > unrolled thread

Some doubts about Mutex

Started byWilly <gruppi@willygroup.org>
First post2012-02-21 16:52 +0100
Last post2012-02-21 23:58 -0600
Articles 2 — 2 participants

Back to article view | Back to comp.lang.c++


Contents

  Some doubts about Mutex Willy <gruppi@willygroup.org> - 2012-02-21 16:52 +0100
    Re: Some doubts about Mutex Paavo Helde <myfirstname@osa.pri.ee> - 2012-02-21 23:58 -0600

#14380 — Some doubts about Mutex

FromWilly <gruppi@willygroup.org>
Date2012-02-21 16:52 +0100
SubjectSome doubts about Mutex
Message-ID<4f43bdbd@extreme.x-privat.org>
Hi,
I've just started to study parallel programming on Linux and I've 
already some doubts about Mutex.

I've a generic data, which I want to access with mutually exclusion 
(1read or 1read).

data

Then I've read/write method:
read_data(...), write_data(...)

Then I've other two method like:

operation_1(...)
{
	...
	operation_2(...)
	...
}

operation_2(...)
{
	...
	read_data(...)		//[1]
	...
	write_data(...)		//[2]
	...
}


Initially I had put between mutex only the read_data and write_data 
methods, then I've realized that this way can bring some trouble because 
if some process/thread call operation_2, some other process/thread can 
call write_data between [1] and [2] screwing up all.

Which is the better way to solve this situation?

Thanks
-- 
*Willy* - *Contatti*: http://snipurl.com/contatti
* *FAQ* di /it-alt.comp.software.mozilla/: http://www.FAQmozilla.org
* *Gratuiki* - Software Free: http://gratuiki.willygroup.org

[toc] | [next] | [standalone]


#14400

FromPaavo Helde <myfirstname@osa.pri.ee>
Date2012-02-21 23:58 -0600
Message-ID<XnsA00151217E3A1myfirstnameosapriee@216.196.109.131>
In reply to#14380
Willy <gruppi@willygroup.org> wrote in news:4f43bdbd@extreme.x-
privat.org:

> Hi,
> I've just started to study parallel programming on Linux and I've 
> already some doubts about Mutex.
> 
> I've a generic data, which I want to access with mutually exclusion 
> (1read or 1read).
> 
> data
> 
> Then I've read/write method:
> read_data(...), write_data(...)
> 
> Then I've other two method like:
> 
> operation_1(...)
> {
>      ...
>      operation_2(...)
>      ...
> }
> 
> operation_2(...)
> {
>      ...
>      read_data(...)          //[1]
>      ...
>      write_data(...)          //[2]
>      ...
> }
> 
> 
> Initially I had put between mutex only the read_data and write_data 
> methods, then I've realized that this way can bring some trouble 
because 
> if some process/thread call operation_2, some other process/thread can 
> call write_data between [1] and [2] screwing up all.

This is not about the mutex, this is about your program logic. If read 
and write must always strictly follow each other in the same thread, then 
naturally you have to keep the mutex locked during the whole cycle, 
meaning the lock should go in operation_2().

In multithreading applications the data is often read and written not in 
a single place, but in some queues. For example, threads might read 
"events" from some queue and write the results in another queue. In this 
approach only queue push and pop operations must be protected, which is 
more scalable. 

> Which is the better way to solve this situation?

Depends on what your program must achieve. Probably you have to rethink 
your app logic.

hth
Paavo


[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web