在一个多线程的C++程序中,我在一个线程中运行相当于这个:
while(obj->member) { } // waiting for obj->member to be set to false in another thread
Run Code Online (Sandbox Code Playgroud)
在另一个线程中,obj-> member设置为false.但是,即使将其设置为false,循环也不会中断.如果我改成它:
while(obj->member) { Sleep(1) }
Run Code Online (Sandbox Code Playgroud)
当obj-> member在另一个线程中设置为false时,它按预期工作并中断.
它为什么这样工作?
有下面的shell,它在(while true)循环中有一个(while true)循环.我试图用"休息"打破内循环,但事实并非如此.我想打破内循环并向用户显示第一循环的选项; 建议请.
#!/bin/ksh
sub_menu() {
echo "~~~~~~~~~~~~~~~~~~~~~~~~~"
echo " S U B - M E N U "
echo "~~~~~~~~~~~~~~~~~~~~~~~~~"
echo "1. Display properties"
echo "2. Back"
}
read_sub_options(){
echo "Please select option"
read option
case $option in
1) sub_menu ;;
***2) break ;;***
*) echo "Please insert options 1 ~ 2";;
esac
}
showSubMenu(){
while true
do
sub_menu
read_sub_options
done
}
read_options(){
echo "Please select option "
read option
case $option in
1) showSubMenu ;;
2) exit …Run Code Online (Sandbox Code Playgroud) 我需要逐行搜索文件中的两个特定单词,如果它们存在,则打印"Found!".
这是file.txt(有四列)
bill gates 62bill microsoft
beyonce knowles 300mill entertainment
my name -$9000 student
Run Code Online (Sandbox Code Playgroud)
以下是我的想法,但它似乎不起作用
char firstname[];
char lastname[];
char string_0[256];
file = fopen("file.txt","r+");
while((fgets(string_0,256,file)) != NULL) {
//scans the line then sets 1st and 2nd word to those variables
fscanf(file,"%s %s",&firstname, &lastname);
if(strcmp(firstname,"beyonce")==0 && strcmp(lastname,"knowles")==0){
printf("A match has been found");
}
}
fclose(file);
Run Code Online (Sandbox Code Playgroud)
请帮忙.可能是指针没有移动到while循环中的下一行吗?如果是这样,我该如何解决?
我正在使用while循环读取串行数据.但是,我无法控制采样率.
代码本身似乎需要花费0.2秒才能运行,所以我知道我将无法以更快的速度运行.但我希望能够准确控制我采样的速度.
我觉得我可以使用'sleep'来做到这一点,但问题是有可能在不同的点上循环本身需要更长的时间来读取(取决于通过串行数据传输的精确内容),所以代码会有弥补平衡.
例如,假设我想每1秒采样一次,并且循环需要0.2s到0.3s才能运行.我的代码需要足够智能才能睡眠0.8秒(如果循环需要0.2秒)或0.7秒(如果循环需要0.3秒).
import serial
import csv
import time
#open serial stream
while True:
#read and print a line
sample_value=ser.readline()
sample_time=time.time()-zero
sample_line=str(sample_time)+','+str(sample_value)
outfile.write(sample_line)
print 'time: ',sample_time,', value: ',sample_value
Run Code Online (Sandbox Code Playgroud) 我是python的初学者,使用v2.7.2这是我试图在命令提示符下执行的
p = 2
while(p>0):
for i in range(10):
print i+1 , p
p-=1
Run Code Online (Sandbox Code Playgroud)
该预期产量
1 2
2 1
Run Code Online (Sandbox Code Playgroud)
但实际输出是
1 2
2 1
3 0
4 -1
5 -2
6 -3
7 -4
8 -5
9 -6
10 -7
Run Code Online (Sandbox Code Playgroud)
为什么会这样?以及我如何实现预期的行为?
这是一个非常主观的问题,所以我会更具体.有没有时间do-while循环比正常的while循环更好的编码风格?
例如
int count = 0;
do {
System.out.println("Welcome to Java");
count++;
} while (count < 10);`
Run Code Online (Sandbox Code Playgroud)
在评估do语句(也就是强制do语句至少运行一次)之后检查while条件似乎没有意义.
对于像我上面的例子那样简单的东西,我会想象:
int count = 0;
while(count < 10) {
System.out.println("Welcome to Java"); count++;
}
Run Code Online (Sandbox Code Playgroud)
通常被认为是以更好的写作风格写成的.
任何人都可以为我提供一个工作示例,说明什么时候将do-while循环视为唯一/最佳选择?你的代码中有do-while循环吗?它起什么作用?你为什么选择do-while循环?
(我有一种深刻的感觉,do-while循环可能在编码游戏中有用.如果我错了,请纠正我,游戏开发者!)
char player_select(void){
char player = 'n';
while(player == 'n'){
printf("Select your player (X or O): ");
scanf("%c\n", &player);
if(player != 'X' && player != 'O'){
printf("Invalid input. Try again.\n");
player = 'n';
}
}
printf("Your character input is: %c\n", player);
exit(0);
return player;
}
Run Code Online (Sandbox Code Playgroud)
我在这里得到一些奇怪的输出:
选择你的播放器(X或O):X
输入无效.再试一次.
选择你的播放器(X或O):i
你的角色输入是:X
我是Java我可以写这个:
Matcher m;
while ((m = pattern.matcher(string)).matches()) {...}
Run Code Online (Sandbox Code Playgroud)
我如何在Scala中执行此操作?这不起作用:
var m: Matcher = null
while ((m = pattern.matcher(s)).matches()) {}
Run Code Online (Sandbox Code Playgroud) 我是Java编程的新手.目前我正在研究一个名为Evercam的Android应用程序的源代码.但是,我在理解涉及while(true)循环的代码的一部分时遇到了问题.
这是代码的片段:
while (true)
{
while (true)
{
byte[] responseMessageByteArray = new byte[4000];
DatagramPacket datagramPacketRecieve = new DatagramPacket(responseMessageByteArray, responseMessageByteArray.length);
datagramSocket.receive(datagramPacketRecieve);
String responseMessage = new String(datagramPacketRecieve.getData());
EvercamDiscover.printLogMessage("\nResponse Message:\n" + responseMessage);
StringReader stringReader = new StringReader(responseMessage);
InputNode localInputNode = NodeBuilder.read(stringReader);
EnvelopeProbeMatches localEnvelopeProbeMatches = (EnvelopeProbeMatches)(new Persister()).read(EnvelopeProbeMatches.class, localInputNode);
if (localEnvelopeProbeMatches.BodyProbeMatches.ProbeMatches.listProbeMatches.size() > 0)
{
ProbeMatch localProbeMatch = (ProbeMatch) localEnvelopeProbeMatches.BodyProbeMatches.ProbeMatches.listProbeMatches.get(0);
if (uuidArrayList.contains(localProbeMatch.EndpointReference.Address))
{
EvercamDiscover.printLogMessage("ONVIFDiscovery: Address " + localProbeMatch.EndpointReference.Address + " already added");
}
else
{
uuidArrayList.add(localProbeMatch.EndpointReference.Address);
DiscoveredCamera discoveredCamera = getCameraFromProbeMatch(localProbeMatch);
if (discoveredCamera.hasValidIpv4Address())
{
this.onActiveOnvifDevice(discoveredCamera);
cameraList.add(discoveredCamera);
} …Run Code Online (Sandbox Code Playgroud) 我有一个问题,我需要(至少可以肯定)通过整个列表来解决.问题是找出列表中连续数字的最大数量,该数字加起来列表中的另一个(更大)元素.如果没有,那么我们只需将列表中的最大值作为候选求和,将1作为最大连续元素数.
我的通用代码有效,但对于大型列表(> 500,000个元素)则不太好.我只是在寻找有关如何以不同方式解决问题的提示.我目前的做法:
L = [1,2,3,4,5,6,7,8,9,10]
candidate_sum = L[-1]
largest_count = 1
N = len(L)
i = 0
while i < N - 1:
s = L[i]
j = 0
while s <= (N - L[i + j + 1]):
j += 1
s += L[i+j]
if s in L and (j+1) > largest_count:
largest_count = j+1
candidate_sum = s
i+=1
Run Code Online (Sandbox Code Playgroud)
在这种情况下,答案是[1,2,3,4],因为它们加起来为10,长度为4(显然这个例子L是一个非常简单的例子).
然后我通过将初始while循环条件更改为:
while i < (N-1)/largest_count
Run Code Online (Sandbox Code Playgroud)
这不是一个很好的假设,但基本认为数字的分布有些统一,因此列表后半部分的两个数字平均大于列表中的最终数字,因此被取消资格.
我只是在寻找: