我有以下任务来演示虚假共享并编写了一个简单的程序:
#include <sys/times.h>
#include <time.h>
#include <stdio.h>
#include <pthread.h>
long long int tmsBegin1,tmsEnd1,tmsBegin2,tmsEnd2,tmsBegin3,tmsEnd3;
int array[100];
void *heavy_loop(void *param) {
int index = *((int*)param);
int i;
for (i = 0; i < 100000000; i++)
array[index]+=3;
}
int main(int argc, char *argv[]) {
int first_elem = 0;
int bad_elem = 1;
int good_elem = 32;
long long time1;
long long time2;
long long time3;
pthread_t thread_1;
pthread_t thread_2;
tmsBegin3 = clock();
heavy_loop((void*)&first_elem);
heavy_loop((void*)&bad_elem);
tmsEnd3 = clock();
tmsBegin1 = clock();
pthread_create(&thread_1, NULL, heavy_loop, …Run Code Online (Sandbox Code Playgroud) 我正在尝试确定一种合理的方法,可以为每个行和列找到3,4或5的匹配.在交换两个相邻的棋子(每回合一次交换)后,玩家在游戏板中寻找相同"宝石"所在的区域(行或列),重复3-5个连续点.
以下是匹配制作移动的示例场景:
玩家移动前的棋盘(粗体是必要的交换):
ACBBC
DDBAD
DAACC
AD BBA
DCDAA
玩家移动后的棋盘(粗体结果匹配):
ACBBC
D DBAD
D AACC
D ABBA
D CDAA
在此示例中,在第一行之后的第一列中存在"D"的4匹配.我试图弄清楚如何在1)之后找到这样的比赛.棋盘在比赛开始时创建并且棋盘随机化多次以消除即时比赛,以及2.)在球员移动之后.如果工作正常,程序本身或播放器正确交换后,程序将能够检测到匹配.
我尝试过的每个算法都会导致循环超出界限和/或在交换后不正确地找到所有匹配的匹配项.通过这个,我的意思是该程序有时会尝试在数组外搜索,因为我没有成功告诉程序如何根据当前点的位置"调整"其数组搜索.即使它不会导致运行时错误,它仍然会显示不正确的结果.例如,玩家将看到该板至少显示了一个完整的匹配,这是不好的.
以下是我尝试的两个程序的解释:
以下空格.从目前的点,检查前面四个空格横跨在同一行(或更少,如果有剩余的排在不到四个空格).首先检查五场比赛,包括当前比赛; 如果没有,检查四(减去一个点); 如果没有,请检查三个(减去一个点); 如果没有,则找不到匹配项.对以下列重复相同的检查.
前面的空间. 从目前的点,检查背部四个空格横跨在同一行(或更少,如果没有在该行中的第一地点与目前现货之间的不到四个空格).首先检查五场比赛,包括当前比赛; 如果没有,检查四(减去一个点); 如果没有,请检查三个(减去一个点); 如果没有,则找不到匹配项.对上面的列重复相同的检查.
这是需要这种工作算法的主要功能.在这里使用我的Gem类(目前已经破坏了)对请求可能并不重要,所以我不会添加它,除非它有用.
bool Board::findMatches(bool scoringMove) // false if board is being setup before game
{
bool matchesFound = false;
// loops through entire board, where "size" is the width, not the number of spots
for (int i …Run Code Online (Sandbox Code Playgroud) 我只是想知道是否有可能(如果是这样,如何)在C++(Windows)中获取线程的返回值.我有几个线程,我使用WaitForMultipleObjects(...)它们.这等待一个线程完成,并返回所述线程的索引,一切都很好.但是,我希望能够获得使用其句柄完成的线程的返回值.
例如:
DWORD WINAPI Thread1(void *parameter){
...
if(...) return 0;
else return -1;
}
DWORD WINAPI Thread2(void *parameter){
...
if(...) return 1;
else return 0;
}
int main(){
HANDLE t1 = CreateThread(0, 0, Thread1, NULL, 0, 0);
HANDLE t2 = CreateThread(0, 0, Thread2, NULL, 0, 0);
HANDLE *threads = new HANDLE[2];
threads[0] = t1;
threads[1] = t2;
int result = WaitForMultipleObjects(2, threads, false, INFINITE);
if(result == 0){
//get the threads value here:
int retVal = SomeFunction(t1); //What is …Run Code Online (Sandbox Code Playgroud) 我试图从mp4视频流中提取图像.看完后,似乎正确的方法是在C++中使用Media Foundations并打开框架/读取内容.
通过文档和示例的方式很少,但经过一些挖掘,似乎有些人通过将帧读入纹理并将该纹理的内容复制到记忆可读纹理(我甚至不是确定我在这里使用正确的条款).尝试我发现的虽然给了我错误,我可能做了一堆错误.
这是我尝试做的一小段代码(项目本身附在底部).
ComPtr<ID3D11Texture2D> spTextureDst;
MEDIA::ThrowIfFailed(
m_spDX11SwapChain->GetBuffer(0, IID_PPV_ARGS(&spTextureDst))
);
auto rcNormalized = MFVideoNormalizedRect();
rcNormalized.left = 0;
rcNormalized.right = 1;
rcNormalized.top = 0;
rcNormalized.bottom = 1;
MEDIA::ThrowIfFailed(
m_spMediaEngine->TransferVideoFrame(m_spRenderTexture.Get(), &rcNormalized, &m_rcTarget, &m_bkgColor)
);
//copy the render target texture to the readable texture.
m_spDX11DeviceContext->CopySubresourceRegion(m_spCopyTexture.Get(),0,0,0,0,m_spRenderTexture.Get(),0,NULL);
m_spDX11DeviceContext->Flush();
//Map the readable texture;
D3D11_MAPPED_SUBRESOURCE mapped = {0};
m_spDX11DeviceContext->Map(m_spCopyTexture.Get(),0,D3D11_MAP_READ,0,&mapped);
void* buffer = ::CoTaskMemAlloc(600 * 400 * 3);
memcpy(buffer, mapped.pData,600 * 400 * 3);
//unmap so we can copy during next update.
m_spDX11DeviceContext->Unmap(m_spCopyTexture.Get(),0);
// and the present it …Run Code Online (Sandbox Code Playgroud) c++ windows-8 windows-runtime ms-media-foundation winrt-xaml
我很难让音频与jQuery一起工作.我用.wav和.ogg格式试过这个.(Firefox 19.0.2)
单击Start按钮会产生:
TypeError:buzzer.play不是函数
我不确定jQuery选择器是否返回一个数组,但我已尝试用两者捕获音频文件:
var buzzer = $('buzzer');
Run Code Online (Sandbox Code Playgroud)
和
var buzzer = $('buzzer')[0];
Run Code Online (Sandbox Code Playgroud)
无论如何,我无法播放音频元素.
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
</head>
<body>
<audio id="buzzer" src="buzzer.ogg" type="audio/ogg">Your browser does not support the <audio> element.</audio>
<form id='sample' action="#" data-ajax="false">
<fieldset>
<input value="Start" type="submit">
</fieldset>
</form>
<script type="text/javascript">
var buzzer = $('buzzer')[0];
$(document).on('submit', '#sample', function() {
buzzer.play();
return false;
});
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud) 我有一个接收std::vector<int>这样的函数:
void foo(std::vector<int>);
Run Code Online (Sandbox Code Playgroud)
我想foo()用动态生成的小向量反复调用。我试图使用初始化列表动态创建一个新向量,但它似乎分配了一个大小为n的向量,而不是初始化它。
例如:
foo(std::vector<int> { 1});
foo(std::vector<int> { 4});
foo(std::vector<int> { 5});
Run Code Online (Sandbox Code Playgroud)
它似乎创建了 3 个向量,分别分配给 1、4 和 5 个默认 (0) 元素。
相反,我想创建 3 个大小为 1,但值为 1、4 和 5 的向量。我知道我可以使用构造函数 (n, value),如vector<int>(1,1)、(1,4) 和 (1, 5),但就我的理解而言,我想了解为什么我的初始化列表没有按照我的期望去做。
我知道在C中不可能进行重载,我想知道:为什么类中的重载函数和类外部在C++中处理相同?
在C++中考虑这种情况,其中函数在类外声明:
foo()
foo(char c)
foo(int a, int b)
Run Code Online (Sandbox Code Playgroud)
如果C++将每个函数头视为唯一,为什么C不能这样做呢?
我认为这些可能是原因:
C中是否存在函数重载不可用的原因?
我有一个像这样的矩阵(1000 x 2830):
9178 3574 3547
160 B_B B_B A_A
301 B_B A_B A_B
303 B_B B_B A_A
311 A_B A_B A_A
312 B_B A_B A_A
314 B_B A_B A_A
Run Code Online (Sandbox Code Playgroud)
我想获得以下内容(复制colnames并拆分每列的每个元素):
9178 9178 3574 3574 3547 3547
160 B B B B A A
301 B B A B A B
303 B B B B A A
311 A B A B A A
312 B B A B A A
314 B B A B A A
Run Code Online (Sandbox Code Playgroud)
我尝试使用, …
我希望用户输入一个由以下代码扫描的数字:
scanner.nextInt();
Run Code Online (Sandbox Code Playgroud)
如果用户输入字符串,则程序会抛出InputMismatchException,这很明显.我想以这样的方式捕获异常,即程序提示用户输入输入,直到用户输入整数值.
Scanner scanner = new Scanner(System.in);
while(true) {
try {
System.out.println("Please enter a number: ");
int input = scanner.nextInt();
System.out.println(input);
//statements
break;
}
catch(InputMismatchException | NumberFormatException ex ) {
continue;
}
}
Run Code Online (Sandbox Code Playgroud)
如果输入字符串,此代码将创建无限循环.
java java.util.scanner numberformatexception inputmismatchexception