Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
УТС 4 семестр / Lab2_Oreshchenko.docx
Скачиваний:
8
Добавлен:
08.08.2022
Размер:
374.87 Кб
Скачать

Реализация pthread_mutex_lock()

#include <iostream>

#include <pthread.h>

#include <unistd.h>

pthread_mutex_t mutex;

bool flag1 = false, flag2 = false;

void *thread1(void* arg) {

while (!flag1) {

pthread_mutex_lock(&mutex);

for (int i = 0; i < 5; i++) {

std::cout << "1" << std::endl;

sleep(1);

}//for

pthread_mutex_unlock(&mutex);

sleep(1);

}//while

return NULL;

}// thread1

void *thread2(void* arg) {

while (!flag2) {

pthread_mutex_lock(&mutex);

for (int i = 0; i < 5; i++) {

std::cout << "2" << std::endl;

sleep(1);

}//for

pthread_mutex_unlock(&mutex);

sleep(1);

}//while

return NULL;

}// thread2

int main()

{

pthread_t id1, id2;

pthread_mutex_init(&mutex, NULL);

pthread_create(&id1, NULL, &thread1, NULL);

pthread_create(&id2, NULL, &thread2, NULL);

getchar();

flag1 = flag2 = true;

pthread_join(id1, NULL);

pthread_join(id2, NULL);

pthread_mutex_destroy(&mutex);

return 0;

}//main

Демонстрация

Рис. 1. Работа программы с блокирующей операцией захвата мьютекса

pthread_mutex_lock()

Реализация pthread_mutex_trylock()

#include <iostream>

#include <pthread.h>

#include <unistd.h>

#include <string.h>

pthread_mutex_t mutex;

bool flag1 = false, flag2 = false;

void *thread1(void* arg) {

while (!flag1) {

int returnCode = pthread_mutex_trylock(&mutex);

if (returnCode != 0) {

perror(strerror(returnCode));

sleep(1);

continue;

}

for (int i = 0; i < 5; i++) {

std::cout << "1" << std::endl;

sleep(1);

}//for

std::cout << std::endl;

pthread_mutex_unlock(&mutex);

sleep(2);

}//while

return NULL;

}// thread1

void *thread2(void* arg) {

while (!flag2) {

int returnCode = pthread_mutex_trylock(&mutex);

if (returnCode != 0) {

perror(strerror(returnCode));

sleep(1);

continue;

}

for (int i = 0; i < 5; i++) {

std::cout << "2" << std::endl;

sleep(1);

}//for

std::cout << std::endl;

pthread_mutex_unlock(&mutex);

sleep(2);

}//while

return NULL;

}// thread2

int main()

{

pthread_t id1, id2;

pthread_mutex_init(&mutex, NULL);

pthread_create(&id1, NULL, &thread1, NULL);

pthread_create(&id2, NULL, &thread2, NULL);

getchar();

flag1 = flag2 = true;

pthread_join(id1, NULL);

pthread_join(id2, NULL);

pthread_mutex_destroy(&mutex);

return 0;

}//main

Демонстрация

Рис. 2. Работа программы c операцией проверки захвата мьютекса

pthread_mutex_trylock()

Реализация pthread_mutex_timedlock()

#include <iostream>

#include <pthread.h>

#include <unistd.h>

#include <string.h>

#include <time.h> // для использования функции clock_gettime

pthread_mutex_t mutex;

bool flag1 = false, flag2 = false;

void *thread1(void* arg) {

struct timespec timeoutTime;

while (!flag1) { // получаем текущее(абсолютное) время

clock_gettime(CLOCK_REALTIME, &timeoutTime); timeoutTime.tv_sec += 3; // время ожидания - 3 сек

int retVal = pthread_mutex_timedlock(&mutex, &timeoutTime);

if (retVal != 0) {

perror(strerror(retVal));

sleep(1);

continue;

}

for (int i = 0; i < 5; i++) {

std::cout << "1" << std::endl;

sleep(1);

}//for

std::cout << std::endl;

pthread_mutex_unlock(&mutex);

sleep(2);

}//while

return NULL;

}// thread1

void *thread2(void* arg) {

struct timespec timeoutTime;

while (!flag2) {

clock_gettime(CLOCK_REALTIME, &timeoutTime);

timeoutTime.tv_sec += 6; // здесь в качестве примера ожидание - 6 сек

int retVal = pthread_mutex_timedlock(&mutex, &timeoutTime);

if (retVal != 0) {

perror(strerror(retVal));

sleep(1);

continue;

}

for (int i = 0; i < 5; i++) {

std::cout << "2" << std::endl;

sleep(1);

}//for

std::cout << std::endl;

pthread_mutex_unlock(&mutex);

sleep(2);

}//while

return NULL;

}// thread2

int main()

{

pthread_t id1, id2;

pthread_mutex_init(&mutex, NULL);

pthread_create(&id1, NULL, &thread1, NULL);

pthread_create(&id2, NULL, &thread2, NULL);

getchar();

flag1 = flag2 = true;

pthread_join(id1, NULL);

pthread_join(id2, NULL);

pthread_mutex_destroy(&mutex);

return 0;

}//main

Соседние файлы в папке УТС 4 семестр