本文整理汇总了C++中Sentence::imperfectlyTransmit方法的典型用法代码示例。如果您正苦于以下问题:C++ Sentence::imperfectlyTransmit方法的具体用法?C++ Sentence::imperfectlyTransmit怎么用?C++ Sentence::imperfectlyTransmit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Sentence
的用法示例。
在下文中一共展示了Sentence::imperfectlyTransmit方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1:
/* PURPOSE: To get the necessary locks, get the sentence, print it,
* transmit it (imperfectly), and unlock and signal the next child.
*/
void* child (void* argPtr)
{
// I. Applicability validity check:
// II. Run for current child:
// II.A. Get 'index':
// YOUR CODE HERE
int index = *((int*) argPtr);
// II.B. Announce that this child is ready:
// YOUR CODE HERE
//cout << "Child " << index << " is ready" << endl;
printf("Child %d is ready.\n", index);
// II.C. Get both locks and wait until signaled (if need to):
if ( (rand() % 2) == 1)
{
// YOUR CODE HERE
pthread_mutex_lock(messageSystem.getLockPtr(index));
pthread_mutex_lock(messageSystem.getLockPtr(index-1));
}
else
{
// YOUR CODE HERE
pthread_mutex_lock(messageSystem.getLockPtr(index-1));
pthread_mutex_lock(messageSystem.getLockPtr(index));
}
// YOUR CODE HERE
//cout << "Child " << index << " got all his/her locks" << endl;
printf("Child %d got all its locks.\n", index);
while (!messageSystem.isReady(index-1)) {
printf("Child %d surrendering lock waiting for signal.\n", index);
//cout << "Child " << index << " surrendering lock waiting for signal." << endl;
pthread_cond_wait(messageSystem.getCondPtr(index-1), messageSystem.getLockPtr(index-1));
}
// II.D. Get pointer to sentence, print it and transmit it:
// YOUR CODE HERE
Sentence* sentencePtr = messageSystem.giveSentencePtr(index-1);
cout << "Child " << index << " says \"" << *sentencePtr << "\"" << endl;
sentencePtr->imperfectlyTransmit();
messageSystem.setSentencePtr(index, sentencePtr);
// II.E. Signal next child that message is ready and unlock their
// YOUR CODE HERE
pthread_mutex_unlock(messageSystem.getLockPtr(index-1));
pthread_mutex_unlock(messageSystem.getLockPtr(index));
pthread_cond_signal(messageSystem.getCondPtr(index));
// III. Finished:
}