Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
11
Task/Mutex/D/mutex-1.d
Normal file
11
Task/Mutex/D/mutex-1.d
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
class Synced
|
||||
{
|
||||
public:
|
||||
synchronized int func (int input)
|
||||
{
|
||||
num += input;
|
||||
return num;
|
||||
}
|
||||
private:
|
||||
static num = 0;
|
||||
}
|
||||
46
Task/Mutex/D/mutex-2.d
Normal file
46
Task/Mutex/D/mutex-2.d
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
import tango.core.Thread, tango.io.Stdout, tango.util.log.Trace;
|
||||
|
||||
class Synced {
|
||||
public synchronized int func (int input) {
|
||||
Trace.formatln("in {} at func enter: {}", input, foo);
|
||||
// stupid loop to consume some time
|
||||
int arg;
|
||||
for (int i = 0; i < 1000*input; ++i) {
|
||||
for (int j = 0; j < 10_000; ++j) arg += j;
|
||||
}
|
||||
foo += input;
|
||||
Trace.formatln("in {} at func exit: {}", input, foo);
|
||||
return arg;
|
||||
}
|
||||
private static int foo;
|
||||
}
|
||||
|
||||
void main(char[][] args) {
|
||||
SimpleThread[] ht;
|
||||
Stdout.print( "Starting application..." ).newline;
|
||||
|
||||
for (int i=0; i < 3; i++) {
|
||||
Stdout.print( "Starting thread for: " )(i).newline;
|
||||
ht ~= new SimpleThread(i+1);
|
||||
ht[i].start();
|
||||
}
|
||||
|
||||
// wait for all threads
|
||||
foreach( s; ht )
|
||||
s.join();
|
||||
}
|
||||
|
||||
class SimpleThread : Thread
|
||||
{
|
||||
private int d_id;
|
||||
this (int id) {
|
||||
super (&run);
|
||||
d_id = id;
|
||||
}
|
||||
|
||||
void run() {
|
||||
auto tested = new Synced;
|
||||
Trace.formatln ("in run() {}", d_id);
|
||||
tested.func(d_id);
|
||||
}
|
||||
}
|
||||
11
Task/Mutex/D/mutex-3.d
Normal file
11
Task/Mutex/D/mutex-3.d
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
class Synced {
|
||||
public int func (int input) {
|
||||
synchronized(Synced.classinfo) {
|
||||
// ...
|
||||
foo += input;
|
||||
// ...
|
||||
}
|
||||
return arg;
|
||||
}
|
||||
private static int foo;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue