Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Run functions concurrently

Reply
Thread Tools

Run functions concurrently

 
 
Frederick Gotham
Guest
Posts: n/a
 
      10-21-2006

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
 
Reply With Quote
 
 
 
 
Tomekstasiowski@buziaczek.pl
Guest
Posts: n/a
 
 
Reply With Quote
 
 
 
 
Gavin Deane
Guest
Posts: n/a
 
      10-21-2006

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

 
Reply With Quote
 
noone
Guest
Posts: n/a
 
      10-23-2006
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?


 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      10-23-2006
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
 
Reply With Quote
 
Puppet_Sock
Guest
Posts: n/a
 
      10-23-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiprocessing.Pipe is not concurrently bi-directional (designflaw?) Metalone Python 2 03-03-2010 02:37 AM
Can perl create process or thread to do things concurrently? quakewang@mail.whut.edu.cn Perl Misc 2 11-22-2006 04:23 AM
run test scripts concurrently Chris McMahon Ruby 4 10-25-2006 06:46 PM
Multi accesses concurrently to a XML file xuanqn09@gmail.com Java 1 04-19-2006 11:34 AM
[J2EE] Legal to access the HttpServletRequest concurrently ? Ben_ Java 7 10-08-2005 07:03 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57