473,487 Members | 2,452 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Run functions concurrently


The Standard says that the behaviour is unspecified with regard to the
order of evaluation in the following:

int FuncA();
int FuncB();

int main()
{
FuncA() + FuncB();
}

FuncA might be called first, or then again it might be FuncB. Does the
Standard place any restriction on the two of them running at the same time
(e.g. if there are two CPU's or whatever)?

Is the behaviour of the following snippet undefined because of a sequence
point violation? (Again, I'm unsure as to whether the implementation may
run them concurrently.)

int i = 5;

int FuncA() { return ++i; }

int FuncB() { return ++i; }

int main()
{
FuncA() + FuncB();
}

If there were a requirement that either FuncA or FuncB must be executed on
its own prior to invocation of the second function, then it would seem that
there would be no problem -- a problem would only arise if they were
executed concurrently. Here's a more sensible example:

char str[] = "My dog is XX years old."

int FuncA()
{
str[10] = ' ';
str[11] = '8';

SomeLibraryFunction(str);

return 0;
}

int FuncB()
{
str[10] = '1';
str[11] = '1';

SomeLibraryFunction(str);

return 0;
}

int main()
{
FuncA() + FuncB();
}

If the two functions were to be executed one after the other, then there
would be no problem. If they were to be executed concurrently, however,
then the string might get mangled... one of them might produce "18" instead
of " 8" or "11".

Which leads me onto one more thing...

Let's say we have two functions, AnalyseStrata and TriangulateSignals.
Let's say that they're invoked as follows:

int main()
{
AnalyseStrata();
TriangulateSignals();
}

Obviously, because of sequence points, the former function must complete
execution prior to invocation of the latter function.

Let's say that the former function, on a particular system, takes 3 minutes
to execute, and that the latter function takes 7 minutes to execute. Let's
say though, that the system in question has two CPU's, and that the
functions in question can be run concurrently. Should the language provide
a way of exploiting this? One method I can think of might be something
like:

(AnalyseStrata(),0) + (TriangulateSignals(),0);

--

Frederick Gotham
Oct 21 '06 #1
5 2779

Frederick Gotham wrote:
The Standard says that the behaviour is unspecified with regard to the
order of evaluation in the following:

int FuncA();
int FuncB();

int main()
{
FuncA() + FuncB();
}

FuncA might be called first, or then again it might be FuncB. Does the
Standard place any restriction on the two of them running at the same time
(e.g. if there are two CPU's or whatever)?
Yes. Whichever function is called first, once execution of that
function begins no other code in main is executed until that function
returns.
Is the behaviour of the following snippet undefined because of a sequence
point violation? (Again, I'm unsure as to whether the implementation may
run them concurrently.)

int i = 5;

int FuncA() { return ++i; }

int FuncB() { return ++i; }

int main()
{
FuncA() + FuncB();
}
No undefined behaviour there. The calls to FuncA and FuncB can not be
interleaved.
If there were a requirement that either FuncA or FuncB must be executed on
its own prior to invocation of the second function, then it would seem that
there would be no problem -- a problem would only arise if they were
executed concurrently.
Correct.

Caveat: In answering your question I have not referred directly to the
standard. I have referred to Herb Sutter.

http://www.gotw.ca/gotw/056.htm

<snip>

Gavin Deane

Oct 21 '06 #3
On Sat, 21 Oct 2006 08:29:36 +0000, Frederick Gotham wrote:
>
The Standard says that the behaviour is unspecified with regard to the
order of evaluation in the following:

int FuncA();
int FuncB();

int main()
{
FuncA() + FuncB();
}

FuncA might be called first, or then again it might be FuncB. Does the
Standard place any restriction on the two of them running at the same time
(e.g. if there are two CPU's or whatever)?
I don't believe the C++ standard makes any assumptions about concurrency
of multiple execution threads, and that's what you're asking, right?

I'm not sure how you would implement the above in multiple threads anyhow...

The expression A+B would always be done in a single thread, right?

In what scenario would it not be?
Oct 23 '06 #4
noone posted:
The expression A+B would always be done in a single thread, right?

In what scenario would it not be?

If the computer had two CPU's, (and thus was able to execute two instructions
simultaneously), then it might be able to run both functions' code at the
same time.

--

Frederick Gotham
Oct 23 '06 #5
Frederick Gotham wrote:
[parallelism issues]

The standard does not talk about multiple threads or parallelism.
You would need to consult the specifics of your platform and
compiler. Different platforms may do different things.
Socks

Oct 23 '06 #6

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

Similar topics

18
5269
by: bart_nessux | last post by:
I need a script to call several functions at the same time. How does one call more than one function simultaneously?
2
7990
by: meng | last post by:
is it possible to use a single connection object shared by several tasks where each task is handled by a thread? these tasks call stored procedures that return record sets, no editing, update or...
3
1462
by: Chris Anderson | last post by:
Could someone please tell me how I ensure only one instance of an application can execute concurrently?
6
11025
by: junky_fellow | last post by:
what are reentrant functions? What are the characteristics of a reentrant code ? what things should be kept in mind while writing a reentrant code ?
12
2596
by: Letsee++ | last post by:
hi, is it possible to write a program to read and print a number without using standard library functions in C and C++? if yes ....how? waiting for a reply...
0
1028
by: Brian Parker | last post by:
I created a new solution with 2 projects: a) A C# Web Service that pauses for 15 seconds then returns a string. b) A C# Web Application that calls the web service and displays the string. ...
1
1160
by: VMI | last post by:
I'm working on a web application that will store employees on to a sql server db. My Employee class consists of several variables (emplcode, fname, lname, lname2) and everytime the user saves a...
17
3228
by: fmassei | last post by:
Dear all, I'm trying to put some old code in a portable, cross-platform library and I'm facing a big problem: is there a standard, platform- independent way to write a reentrant function that...
1
1696
by: danep2 | last post by:
Let me start by saying that this is more a question about principle than practice - with the speed of today's computers it's probably rarely an actual issue. Still I'd like to know... If I have...
0
7106
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6967
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...
0
7137
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,...
0
7181
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...
0
5442
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,...
0
4565
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...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1381
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 ...
1
600
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.