
Добавил:
chachshin_denis
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:1 лаба / LR_1
.cpp#include "iostream"
#include "pthread.h"
using namespace std;
void* potok1(void* flag1)
{
int *n = (int*)flag1;
int *i;
i = new int;
*i = 0;
while(*n == 0)
{
(*i)++;
}
pthread_exit(i);
}
void* potok2(void* flag2)
{
int *n = (int*)flag2;
int *i;
i = new int;
*i = 0;
while (*n == 0)
{
(*i)++;
}
pthread_exit(i);
}
int main()
{
cout << "Программа начала работу\n";
int flag1 = 0;
int flag2 = 0;
pthread_t id1, id2;
int ret_val = pthread_create(&id1, NULL, potok1, &flag1);
if (ret_val == 0)
{
cout << "Поток №1 начал работу\n";
}
else
{
perror("pthread_create");
}
ret_val = pthread_create(&id2, NULL, potok2, &flag2);
if (ret_val == 0)
{
cout << "Поток №2 начал работу\n";
}
else
{
perror("pthread_create");
}
cout << "Нажмите Enter\n";
getchar();
cout << "Клавиша нажата\n";
*(&flag1) = *(&flag2) = 1;
void *res1, *res2;
pthread_join(id1, &res1);
pthread_join(id2, &res2);
int *result1 = (int*)res1;
int *result2 = (int*)res2;
cout << "Количество выполненных итераций в первом потоке: " << *result1 << "\n";
cout << "Количество выполненных итераций во втором потоке: " << *result2 << "\n";
delete result1;
delete result2;
cout << "Программа завершила работу\n";
return 0;
}