65.7k views
0 votes
Solve only one of 7,8 8. Use the testandSet function to create a mutex class of your own. Your class should provide lock and unlock methods and a proper constructor. By default the mutex should be open.

1 Answer

1 vote

Final answer:

To create a mutex class using the testandSet function, define a class with a boolean variable representing the state of the mutex. Implement lock and unlock methods that check and update the mutex state accordingly.

Step-by-step explanation:

To create a mutex class using the testandSet function, you can define a class with a boolean variable that represents the state of the mutex. The lock method can check if the mutex is already locked and wait until it becomes available. The unlock method can update the state of the mutex to indicate that it is unlocked again. Here's an example:

class Mutex {bool locked;public:Mutex({ locked = false; }
void lock() { while (testandSet(locked)) {
// wait until mutex is unlocked } } void unlock() { locked = false; }};

User Czimi
by
7.8k points