Thread Verzögerung von Ereignissen (17 answers)
Opened by piet at 2015-06-02 16:18

hlubenow
 2015-06-04 01:37
#181251 #181251
User since
2009-02-22
875 Artikel
BenutzerIn
[default_avatar]
Guest piet
Wenn also [also = schönes Wort :-) ] $i > 100 ist/wird, starten/laufen die z.B. 5 Sekunden. d.h. Bedingung für den Timer ist immer die Bedingung $i > 100
Als Rückgabewert bringt der timer_start("timer_1", 5s)
1. 0=Zeit läuft noch
2. 1=Zeit ist abgelaufen
Während die Zeit läuft bringt die timer_restzeit("timer_1") die Restzeit zurück

Merkst Du, daß Du zwei Programmsteuerungen gleichzeitig verwenden möchtest?
Eine Klasse allein macht das nicht, dazu braucht man wie gesagt Threads.
So, ich hab' jetzt mal ein Beispiel geschrieben. Sorry für die globale Variable @results. Wußte nicht, wie ich ein Array in einem Klassen-"$self"-Hash mit einem Thread "shared" bekomme (das war Denglisch). Mein erster Versuch mit Perl-Threads. Also
Code (perl): (dl )
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/perl

use warnings;
use strict;

use 5.14.0;
use threads;
use threads::shared;
use Time::HiRes qw(usleep);

my @results = ();

package Timer;

    sub new {
        my $classname = shift;
        my $self = {comparevalue => 0,
                    limit => 100,
                    timerstart => 5,
                    state => "off",
                    round => 0,
                    stopthread => 0}; 
        return bless($self, $classname);
    }   

    sub startThread {
        my $self = shift;
        $self->{timer} = $self->{timerstart};
        threads::shared::share($self->{timerstart});
        threads::shared::share($self->{timer});
        threads::shared::share($self->{comparevalue});
        threads::shared::share($self->{limit});
        threads::shared::share($self->{state});
        threads::shared::share($self->{round});
        threads::shared::share($self->{stopthread});
        threads::shared::share(@results);
        $self->{thread} = threads->create(sub { $self->myThread() });
    }

    sub myThread {
        my $self = shift;
        while ($self->{stopthread} == 0) {
            if ($self->{comparevalue} > $self->{limit}) {
                $self->{timer} = $self->{timerstart};
                my $result = $self->runLoop();
                push(@results, $result);
            }
        }
    }

    sub runLoop {
        my $self = shift;
        $self->{round}++;
        $self->{state} = "on";
        while ($self->{timer} >= 0) {
            if ($self->{comparevalue} <= $self->{limit}) {
                $self->{state} = "off";
                return 0;
            }
            Time::HiRes::usleep(100000);
            $self->{timer} -= 0.1;
            $self->{timer} = sprintf("%.1f", $self->{timer});
        }
        $self->{state} = "off";
        return 1;
    }

    sub getAktZeit {
        my $self = shift;
        return $self->{timer};
    }

    sub getStatus {
        my $self = shift;
        return $self->{state};
    }

    sub getRound {
        my $self = shift;
        return $self->{round};
    }

    sub setCompareValue {
        my $self = shift;
        $self->{comparevalue} = shift;
    }

    sub stopThread {
        my $self = shift;
        $self->{stopthread} = 1;
        $self->{thread}->detach();
    }


package main;

my $i = 0;

my $timer = Timer->new();
$timer->startThread();

my $u;
print "\$i\tTimerStatus\tTimerrestzeit\tTimerrunde\n";
for ($u = 0; $u <= 100; $u++) {
    $timer->setCompareValue($i);
    print "$i\t\t";
    print $timer->getStatus() . "\t\t";
    print $timer->getAktZeit() . "\t\t";
    print $timer->getRound() . "\n";
    Time::HiRes::usleep(200000);
    if ($u == 5 || $u == 40) {
        $i = 105;
    }
    if ($u == 20) {
        $i = 10;
    }
} 

$timer->stopThread();

print "\nErgebnisse:\n";
print "Timerrunde\tErgebnis\n";
for $u (0 .. $#results) {
    print $u + 1;
    print "\t\t$results[$u]\n";
}

View full thread Verzögerung von Ereignissen