0

I would like a little help if possible.

I need to find the time in seconds to execute the tasks.

I'm still learning to program in Python, my code below is not working correctly. What could I modify or correct in it?

import threading 
import time

x = 0
turn = 0


def entra_cs(num_tasks):
    global turn
    while turn != num_tasks:
        pass


def sai_cs(num_tasks):
    global turn
    turn = (turn + 1) % num_tasks


def incrementa():
    global x
    x = x + 1


def thread_task(num_tasks):
    for _ in range(10):
        entra_cs(num_tasks)
        incrementa()
        sai_cs(num_tasks)


def main_task():
    global x
    x = 0

    t1 = threading.Thread(target=thread_task, args=(1,))
    t2 = threading.Thread(target=thread_task, args=(2,))

    t1.start()
    t2.start()

    t1.join()
    t2.join()


if __name__ == "__main__":
    comeco = time.time()
    for i in range(10):
        main_task()
        print("Iteration {0}: x = {1}".format(i, x))
    fim = time.time()
    print('Duration in seconds -', comeco - fim)

0 Answers0