根据我的理解,如果两个或多个线程同时尝试访问同一个内存块,它应该"抱怨",至少可以说.
我正在为一个计算回文的课程编写一个程序(在列表中出现向后和向前的单词也算在内).在我的多线程解决方案中,我生成了26个线程来处理字母表中的每个字母
int error = pthread_create(&threads[i], NULL, computePalindromes, args);
计算回文只是贯穿单词子列表:
void * computePalindromes(void * arguments) {
struct arg_struct *args = (struct arg_struct *)arguments;
int i;
for (i = args->start; i < args->end; i++) {
if (quickFind(getReverse(array[i]), 0, size - 1)) {
printf("%s\n", array[i]);
}
}
return NULL;
}
Run Code Online (Sandbox Code Playgroud)
现在,应该导致程序停止的段.我修改了quickSelect来查找列表中的反向字.
int quickFind(char * string, int lower_bound, int upper_bound) {
int index = ((upper_bound + lower_bound) / 2);
//sem_wait(&semaphores[index]);
if (upper_bound <= lower_bound) return (strcmp(string, array[index]) == 0);
if (strcmp(string, array[index]) > …Run Code Online (Sandbox Code Playgroud) 下面的代码遇到了问题java.util.ConcurrentModificationException.有什么方法可以防止这种情况或允许这种情况吗?
public void saveHomes() throws IOException {
BufferedWriter br;
br = new BufferedWriter(new FileWriter(homeFile));
Map<String, Location> homesLoc;
System.out.println(homes2.keySet());
for (String player : homes2.keySet()) {
homesLoc = homes2.get(player);
for (String name : homesLoc.keySet()) {
br.write(player + " " + homesLoc.get(name) + " " + name);
br.newLine();
br.flush();
}
}
br.close();
}
Run Code Online (Sandbox Code Playgroud) void RdImage(FILE *fpi, char Image1[MAXROW][MAXCOL], int Nrows, int Ncols) {
int i = 0, j = 0, temp;
while (!feof(fpi)) {
if (i % Nrows == 0) {
i = 0;
j++;
}
**fscanf(fpi, "%d", temp);**
if (temp == 1) {
Image1[i][j] == AP;
} else {
Image1[i][j] == PL;
}
i++;
}
}
Run Code Online (Sandbox Code Playgroud)
我用星号包围的那条线给了我一个分段错误.该文件绝对不是空的.我在我的程序中的其他地方使用过两次相同的行,并且它的行为并不像那样.
(Math.random() > 0.75) ? type = TreeType.BIG_TREE : type = TreeType.TREE
Run Code Online (Sandbox Code Playgroud)
为什么(Math.random()> 0.75)根据控制台而不是变量返回一个值?以上陈述是if-else阶梯的一部分.