CS计算机代考程序代写 #include

#include
#include
#include

#include “uthread.h”
#include “uthread_mutex_cond.h”

#define PAUSE 500000

/*
* The two locks.
*/
uthread_mutex_t lockA;
uthread_mutex_t lockB;

void *thread1(void *arg)
{
uthread_mutex_lock(lockA);
usleep(random() % PAUSE);
uthread_mutex_lock(lockB);
uthread_mutex_unlock(lockB);
uthread_mutex_unlock(lockA);

return 0;
}

void *thread2(void *arg)
{
uthread_mutex_lock(lockB);
usleep(random() % PAUSE);
uthread_mutex_lock(lockA);
uthread_mutex_unlock(lockA);
uthread_mutex_unlock(lockB);

return 0;
}

int main(int argc, char **argv)
{
uthread_t t1, t2;
uthread_init(2);
lockA = uthread_mutex_create();
lockB = uthread_mutex_create();

t1 = uthread_create(thread1, 0);
t2 = uthread_create(thread2, 0);
uthread_join(t1, NULL);
uthread_join(t2, NULL);

return 0;
}