pthread 読み取り/書き込みロックのテストで予期しない結果に遭遇しました。
以下は私のコードです。
#include <iostream>
#include <thread>
#include <pthread.h>
//locks declaration
pthread_rwlock_t rwlock;
//shared resource
int numbers[20];
int size = 0;
void readFrom()
{
int rc;
rc = pthread_rwlock_rdlock(&rwlock);
for(int index = 0; index < size; index++) {
std::cout << numbers[index] << " ";
}
std::cout << std::endl;
rc = pthread_rwlock_unlock(&rwlock);
}
void writeTo(int index, int val)
{
int rc;
rc = pthread_rwlock_wrlock(&rwlock);
numbers[index] = val;
size++;
rc = pthread_rwlock_unlock(&rwlock);
}
int main(int argc, char **argv)
{
int rc=0;
std::cout << std::endl;
std::thread threads[25];
rc = pthread_rwlock_init(&rwlock, NULL);
for(int i=0; i<20; ++i) {
threads[i] = std::thread(writeTo, i, i);
if(i % 5 == 0) {
threads[20 + (i / 5)] = std::thread(readFrom);
}
}
for(int i=0; i<24; ++i) {
threads[i].join();
}
std::cout << "size is " << size << std::endl;
threads[24] = std::thread(readFrom);
threads[24].join();
std::cout << std::endl;
rc = pthread_rwlock_destroy(&rwlock);
return 0;
}
何度か実行した後、時折、予期しないものがあることに気付きます。次に例を示します。
0 1 2 3 0
リーダースレッドからの出力です。基本的に、数字のサイズは今のところ5だと言っています。その場合、結果は 0 1 2 3 4 になるはずです。
ところで、追加の相互排他ロックを実装しようとしたところ、予期しない動作が発生しました。
解決策と根本原因に興味があります。誰か助けてくれませんか?
どんな助けでも事前に感謝します。