Thread overview | |||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
September 27, 2004 Can D throw a hanging exception if a function takes too long? | ||||
---|---|---|---|---|
| ||||
Earlier today, while working on a BigDecimal library for D (my first project in D, porting a JavaScript-based BigDecimal library I wrote a few years ago), I accidentally caused a hang in my code. I'm thinking it would be really nice to specify a code block like this: unittest { timelimit(n) { // code to run goes here } } where n is a number of milliseconds that the code within must finish running by. If the elapsed amount of time passes, throw an exception and bail out. I don't know anything worth talking about when it comes to compilers, so if this is a "mission: impossible" task, I'd like to know. |
September 27, 2004 Re: Can D throw a hanging exception if a function takes too long? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ajvincent | ajvincent@juno.com wrote:
> Earlier today, while working on a BigDecimal library for D (my first project in
> D, porting a JavaScript-based BigDecimal library I wrote a few years ago), I
> accidentally caused a hang in my code.
>
> I'm thinking it would be really nice to specify a code block like this:
> unittest {
> timelimit(n) {
> // code to run goes here
> }
> }
Kids, don't call this at home. I have no idea if it's actually doing it properly. All I know is that it worked the one time I tried it.
private import std.thread;
private import std.c.windows.windows;
extern (Windows) BOOL TerminateThread (HANDLE handle, DWORD exitCode);
class TimeLimitError : Error
{
this ()
{
super ("TimeLimitError: Time limit has run out.");
}
}
void timelimit (ulong milliseconds, void delegate () call)
{
int call_wrap ()
{
call ();
return 0;
}
Thread thread = new Thread (&call_wrap);
thread.start ();
try thread.wait (milliseconds);
catch (ThreadError error) { }
if (thread.getState == Thread.TS.RUNNING)
{
TerminateThread (thread.hdl, 0);
foreach (inout Thread compare; Thread.getAll ())
if (compare === thread)
{
compare = null;
break;
}
Thread.nthreads --;
CloseHandle (thread.hdl);
throw new TimeLimitError ();
}
}
Usage example:
timelimit (100, delegate void ()
{
while (1)
printf ("");
});
Does anyone know if there's a way to terminate threads in Linux?
|
September 27, 2004 Re: Can D throw a hanging exception if a function takes too long? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ajvincent | In article <cj8bge$17ni$1@digitaldaemon.com>, ajvincent@juno.com says... >I'm thinking it would be really nice to specify a code block like this: >unittest { >timelimit(n) { >// code to run goes here >} >} > >where n is a number of milliseconds that the code within must finish running by. Actualy, I think the code would look like this: # unittest # { # auto TimeLimit timeLimit = new TimeLimit(n); # // code to run goes here # } where TimeLimit is an auto class. Its job would be simply to sleep for the given timeout and then throw - unless it gets destructed first. >if this >is a "mission: impossible" task, I'd like to know. I can imagine some difficulties ... throwing exceptions in constructors/destructors; throwing exceptions from another thread, and so on. It may be that the exception handling mechanism is not the ideal tool for this, but that's the best idea I have so far. Jill |
September 27, 2004 Re: Can D throw a hanging exception if a function takes too long? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ajvincent |
How about this:
[1] new loop construct
whilst
{
// never loops more times than u32_max or u64_max
// compiler issues error if looped more times during unit testing
}
[2] new prefix for keywords
no_overflow ubyte nFoo = 0xFF;
while( nFoo++ ) {}; // error during unit testing
Chr. Grade
ajvincent@juno.com wrote:
> I'm thinking it would be really nice to specify a code block like this:
> unittest {
> timelimit(n) { // code to run goes here };
> }
>
> where n is a number of milliseconds that the code within must finish running by.
> If the elapsed amount of time passes, throw an exception and bail out.
|
September 27, 2004 hanging exception- NEED IT,NEED IT,NEED IT | ||||
---|---|---|---|---|
| ||||
Posted in reply to ajvincent | It is a very good idea, I need them SO MUCH in my programs. And the syntax should be the same in WINDOWS and LINUX. So Walter, PLEASE PLEASE PLEASE make it! In article <cj8bge$17ni$1@digitaldaemon.com>, ajvincent@juno.com says... > >Earlier today, while working on a BigDecimal library for D (my first project in D, porting a JavaScript-based BigDecimal library I wrote a few years ago), I accidentally caused a hang in my code. > >I'm thinking it would be really nice to specify a code block like this: >unittest { >timelimit(n) { >// code to run goes here >} >} > >where n is a number of milliseconds that the code within must finish running by. If the elapsed amount of time passes, throw an exception and bail out. > >I don't know anything worth talking about when it comes to compilers, so if this is a "mission: impossible" task, I'd like to know. > > |
September 27, 2004 Re: Can D throw a hanging exception if a function takes too long? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ajvincent | In article <cj8bge$17ni$1@digitaldaemon.com>, ajvincent@juno.com says... <snip> >I'm thinking it would be really nice to specify a code block like this: >unittest { >timelimit(n) { >// code to run goes here >} >} > >where n is a number of milliseconds that the code within must finish running by. If the elapsed amount of time passes, throw an exception and bail out. <snip> The time it takes will depend on the speed of the CPU. So wouldn't it make more sense to specify a timelimit in CPU cycles? Stewart. |
September 27, 2004 Re: Can D throw a hanging exception if a function takes too long? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ajvincent | On Mon, 27 Sep 2004 06:21:02 +0000, ajvincent wrote:
> Earlier today, while working on a BigDecimal library for D (my first project in D, porting a JavaScript-based BigDecimal library I wrote a few years ago), I accidentally caused a hang in my code.
>
> I'm thinking it would be really nice to specify a code block like this:
> unittest {
> timelimit(n) {
> // code to run goes here
> }
> }
>
> where n is a number of milliseconds that the code within must finish running by. If the elapsed amount of time passes, throw an exception and bail out.
>
> I don't know anything worth talking about when it comes to compilers, so if this is a "mission: impossible" task, I'd like to know.
I suspect this would be difficult to do at the language level. However, a threading library could have such functionality, but I think a program would need to be designed for it (i.e. set up it's own watchdog thread).
What may be more immediately useful to some people, under Unix at least, is
the alarm() system call.
Mike Swieton
__
The difference between losers and winners is that losers don't fail enough.
- Ross Jeffries
|
September 27, 2004 Re: Can D throw a hanging exception if a function takes too long? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chr. Grade | Chr. Grade wrote:
>
> How about this:
>
> [1] new loop construct
>
> whilst
> {
> // never loops more times than u32_max or u64_max
> // compiler issues error if looped more times during unit testing
> }
>
>
> [2] new prefix for keywords
>
> no_overflow ubyte nFoo = 0xFF;
>
> while( nFoo++ ) {}; // error during unit testing
>
Don't quite see it:
invariant {
assert(nFoo <= 0xFF);
}
it's valid.
Wrap a class that uses this for no_overflow...
Regards,
Sjoerd
|
September 27, 2004 Re: Can D throw a hanging exception if a function takes too long? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sjoerd van Leent | In article <cj9bcm$109b$1@digitaldaemon.com>, Sjoerd van Leent says... > >Chr. Grade wrote: >> >> How about this: >> >> [1] new loop construct >> >> whilst >> { >> // never loops more times than u32_max or u64_max >> // compiler issues error if looped more times during unit testing >> } >> >> >> [2] new prefix for keywords >> >> no_overflow ubyte nFoo = 0xFF; >> >> while( nFoo++ ) {}; // error during unit testing >> > >Don't quite see it: > >invariant { > assert(nFoo <= 0xFF); >} > >it's valid. > >Wrap a class that uses this for no_overflow... But won't ubyte always be <= 0xFF? Sean |
September 27, 2004 Re: Can D throw a hanging exception if a function takes too long? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean Kelly | Sean Kelly wrote:
> But won't ubyte always be <= 0xFF?
Yeah. Let's correct the idea. It would be very cool if we could have the compiler add automatic assertions which protect (in debug mode) against overflow. My thought is that, in debug mode, overflow should always be an error unless you mark a statement as expecting it. Something like this:
int i = <whatever>;
/* added automatically by compiler */ assert(i < INT_MAX);
i++;
/* no assert, since the statement expects overflow
* MIGHT happen
*/
overflow i++;
I haven't worked out all of the issues here, but this is my rough idea.
|
Copyright © 1999-2021 by the D Language Foundation