473,569 Members | 2,648 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

return const value to be thread save?

Hi,

normally I use a local variable assigned to different return values inside the function and return it like this:
[example 1:]

int f(){
int my_err_code;
my_err_code=1;

if( /* ... */ ){

/* result ok */
my_err_code=0;
}

return my_err_code;
}
If I now add two macros to make the function thread save (assume they
work properly) isn't there still a slight chance that the return
variable is changed by a second thread just before the first thread
leaves the function?
[example 2:]

int f(){
int my_err_code;

MACRO_THREADS_E NTER();

my_err_code=1;

if( /* ... */ ){

/* result ok */
my_err_code=0;
}

MACRO_THREADS_L EAVE();

/* (Couldn't the second thread NOW have already changed
my_err_code=1 ? */

return my_err_code;
}
So, I wonder if it is necessary to return constant values to be thread
save in functions and code the fucntion like this:
[example 3:]

int f(){

MACRO_THREADS_E NTER();

if( /* ... */ ){

/* result ok */
goto jmp_ok;
}

MACRO_THREADS_L EAVE();
return 1;

jmp_ok:

MACRO_THREADS_L EAVE();
return 0;
}
Nov 14 '05 #1
3 1679
Felix Kater <f.******@gmx.n et> wrote:
So, I wonder if it is necessary to return constant values to be thread
save in functions and code the fucntion like this:
[example 3:]

int f(){

MACRO_THREADS_E NTER();

if( /* ... */ ){

/* result ok */
goto jmp_ok;
}

MACRO_THREADS_L EAVE();
return 1;

jmp_ok:

MACRO_THREADS_L EAVE();
return 0;
}


I have no answer to your threads question, except to note that threads
are not ISO C, and therefore off-topic here. You might have better
results asking in a newsgroup that deals with the particular threads
library you're using. This is a good idea anyway, since I can easily
imagine different species of threads requiring different answers to your
original question.
However, I'd like to note that if you're going to use goto like that,
you've already cast Wirthian rigor to the wind (with good reason), and
might as well put the return statement where you now have the goto.

Richard
Nov 14 '05 #2
"Felix Kater" <f.******@gmx.n et> wrote in message
news:2005050308 2941.4b77158c.f .******@gmx.net ...
normally I use a local variable assigned to different return values
inside the function and return it like this: [snip] If I now add two macros to make the function thread save (assume they
work properly) isn't there still a slight chance that the return
variable is changed by a second thread just before the first thread
leaves the function?


Threads are not part of standard C, so your question is off-topic here. You
should ask in a group dedicated to your platform or perhaps
comp.programmin g.threads.

That said, the answer is almost certainly no; if you have two threads
running a function, there will be two seperate areas for storing the
function's automatic variables (typically each thread's stack, or processor
registers).

Alex
Nov 14 '05 #3
On Tue, 03 May 2005 08:29:41 +0200, Felix Kater wrote:
Hi,

normally I use a local variable assigned to different return values inside the function and return it like this:
[example 1:]

int f(){
int my_err_code;
my_err_code=1;

if( /* ... */ ){

/* result ok */
my_err_code=0;
}

return my_err_code;
}
If I now add two macros to make the function thread save
Standard C doesn't support threads as such but similar situations can
arise in signal handling. Problems only occur when 2 "threads" (including
signal handlers) could access the same object in memory. Each invocation
of a function gets its own set of automatic variables so this isn't an
issue, i.e. the code we can see above is already thread safe in any
reasonable implementation of threads.
(assume they
work properly) isn't there still a slight chance that the return
variable is changed by a second thread just before the first thread
leaves the function?


Two invocations of the function would have separate returns accessing
separate areas of memory.

Lawrence
Nov 14 '05 #4

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

Similar topics

3
22919
by: Vipul Pathak | last post by:
Hello Friends ! I have the Following Code, that Executes a Stored Procedure and Attempt to read a Returned Integer Value from the StoredProc. But It gives Error ... ADODB.Command (0x800A0BB9) Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another. /C4U/DBOutputParameterTest.asp, line 25
4
3893
by: Ahmed Ossman | last post by:
hi, I want to save the return value from the signal() function, what is the type of the variable. i.e. I want to do the following: <variable type?> ret_sig = signal(......); I need it soo much. Thanks, Ahmed Ossman
7
1924
by: mcdonamw | last post by:
This may sound like a stupid stupid question and I figure it would b more "general" than pertaining to a specific Language. I'm using vb.net and I have a bunch of Const values in my program. can use them obviously by placing their names in place of values, henc the ideal behind using Const. My question is... is there a way to get the...
27
2646
by: Maximus | last post by:
Hi, I was just wondering, is it good to use return without arguments in a void function as following: void SetMapLayer() { if( !Map ) return; layer = LAYER_MAP; }
35
2654
by: ytrama | last post by:
Hi, I have read in one of old posting that don't cast of pointer which is returned by the malloc. I would like to know the reason. Thanks in advance, YTR
2
2341
by: Eric Lilja | last post by:
Hello, consider this complete program: #include <iostream> #include <string> using std::cout; using std::endl; using std::string; class Hanna {
5
2955
by: Shea Martin | last post by:
I have a struct like so: struct MyStruct { public: void Value( int newValue ) { mValue = newValue; } int Value() const { return mValue; } private: int mValue;
1
1653
by: tom | last post by:
I have the section of code listed below, but when I return from main an exception is thrown from the library, because while returning, destructor is called on each of the object in the sequence below: step1: destroy m2 step2: destroy f step3: destroy m1 notice, "m1" and "m2" have the class type of Message, and will both use object "f" in...
68
4575
by: Jim Langston | last post by:
I remember there was a thread a while back that was talking about using the return value of a function as a reference where I had thought the reference would become invalidated because it was a temporary but it was stated that it would not. This has come up in an irc channel but I can not find the original thread, nor can I get any code to...
0
7615
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...
0
7924
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. ...
1
7677
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...
0
7979
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...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
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...
0
3643
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2115
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
0
940
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...

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.