Добавил:
Andrew1992
Факультет ИКСС, группа ИКВТ-61
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
static volatile int counter = 0;
static pthread_mutex_t myMutex;
void *mutex_testing(void *param)
{
int i;
for(i = 0; i < 5; i++)
{
pthread_mutex_lock(&myMutex);
counter++;
sleep(1);
printf("thread [%i] entered\tcounter = %i\n", *reinterpret_cast<int*>(param), counter);
pthread_mutex_unlock(&myMutex);
}
}
int main()
{
int one = 1, two = 2, three = 3;
pthread_t thread1, thread2, thread3;
pthread_mutex_init(&myMutex,nullptr);
pthread_create(&thread1, nullptr, mutex_testing, reinterpret_cast<void*>(&one));
pthread_create(&thread2, nullptr, mutex_testing, reinterpret_cast<void*>(&two));
pthread_create(&thread3, nullptr, mutex_testing, reinterpret_cast<void*>(&three));
pthread_join(thread1, nullptr);
pthread_join(thread2, nullptr);
pthread_join(thread3, nullptr);
pthread_mutex_destroy(&myMutex);
return 0;
}
Соседние файлы в папке WORK_5