当前位置: 首页>>代码示例>>C++>>正文


C++ Sentence::imperfectlyTransmit方法代码示例

本文整理汇总了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:
}
开发者ID:anishasilva,项目名称:homework,代码行数:71,代码来源:telephoneGame.cpp


注:本文中的Sentence::imperfectlyTransmit方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。