473,807 Members | 2,827 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Code to behave as Re-Entrant

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 */
..
..
}
Sep 11 '08 #1
8 1883
On 11 Sep, 11:21, Pallav singh <singh.pal...@g mail.comwrote:
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.Interproc ess (http://www.boost.org/
doc/libs/1_36_0/doc/html/interprocess/
synchronization _mechanisms.htm l).

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

Regards,

Pete
Sep 11 '08 #2
On 2008-09-11 16:11, ne*******@gmail .com wrote:
On 11 Sep, 11:21, Pallav singh <singh.pal...@g mail.comwrote:
>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.Interproc ess (http://www.boost.org/
doc/libs/1_36_0/doc/html/interprocess/
synchronization _mechanisms.htm l).

Alternatively, are you on Win32? Take a look at the WINAPI call
EnterCriticalSe ction().
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.

--
Erik Wikström
Sep 11 '08 #3
On 2008-09-11 12:21, Pallav singh 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 */
.
.
}
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.

--
Erik Wikström
Sep 11 '08 #4
On Sep 11, 7:01*pm, Erik Wikström <Erik-wikst...@telia. comwrote:
On 2008-09-11 16:11, newbar...@gmail .com wrote:


On 11 Sep, 11:21, Pallav singh <singh.pal...@g mail.comwrote:
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.Interproc ess (http://www.boost.org/
doc/libs/1_36_0/doc/html/interprocess/
synchronization _mechanisms.htm l).
Alternatively, are you on Win32? Take a look at the WINAPI call
EnterCriticalSe ction().

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
Sep 11 '08 #5
On Sep 12, 3:35*am, juanvicfer <juanvic...@gma il.comwrote:
On Sep 11, 7:01*pm, Erik Wikström <Erik-wikst...@telia. comwrote:
On 2008-09-11 16:11, newbar...@gmail .com wrote:
On 11 Sep, 11:21, Pallav singh <singh.pal...@g mail.comwrote:
>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.Interproc ess (http://www.boost.org/
doc/libs/1_36_0/doc/html/interprocess/
synchronization _mechanisms.htm l).
Alternatively, are you on Win32? Take a look at the WINAPI call
EnterCriticalSe ction().
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

Sep 12 '08 #6
On Sep 12, 11:49*am, Pallav singh <singh.pal...@g mail.comwrote:
On Sep 12, 3:35*am, juanvicfer <juanvic...@gma il.comwrote:
On Sep 11, 7:01*pm, Erik Wikström <Erik-wikst...@telia. comwrote:
On 2008-09-11 16:11, newbar...@gmail .com wrote:
On 11 Sep, 11:21, Pallav singh <singh.pal...@g mail.comwrote:
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.Interproc ess (http://www.boost.org/
doc/libs/1_36_0/doc/html/interprocess/
synchronization _mechanisms.htm l).
Alternatively, are you on Win32? Take a look at the WINAPI call
EnterCriticalSe ction().
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

IO code is usually not reentrant because it relies on shared,
singleton resources such as disks.
Sep 12 '08 #7
On 2008-09-12 08:52, Pallav singh wrote:
On Sep 12, 11:49 am, Pallav singh <singh.pal...@g mail.comwrote:
>On Sep 12, 3:35 am, juanvicfer <juanvic...@gma il.comwrote:
On Sep 11, 7:01 pm, Erik Wikström <Erik-wikst...@telia. comwrote:
On 2008-09-11 16:11, newbar...@gmail .com wrote:
On 11 Sep, 11:21, Pallav singh <singh.pal...@g mail.comwrote:
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.Interproc ess (http://www.boost.org/
doc/libs/1_36_0/doc/html/interprocess/
synchronization _mechanisms.htm l).
Alternatively, are you on Win32? Take a look at the WINAPI call
EnterCriticalSe ction().
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.

--
Erik Wikström
Sep 12 '08 #8
On Sep 12, 5:48 pm, Erik Wikström <Erik-wikst...@telia. comwrote:
On 2008-09-12 08:52, Pallav singh wrote:
On Sep 12, 11:49 am, Pallav singh <singh.pal...@g mail.comwrote:
On Sep 12, 3:35 am, juanvicfer <juanvic...@gma il.comwrote:
On Sep 11, 7:01 pm, Erik Wikström <Erik-wikst...@telia. comwrote:
On 2008-09-11 16:11, newbar...@gmail .com wrote:
On 11 Sep, 11:21, Pallav singh <singh.pal...@g mail.comwrote:
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.Interproc ess (http://www.boost.org/
doc/libs/1_36_0/doc/html/interprocess/
synchronization _mechanisms.htm l).
Alternatively, are you on Win32? Take a look at the WINAPI call
EnterCriticalSe ction().
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.

--
gpd
Sep 12 '08 #9

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

Similar topics

289
1805
by: napi | last post by:
I think you would agree with me that a C compiler that directly produces Java Byte Code to be run on any JVM is something that is missing to software programmers so far. With such a tool one could stay with C and still be able to produce Java byte code for platform independent apps. Also, old programs (with some tweaking) could be re-compiled and ported to the JVM. We have been developing such a tool over the last 2 years and currently...
5
4311
by: malcolm | last post by:
I'm trying to make a combo box custom control (Windows Forms .NET 1.1 c#) that can behave like a label programatically at run time. This is actually a strong feature request by our customers. I really don't want to have to make a UserControl that has both a label and ComboBox in it. I would rather have my control derive from a ComboBox for databinding (and other) reasons. I can get close, I've tried setting cbo.DropDownStyle =...
1
10422
by: Mitan | last post by:
Hello, I'm a beginner with what appears to be a simple question for which I haven't been able to get an answer. Can someone explain what "implementation code" is in relation to VB.NET? I want to be sure I have a good understanding of this before I continue with my studies. Thanks in advance. Semper Fi
16
1440
by: Joe Fallon | last post by:
I have a C# class that works correctly. I translated it to VB and now it runs differently. The C# class evaluates the Public properties and then executes MyBase.New. So default values are set first and then MyBase.New reads in new values (if they exist.) The VB code does it in reverse. It excutes MyBase.New and then overwrites the new values when it sets the defaults.
5
2369
by: KitKat | last post by:
I've got two queries; one is a modification of an older query; the other I created from scratch. The old one is about 5 copies down the road from something that has been expandable when I'm in View mode--it showed the Points of Contact for records whose Begin Date was in a form-selected (or manually inputted) year. That used to be all it did. Then copied it, took out the year criterion, and changed "POC" to show the Director field...
5
1958
by: Noozer | last post by:
I have two Classes in use in an ASP application... The first Class is named "ORDER". It represents one job order. It has a Delete method, among others, which deletes it from my database. The second Class is named "ORDERS". It represents a collection of ORDER objects. Basically an array of ORDER objects. It has a Remove method that JUST removes the ORDER object from the collection - no data is erased.
4
1791
by: Gert Kok | last post by:
The microsoft page http://msdn2.microsoft.com/en-us/library/9fkccyh4.aspx states: Remarks (nr 4) Virtual properties behave like abstract methods, except for the differences in declaration and invocation syntax.
28
2373
by: hijkl | last post by:
hey guys anything wrong with this code?? if it is then what? int *array(int n){ return new int(n); } int main(){ int *p = array(10); for( int i = 0; i < 10; i++ ) {
2
1683
by: Sunfire | last post by:
I was wondering if there was a way to hit enter to insert blank lines while in the designer? I tried this but all it seems to want to do is insert code that really shouldn't be there. For example, I had a heading 1 centered on a page. Right under that I had some words in smaller print right under the heading. When I hit enter after the heading 1, it automatically put the text right below it in a <p></ptag. I don't want this sort of stuff to...
0
9600
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10373
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10374
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,...
1
7651
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
6880
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5685
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4331
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3859
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3011
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.