Using Discrete-Event Systems for the Automatic Generation of Concurrency Control for Dynamic Threads