473,385 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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_ENTER();

my_err_code=1;

if( /* ... */ ){

/* result ok */
my_err_code=0;
}

MACRO_THREADS_LEAVE();

/* (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_ENTER();

if( /* ... */ ){

/* result ok */
goto jmp_ok;
}

MACRO_THREADS_LEAVE();
return 1;

jmp_ok:

MACRO_THREADS_LEAVE();
return 0;
}
Nov 14 '05 #1
3 1671
Felix Kater <f.******@gmx.net> 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_ENTER();

if( /* ... */ ){

/* result ok */
goto jmp_ok;
}

MACRO_THREADS_LEAVE();
return 1;

jmp_ok:

MACRO_THREADS_LEAVE();
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.net> wrote in message
news:20050503082941.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.programming.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
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)...
4
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...
7
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...
27
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
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
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
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
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...
68
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.