Я пробовал базовую программу pthreads / mutex:
#include<iostream>
#include<pthread.h>
using namespace std;
pthread_mutex_t mutex;
void* PrintHello(void *t)
{
int i = reinterpret_cast<int>(t);
pthread_mutex_lock(&mutex);
cout<<"Hello, World thread <"<<i<<">!"<<endl;
pthread_mutex_unlock(&mutex);
pthread_exit(NULL);
}
int main()
{
pthread_t threadId[5];
pthread_mutex_init(&mutex, NULL);
for(int i = 0; i < 5; i ++)
{
int rc = pthread_create(&threadId[i], NULL, PrintHello, reinterpret_cast<void *>(i + 1));
}
return 0;
}
И я получаю следующий вывод:
Выполнение 1:
Hello, World thread <1>!
Hello, World thread <2>!
Hello, World thread <3>!
Hello, World thread <4>!
Hello, World thread <4>!
Hello, World thread <5>!
Выполнение 2:
Hello, World thread <1>!
Hello, World thread <2>!
Hello, World thread <3>!
Hello, World thread <4>!
Hello, World thread <5>!
Hello, World thread <5>!
Я ожидал, что всегда будет пять «Привет, мир!»печатает как вывод этой программы, но я вижу разные.Кто-нибудь может сказать мне, почему?