Skip to content

Commit d4a5414

Browse files
First exercise updated
1 parent 4b39f81 commit d4a5414

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed

Ex1/ex1_c

9.99 KB
Binary file not shown.

Ex1/ex1_c.c

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <pthread.h>
2+
#include <stdio.h>
3+
4+
int i = 0;
5+
6+
7+
void* inc(){
8+
for(int j = 0; j < 1000000; j++){
9+
i++;
10+
}
11+
return NULL;
12+
}
13+
14+
void* dec(){
15+
for (int j = 0; j < 1000000; j++){
16+
i--;
17+
}
18+
return NULL;
19+
}
20+
21+
int main(){
22+
pthread_t thread_inc;
23+
pthread_t thread_dec;
24+
25+
pthread_create(&thread_inc, NULL, inc, NULL);
26+
pthread_create(&thread_dec, NULL, dec, NULL);
27+
28+
pthread_join(thread_inc, NULL);
29+
pthread_join(thread_dec, NULL);
30+
31+
printf("Result: %d\n", i);
32+
33+
return 0;
34+
}

Ex1/ex1_go.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"runtime"
6+
"time"
7+
)
8+
9+
var i int = 0
10+
11+
func inc(){
12+
for j := 0; j < 1000000; j++ {
13+
i++
14+
}
15+
}
16+
17+
func dec(){
18+
for j := 0; j < 1000000; j++ {
19+
i--
20+
}
21+
}
22+
23+
func main() {
24+
runtime.GOMAXPROCS(runtime.NumCPU())
25+
26+
go inc()
27+
go dec()
28+
29+
time.Sleep(100 * time.Millisecond)
30+
fmt.Printf("Result: %d\n", i)
31+
}

Ex1/ex1_py.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from threading import Thread
2+
3+
i = 0
4+
5+
def thread_inc():
6+
global i
7+
for j in range(1000000):
8+
i += 1
9+
10+
def thread_dec():
11+
global i
12+
for p in range(1000000):
13+
i -= 1
14+
15+
def main():
16+
inc = Thread(target = thread_inc, args = (),)
17+
dec = Thread(target = thread_dec, args = (),)
18+
inc.start()
19+
dec.start()
20+
inc.join()
21+
dec.join()
22+
print(i)
23+
24+
main()

0 commit comments

Comments
 (0)