when loop prototype

So i originally posted the idea on this forum (http://www.cplusplus.com/forum/lounge/76849/#msg413006) and i now have a working pro type. the syntax is:
when(seconds)
{
code
}
it will only execute the code when a timer i found is equal to seconds

here is the code (when.h):
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
#ifndef whenDev_when_h
#define whenDev_when_h

//class credit to alex79roma
class timer {
private:
    unsigned long begTime;
public:
    void start() {
        begTime = clock();
    }
    
    unsigned long elapsedTime() {
        return ((unsigned long) clock() - begTime) / CLOCKS_PER_SEC;
    }
    
    bool isTimeout(unsigned long seconds) {
        return seconds >= elapsedTime();
    }
};

timer t;
typedef unsigned long end;
end seconds;

#define when(expression)\
t.start();\
while(true) {\
if(t.elapsedTime() >= seconds) {\
break;\
}\
}

#endif 
Why isn't expression used?
How does it handle nesting? And concurrency?
Can you make it not use CPU time while the body is not run?
Last edited on
it can nest. expression is what grabs seconds. what is concurrency? what do you mean by your last question?
expression isn't used in the macro. The seconds global is never set.

The macro can be nested, but it will behave incorrectly. It's not reentrant. Thus, it will also fail with concurrency.
Last edited on
so it behaves like a function ptr but with no return vslue
Topic archived. No new replies allowed.