
14.3
スレッド間通信:条件変数
249
{
prodArg[i].id = i+1, prodArg[i].bufPtr = &buf;
if (thrd_create(&prod[i], producer, &prodArg[i]) != thrd_success)
{
errorExit("Thread error.");
}
}
for (i = 0; i < NC; ++i) //
消費者開始
{
consArg[i].id = i+1, consArg[i].bufPtr = &buf;
if (thrd_create(&cons[i], consumer, &consArg[i]) != thrd_success)
{
errorEx
it("Thread error.");
}
}
for (i = 0; i < NP; ++i) //
スレッド終了を待つ
{
thrd_join(prod[i], &res),
printf("\nProducer %d ended with result %d.\n", prodArg[i].id, res);
}
for (i = 0; i < NC; ++i)
{
thrd_join(cons[i], &res),
printf("Consumer %d ended with result %d.\n", consArg[i].id, res);
}
bufDestroy(&buf);
return 0;
}
int producer(void* ...