C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Threading: early days?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
James Hopkin
Guest





PostPosted: Fri Sep 17, 2004 2:07 am    Post subject: Threading: early days? Reply with quote



In the early days of C++ exception safety, I remember articles
approaching the subject of exceptions something like this (obviously
much better written):

"Here's a function that I've written. Ah, but it's not exception safe.
You have to put a try-catch round here. But then this other bit of
code needs a try-catch. So you'll have to change this other bit as
well. This ought to hold up in most situations."

These days, since Dave Abrahams' efforts in the exception
specifications of STL, and Herb Sutter's Exceptional C++ series, we
can write exception-safe code which is actually more elegant than what
we would probably have written all that time ago.

The point I want to make is that discussions between experts on
threading seem to me reminiscent of those early exceptions articles.

Is it likely that writing thread-safe code will become as natural as
writing exception-safe code is now, or is it just 'too hard', and
there'll always be an element of ugliness to it?


James


Disclaimer: my actual experience of threading is almost limited to: if
you want things to run simultaneously, run two executables. I have
read about the subject a fair amount, and tried to follow discussions
here and elsewhere.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Pete Becker
Guest





PostPosted: Sat Sep 18, 2004 11:16 am    Post subject: Re: Threading: early days? Reply with quote



James Hopkin wrote:
Quote:

The point I want to make is that discussions between experts on
threading seem to me reminiscent of those early exceptions articles.


The difference is that discussions of how to write thread-safe code have
been going on for thirty years.

Quote:
Is it likely that writing thread-safe code will become as natural as
writing exception-safe code is now, or is it just 'too hard', and
there'll always be an element of ugliness to it?


Neither. It's not a matter of coding, but of application design. You
cannot write a robust multi-threaded application by only making local
decisions about whether to lock some particular piece of code.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
dayton
Guest





PostPosted: Mon Nov 08, 2004 12:12 am    Post subject: Re: Threading: early days? Reply with quote



We're in the early days of C++ threading. Some compilers have added
extensions to declare thread local variables. Perhaps Java-like edge
locking will migrate its way into C++.

A consensus has yet to be reached about what a thread should look like
in C++. If a thread is an object, then would the non-static members of
that class be thread specific local variables?

Note that until the STL came along, C++ had no system objects. Even
activities such as the global new() and delete() appear like functions,
not as members of some global class. If you look at the Boost threads
library, even those smart people model thread instantiation as a
function call.

Much of the design decisions in C++ were made to maintain its C-like
power for systems implementation; so any fancier additions to the
language must be made with an eye towards low-low overhead. It appears
same threading related features may satisfy the criteria.

Some features I'd like to see added to C++ to support threading:

1. a new keyword, similar to 'volatile', that would declare all
modifications to a variable are interlocked or suitable for atomic
compare-and-swap operation. Perhaps instead of a storage modifier, a
new POD needs to be created. This new POD would be suitable for
semaphore and mutex operations.

2. a new modifier for classes similar to Java's "synchronized". This
would add a strictly intraprocess lock to an object, and modify every
non-const method in the class to acquire the lock on entry and release
it on exit. Note this may be implemented right now in the language with
a little discipline -- but everyone knows you'll miss a critical
function when you're adding locking. Related to "synchronized" may be a
declaration where you've specified your class as thread-safe and ypu've
taken responsibility for making it so. This particular feature adds
overhead so I'm not enthusiastic about it.

3. A modifier for class and functions that specifies methods and
functions must be re-entrant -- no statics or modifications of global
variables unless the global itself has been declared thread-safe or
synchronized.

4. Standardize the support for thread local variables.

5. Standardize the support for openMP.

6. Add threading to the STL. Implement a thread as an abstract class
that the programmer instantiates with their own thread method. This
will encapsulate all the issues of thread local variables and reaping of
status.

It is simply not true that threading can only be implemented at the
design level of an application. When you create a class, you can make
it thread-safe and each method re-entrant. In modern systems, whenever
you use a sleep function, or deal with most GUI application frameworks,
you're using threading.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Phlip
Guest





PostPosted: Tue Nov 09, 2004 12:33 pm    Post subject: Re: Threading: early days? Reply with quote

dayton wrote:

Quote:
We're in the early days of C++ threading. Some compilers have added
extensions to declare thread local variables. Perhaps Java-like edge
locking will migrate its way into C++.

A consensus has yet to be reached about what a thread should look like
in C++. If a thread is an object, then would the non-static members of
that class be thread specific local variables?

Just say no to threads. They are like 'goto'. Sometimes you need them; work
really hard not to.

Some programmers use threads as an excuse for not refactoring their code to
make it fully event driven.

When an application needs threads, it needs a small threading kernel, to
dispatch re-entrant code that uses no semaphores nor shared variables. The
"all variables might be concurrent" ideal is a good notion for computer
science, but not software engineering.

--
Phlip
http://industrialxp.org/community/bin/view/Main/TestFirstUserInterfaces


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Francis Glassborow
Guest





PostPosted: Tue Nov 09, 2004 10:37 pm    Post subject: Re: Threading: early days? Reply with quote

In article <XLNjd.27399$Qv5.3172 (AT) newssvr33 (DOT) news.prodigy.com>, Phlip
<phlip_cpp (AT) yahoo (DOT) com> writes
Quote:
dayton wrote:

We're in the early days of C++ threading. Some compilers have added
extensions to declare thread local variables. Perhaps Java-like edge
locking will migrate its way into C++.

A consensus has yet to be reached about what a thread should look like
in C++. If a thread is an object, then would the non-static members of
that class be thread specific local variables?

Just say no to threads. They are like 'goto'. Sometimes you need them; work
really hard not to.


I think that might be OK using a single core single CPU. However that is
rapidly ceasing to be the kind of hardware we are programming for. AMD
sampled dual core CPUs earlier this year, Intel have announced that
future versions of the Pentium family will be dual core and Sun
Microsystems have announced a 8-core processor.

Actually what we need is more awareness of concurrency issues and how to
provide a more appropriate abstract machine to match the hardware of the
coming years.

--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Thomas Richter
Guest





PostPosted: Thu Nov 11, 2004 11:01 am    Post subject: Re: Threading: early days? Reply with quote

Hi,

Quote:
A consensus has yet to be reached about what a thread should look like
in C++. If a thread is an object, then would the non-static members of
that class be thread specific local variables?

Just say no to threads. They are like 'goto'. Sometimes you need them; work
really hard not to.

Some programmers use threads as an excuse for not refactoring their code to
make it fully event driven.

When an application needs threads, it needs a small threading kernel, to
dispatch re-entrant code that uses no semaphores nor shared variables. The
"all variables might be concurrent" ideal is a good notion for computer
science, but not software engineering.

Allow me to disagree heavily. If you're working on a small project
where you have total control over all parts, then event-driven
programming might turn out simpler indeed, but it is quite realistic
that you need to integrate parts of other code bases that are not at
all event driven. In that case, there is often no way around threads.

A good example how that happens is the "curl" library, used to load
data off the internet. If you're calling it, "curl" gets stuck in its
main loop waiting for data arriving on one of its sockets, and your
main program "hangs". Another example are CORBA applications that (in
the simplest possibility) need to wait in their Orb for incoming
requests. It is much simpler to handle these cases with threads, and
it IMHO feels much more natural to do it this way rather than turning
the program logic around.

In conclusion, C++ really needs some kind of threading mechanism, the
sooner the better, plus some mutex mechanism to grant exclusive access
rights for a single thread. It would also help a big deal to integrate
other technology more naturally into the language, e.g. CORBA. This is
not just a single vs. multiprocessing issue.

So long,
Thomas


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Branimir Maksimovic
Guest





PostPosted: Thu Nov 11, 2004 11:03 am    Post subject: Re: Threading: early days? Reply with quote

"Phlip" <phlip_cpp (AT) yahoo (DOT) com> wrote

Quote:
dayton wrote:

We're in the early days of C++ threading. Some compilers have added
extensions to declare thread local variables. Perhaps Java-like edge
locking will migrate its way into C++.

A consensus has yet to be reached about what a thread should look like
in C++. If a thread is an object, then would the non-static members of
that class be thread specific local variables?

In my design of threads each thread has sepparate object that points to common
internal state object, so that things like joining in destructor are
possible. If thread needs to operate on other thread object I've
provided getter for that object. Of course programmer can put some
pointer/reference to some other common object if needed.

Quote:

Just say no to threads. They are like 'goto'. Sometimes you need them; work
really hard not to.

I could not agree with this. Common opinion about threads is that they
are difficult to program, and that they are cause of lot of bugs.
Actually problem is that people are used to use too many global variables.:)

Quote:

Some programmers use threads as an excuse for not refactoring their code to
make it fully event driven.

Event driven processing is ok for certain programs that have short
processing time for event (like asynchrounous processing),
but are unsuitable for programs that have
requests which will be processed longer time.
Even gui programs needs threads if some background task must be
processed or else screen will freeze Smile or programer must call
processevents(tm:) occasionally in the middle of routine that does
not have anything to do with events.


Quote:

When an application needs threads, it needs a small threading kernel, to
dispatch re-entrant code that uses no semaphores nor shared variables. The
"all variables might be concurrent" ideal is a good notion for computer
science, but not software engineering.

Well, threads are better then expensive forking and afterwads using
shared memory. It is true that there are usually small number of
shared variables and that most of the code is running independently, but
there is always some shared queue that needs processing.
for example when designing http server it is ok to implement
it fully event driven, but there are pages that needs server processing,
and then one must either fork and use shared memory which is expensive or use
pool of threads or put "processevents" calls in php interpreter
for example :)

Greetings, Bane.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.