Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C or C++ program using POSIX threads and mutexes and condition variables

ID: 3644295 • Letter: W

Question

Write a C or C++ program using POSIX threads and mutexes and condition variables , which implements a solution to the Media Playback Control Problem (described below) that is free from deadlock.

The Media Playback Control Problem

Consider a system with three control threads and one player thread. The player thread plays back a media stream, consisting of some digital medium such as digital audio or video (or, in our case, text). The player has two modes: playing, and stopped. The player thread maintains a current media position, which changes periodically when the player is playing, and can be affected by commands such as Rewind(). The player accepts the following commands:
LoadFile(const char *filename) -- Prepares the player to playback the file whose name is passed in. Stops the player if currently playing.
Stop() -- Stops the player if currently playing.
Start() -- Starts the player if currently stopped.
Rewind() -- Sets the position of the player to time 0.
SeekTo(int byteoffset) -- Sets the position of the player to the closest word boundary past the given byte offset from the start of the file.
SetRate(float rate) -- Sets the playback rate: 1.0 is forward at normal speed, -1.0 is reverse, 0.5 is forward at 1/2 speed, etc.

there is another part of the problem need help with (ill post when this is solved)

Explanation / Answer

Solution:

Here are some functional blocks which you mus include in to your project code.Hop this helps you.

std::atomic<int> mutex;
int data;
void thread1()
{
// simple spin-mutex
while (mutex.exchange(1, std::memory_order_acquire))
std::this_thread::yield();
data = 1;
mutex.store(0, std::memory_order_release);
}
void thread2()
{
// simple spin-mutex
while (mutex.exchange(1, std::memory_order_acquire))
std::this_thread::yield();
data = 2;
mutex.store(0, std::memory_order_relaxed);
}

//

struct node
{
rl::atomic<node*> next;
rl::var<void*> data;
};
struct stack
{
rl::atomic<node*> head;
};
void push(stack* s, void* data)
{
node* n = RL_NEW(node);
n->data($) = data;
node* next = s->head($).load(rl::memory_order_relaxed);
for (;;)
{
n->next($).store(next, rl::memory_order_relaxed);
if (s->head($).compare_exchange_weak(
next, n, rl::memory_order_release))
break;
}
}
void* pop(stack* s)
{
node* n = s->head($).load(rl::memory_order_relaxed);
for (;;)
{
if (0 == n)
return 0;
node* next = n->next($).load(rl::memory_order_relaxed);
if (s->head($).compare_exchange_weak(
n, next, rl::memory_order_acquire))
break;
}
void* data = n->data($);
RL_DELETE(n);
return data;
}

//unit-test for RRD:

struct test : rl::test_suite<test, 2>
{
stack s;

// is executed in one thread
// before execution of the main function of threads
void before()
{
s.head($) = 0;
}
// the main function of threads
void thread(unsigned /*thread_index*/)
{
push(&s, (void*)1);
void* data = pop(&s);
RL_ASSERT(data == (void*)1);
}
};
int main()
{
rl::simulate<test>();
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote