Le 13/01/11 13:42, tm a crit :
I want portable support for threads, not the threads
provided by the OS. I want that my threaded standard C
programs are portable. I don't care if it is pthreads or
something else. Probably it makes sense to take the
proposed C++ thread standard (when it is not template
or class based).
I am
doubtful if all the code written for pthreads or windows
threads will be rewritten to cater for yet another standard
So am I.
The people can continue to use the OS
dependend threads. They are not standard C anyway, but
standard C threads allow future programs to be portable.
[snip]
Why you need that?
Can you give an example?
We all use closures all the time without realizing.
Examples of closures are:
The conditions of a while loop
The statements in loop bodies
The statements that are conditionally executed in
an if statement.
Closures are expressions and they might be executed
once, many times or not at all. They differ from normal
function pointers, but they can be implemented with
function pointers.
There are several programming languages that compile
to C and which support closures. Closure support in C
would make the job of this compilers easier.
In case of Seed7 I have already implemented closure
support in the Seed7 compiler. So I do not really need it,
but it would simplify many things. Probably a C compiler
would have to do similar things that the Seed7 compiler,
therefore I describe the strategy here:
First it must be decided if a function with closure
parameter is called recursively. A non-recursive function
with closure parameters can be implemented with inlining.
Recursive functions cannot be implemented this way.
Implementing closures with function pointers works this
way. A function pointer together with an environment data
structure is used. A call of the closure parameter is
compiled to calling the function (via function pointer)
and giving this function a pointer to the environment
data as parameter.
The actual closure parameter must be analyzed to find
out which local variables (and parameters) it uses.
This local variables are the environment data of the
actual closure parameter.
This is just a general concept, the devil is in the
details.
I do not think that a language standard can go so far in compiler
internals.
This is not compiler internals. It is like the register
keyword. Based on the gcc support I defined the macros
likely and unlikely. So a program can contain code like
if (unlikely((abc = malloc(sizeof(qwert))) == NULL)) {
/* out of memory */
...
} else {
/* okay */
...
The programmer has defenitely more knowledge about how
often an out of memory situation occurs. When a compiler
is not able to support this feature, it can just be ignored.
Greetings Thomas Mertes
--
Seed7 Homepage:
http://seed7.sourceforge.net
Seed7 - The extensible programming language: User defined statements
and operators, abstract data types, templates without special
syntax, OO with interfaces and multiple dispatch, statically typed,
interpreted or compiled, portable, runs under linux/unix/windows.