Code to behave as Re-Entrant

P

Pallav singh

How to make flowing Code to behave as Re-Entrant ( concurrently
accessed safely by Multiple user )

Func( )
{
..
static char buffer_1 [1024] ;
/* few write Operation on buffer */
..
..
static char buffer_2 [1024] ;
/* few write Operation on buffer */
..
..
..
static char buffer_3 [1024] ;
/* few write Operation on buffer */
..
..
}
 
N

newbarker

How to make flowing Code to behave as  Re-Entrant ( concurrently
accessed safely by Multiple user  )

Func( )
{
.
static char buffer_1 [1024] ;
/* few write Operation on buffer   */
.
.
static char buffer_2 [1024] ;
/* few write Operation on buffer   */
.
.
.
static char buffer_3 [1024] ;
/* few write Operation on buffer   */
.
.



}- Hide quoted text -

Can you use the Boost libraries in your codebase? If so, check out the
synchronisation mechanisms in Boost.Interprocess (http://www.boost.org/
doc/libs/1_36_0/doc/html/interprocess/
synchronization_mechanisms.html).

Alternatively, are you on Win32? Take a look at the WINAPI call
EnterCriticalSection().

Regards,

Pete
 
E

Erik Wikström

How to make flowing Code to behave as Re-Entrant ( concurrently
accessed safely by Multiple user )

Func( )
{
.
static char buffer_1 [1024] ;
/* few write Operation on buffer */
.
.
static char buffer_2 [1024] ;
/* few write Operation on buffer */
.
.
.
static char buffer_3 [1024] ;
/* few write Operation on buffer */
.
.



}- Hide quoted text -

Can you use the Boost libraries in your codebase? If so, check out the
synchronisation mechanisms in Boost.Interprocess (http://www.boost.org/
doc/libs/1_36_0/doc/html/interprocess/
synchronization_mechanisms.html).

Alternatively, are you on Win32? Take a look at the WINAPI call
EnterCriticalSection().

A reentrant function is not just thread-safe, it can also safely call
iteself, which usually is not possible if the function has any static
variables.
 
E

Erik Wikström

How to make flowing Code to behave as Re-Entrant ( concurrently
accessed safely by Multiple user )

Func( )
{
.
static char buffer_1 [1024] ;
/* few write Operation on buffer */
.
.
static char buffer_2 [1024] ;
/* few write Operation on buffer */
.
.
.
static char buffer_3 [1024] ;
/* few write Operation on buffer */
.
.
}

Make the buffers non-static, of if that grows to much on the stack
allocate the buffers dynamically. Also you should investigate the
possibility to only use one buffer.
 
J

juanvicfer

How to make flowing Code to behave as  Re-Entrant ( concurrently
accessed safely by Multiple user  )
Func( )
{
.
static char buffer_1 [1024] ;
/* few write Operation on buffer   */
.
.
static char buffer_2 [1024] ;
/* few write Operation on buffer   */
.
.
.
static char buffer_3 [1024] ;
/* few write Operation on buffer   */
.
.
}- Hide quoted text -
Can you use the Boost libraries in your codebase? If so, check out the
synchronisation mechanisms in Boost.Interprocess (http://www.boost.org/
doc/libs/1_36_0/doc/html/interprocess/
synchronization_mechanisms.html).
Alternatively, are you on Win32? Take a look at the WINAPI call
EnterCriticalSection().

A reentrant function is not just thread-safe, it can also safely call
iteself, which usually is not possible if the function has any static
variables.

I don´t really understand why a function with static variables usually
cannot call itself?? What I think that would happen is that that
identifier refers to the same memory position in both the caller an
the calee, right? But, where´s the problem?
 
P

Pallav singh

On 2008-09-11 16:11, (e-mail address removed) wrote:
How to make flowing Code to behave as  Re-Entrant ( concurrently
accessed safely by Multiple user  )
Func( )
{
.
static char buffer_1 [1024] ;
/* few write Operation on buffer   */
.
.
static char buffer_2 [1024] ;
/* few write Operation on buffer   */
.
.
.
static char buffer_3 [1024] ;
/* few write Operation on buffer   */
.
.
}- Hide quoted text -
Can you use the Boost libraries in your codebase? If so, check out the
synchronisation mechanisms in Boost.Interprocess (http://www.boost.org/
doc/libs/1_36_0/doc/html/interprocess/
synchronization_mechanisms.html).
Alternatively, are you on Win32? Take a look at the WINAPI call
EnterCriticalSection().
A reentrant function is not just thread-safe, it can also safely call
iteself, which usually is not possible if the function has any static
variables.

I don´t really understand why a function with static variables usually
cannot call itself?? What I think that would happen is that that
identifier refers to the same memory position in both the caller an
the calee, right? But, where´s the problem?

Consider a senario

we have Above code in Shared Library

user_1 has updated buffer 1 & 2 ...... He require data of either
buffer to compute for buffer 3
But i mean time user_2 start executing the function and changes data
stored in buffer 1

User_2 Corrupts Data of User_1 Un-knowingly
 
P

Pallav singh

On 2008-09-11 16:11, (e-mail address removed) wrote:
How to make flowing Code to behave as  Re-Entrant ( concurrently
accessed safely by Multiple user  )
Func( )
{
.
static char buffer_1 [1024] ;
/* few write Operation on buffer   */
.
.
static char buffer_2 [1024] ;
/* few write Operation on buffer   */
.
.
.
static char buffer_3 [1024] ;
/* few write Operation on buffer   */
.
.
}- Hide quoted text -
Can you use the Boost libraries in your codebase? If so, check out the
synchronisation mechanisms in Boost.Interprocess (http://www.boost.org/
doc/libs/1_36_0/doc/html/interprocess/
synchronization_mechanisms.html).
Alternatively, are you on Win32? Take a look at the WINAPI call
EnterCriticalSection().
A reentrant function is not just thread-safe, it can also safely call
iteself, which usually is not possible if the function has any static
variables.
I don´t really understand why a function with static variables usually
cannot call itself?? What I think that would happen is that that
identifier refers to the same memory position in both the caller an
the calee, right? But, where´s the problem?

Consider a senario

we have Above code in Shared Library

user_1 has updated buffer 1 & 2 ...... He require data of either
buffer to compute for buffer 3
But i mean time user_2 start executing the function and changes data
stored in buffer 1

User_2 Corrupts Data of User_1 Un-knowingly

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

A function may be non-reentrant for a number of reasons:

– it uses a static data structure
– it manipulates the heap: malloc(), free(), etc.
– it uses the standard I/O library
e,g, scanf(), printf()
the library uses global data structures in a non-reentrant way
– rely on locks to singleton resources

IO code is usually not reentrant because it relies on shared,
singleton resources such as disks.
 
E

Erik Wikström

On Sep 11, 7:01 pm, Erik Wikström <[email protected]> wrote:
On 2008-09-11 16:11, (e-mail address removed) wrote:
How to make flowing Code to behave as Re-Entrant ( concurrently
accessed safely by Multiple user )
Func( )
{
.
static char buffer_1 [1024] ;
/* few write Operation on buffer */
.
.
static char buffer_2 [1024] ;
/* few write Operation on buffer */
.
.
.
static char buffer_3 [1024] ;
/* few write Operation on buffer */
.
.
}- Hide quoted text -
Can you use the Boost libraries in your codebase? If so, check out the
synchronisation mechanisms in Boost.Interprocess (http://www.boost.org/
doc/libs/1_36_0/doc/html/interprocess/
synchronization_mechanisms.html).
Alternatively, are you on Win32? Take a look at the WINAPI call
EnterCriticalSection().
A reentrant function is not just thread-safe, it can also safely call
iteself, which usually is not possible if the function has any static
variables.
I don´t really understand why a function with static variables usually
cannot call itself?? What I think that would happen is that that
identifier refers to the same memory position in both the caller an
the calee, right? But, where´s the problem?

Consider a senario

we have Above code in Shared Library

user_1 has updated buffer 1 & 2 ...... He require data of either
buffer to compute for buffer 3
But i mean time user_2 start executing the function and changes data
stored in buffer 1

User_2 Corrupts Data of User_1 Un-knowingly

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

A function may be non-reentrant for a number of reasons:

– it uses a static data structure
– it manipulates the heap: malloc(), free(), etc.
– it uses the standard I/O library
e,g, scanf(), printf()
the library uses global data structures in a non-reentrant way
– rely on locks to singleton resources


I fail to see why manipulating the heap would make a function non-
reentrant. If the allocator is thread-safe there is no issue, and if it
is't you can use a mutex to make the operations thread-safe.

Which of course also means that using locks to singleton resources (such
as the allocator) does not automatically make a function non-reentrant
either. On the other hand there are situations where using locks in the
wrong way can lead to problems.
 
G

gpderetta

On 2008-09-11 16:11, (e-mail address removed) wrote:
How to make flowing Code to behave as Re-Entrant ( concurrently
accessed safely by Multiple user )
Func( )
{
.
static char buffer_1 [1024] ;
/* few write Operation on buffer */
.
.
static char buffer_2 [1024] ;
/* few write Operation on buffer */
.
.
.
static char buffer_3 [1024] ;
/* few write Operation on buffer */
.
.
}- Hide quoted text -
Can you use the Boost libraries in your codebase? If so, check out the
synchronisation mechanisms in Boost.Interprocess (http://www.boost.org/
doc/libs/1_36_0/doc/html/interprocess/
synchronization_mechanisms.html).
Alternatively, are you on Win32? Take a look at the WINAPI call
EnterCriticalSection().
A reentrant function is not just thread-safe, it can also safely call
iteself, which usually is not possible if the function has any static
variables.
I don´t really understand why a function with static variables usually
cannot call itself?? What I think that would happen is that that
identifier refers to the same memory position in both the caller an
the calee, right? But, where´s the problem?
--
Erik Wikström
Consider a senario
we have Above code in Shared Library
user_1 has updated buffer 1 & 2 ...... He require data of either
buffer to compute for buffer 3
But i mean time user_2 start executing the function and changes data
stored in buffer 1
User_2 Corrupts Data of User_1 Un-knowingly
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

A function may be non-reentrant for a number of reasons:
– it uses a static data structure
– it manipulates the heap: malloc(), free(), etc.
– it uses the standard I/O library
e,g, scanf(), printf()
the library uses global data structures in a non-reentrant way
– rely on locks to singleton resources

I fail to see why manipulating the heap would make a function non-
reentrant. If the allocator is thread-safe there is no issue, and if it
is't you can use a mutex to make the operations thread-safe.

Which of course also means that using locks to singleton resources (such
as the allocator) does not automatically make a function non-reentrant
either. On the other hand there are situations where using locks in the
wrong way can lead to problems.

Well, reentrancy is a stronger property than thread safety. For
example under posix, a function execution can be interrupted at any
point by a signal which causes the execution of the signal handler.
Protecting the shared state between the interrupted function and the
handler by a mutex is not necessary nor sufficient to guarantee
reentrancy. Posix calls this type of reentrancy async signal safety.
Memory allocation is not async signal safe. This is beyond standard C+
+, though.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top