线程数独程序调整线程数在27到12
Thread Sudoku program Adjust the number of threads in 27 to 12
int main() {
pthread_t threads[num_threads];
int threadIndex = 0;
int i,j;
// Create 9 threads for 9 3x3 subsections, 9 threads for 9 columns and 9 threads for 9 rows.
// This will end up with a total of 27 threads.
for (i = 0; i < 9; i++) {
for (j = 0; j < 9; j++) {
if (i%3 == 0 && j%3 == 0) {
parameters *data = (parameters *) malloc(sizeof(parameters));
data->row = i;
data->column = j;
pthread_create(&threads[threadIndex++], NULL, is3x3Valid, data); // 3x3 subsection threads
}
if (i == 0) {
parameters *columnData = (parameters *) malloc(sizeof(parameters));
columnData->row = i;
columnData->column = j;
pthread_create(&threads[threadIndex++], NULL, isColumnValid, columnData); // column threads
}
if (j == 0) {
parameters *rowData = (parameters *) malloc(sizeof(parameters));
rowData->row = i;
rowData->column = j;
pthread_create(&threads[threadIndex++], NULL, isRowValid, rowData); // row threads
}
}
}
for (i = 0; i < num_threads; i++) {
pthread_join(threads[i], NULL); // Wait for all threads to finish
}
// If any of the entries in the valid array are 0, then the sudoku solution is invalid
for (i = 0; i < num_threads; i++) {
if (valid[i] == 0) {
printf("Sudoku solution is invalid!\n");
return EXIT_SUCCESS;
}
}
printf("Sudoku solution is valid!\n");
return EXIT_SUCCESS;
}
可以看到,这个程序一共有27个线程。
首先,制作数独程序是有规律的。
共有9个3x3小节,使用9x9网格。
小节包含1~9的其中一个数。
在 9x9 网格上水平和垂直输入 1 到 9 的数字。
在此,我想满足以下条件
9个线程来验证每个小节,
1 个线程检查专栏,
1 个线程检查行,
以及综合这些结果的主线程=1
我想把总线程数改成12
已经有 9 个线程 3x3,所以没有问题。
但是,有 9 列和行线程。
所以,我的问题是这样的。
如果要将 "for Sentence" 中的 "if(columnData)" 和 "if(columnData)" 更改为 1 个线程而不是 9 个线程,我可以将其从 "for sentence" 中取出吗?
或者我应该将其更改为 + 而不是 x?
如果你这样改变它我已经留下了一个,但是你如何创建一个主线程来合并这些结果?
你能把它做成datacolumnDatarowData吗?
真的很抱歉初学者编码+英语水平差。
太难了,所以才来这里求助
由于整个源码169行,所以只取使用线程的main函数部分
最后,提前感谢受访者。
另外,感谢阅读本文的人 post 和发布源代码的 "Sarmad Hashmi"。
原始源代码来自 Sarmad Hashmi
问题的一种可能解决方案:
将整个 9x9 板传递给每个线程函数。让线程函数提取rows
或columns
或sub grids
那么只需要三个线程
每个循环9次。
建议每个线程 return(通过 pthread_exit()
该线程的最终状态,因此只需要检查 3 个 returned 值。
这将允许消除对 malloc()
、结构 parameters
等
的调用
注意:引用的代码未能将每个 malloc'd
内存区域传递给 free()
,因此引用的代码有 27 个内存泄漏
int main() {
pthread_t threads[num_threads];
int threadIndex = 0;
int i,j;
// Create 9 threads for 9 3x3 subsections, 9 threads for 9 columns and 9 threads for 9 rows.
// This will end up with a total of 27 threads.
for (i = 0; i < 9; i++) {
for (j = 0; j < 9; j++) {
if (i%3 == 0 && j%3 == 0) {
parameters *data = (parameters *) malloc(sizeof(parameters));
data->row = i;
data->column = j;
pthread_create(&threads[threadIndex++], NULL, is3x3Valid, data); // 3x3 subsection threads
}
if (i == 0) {
parameters *columnData = (parameters *) malloc(sizeof(parameters));
columnData->row = i;
columnData->column = j;
pthread_create(&threads[threadIndex++], NULL, isColumnValid, columnData); // column threads
}
if (j == 0) {
parameters *rowData = (parameters *) malloc(sizeof(parameters));
rowData->row = i;
rowData->column = j;
pthread_create(&threads[threadIndex++], NULL, isRowValid, rowData); // row threads
}
}
}
for (i = 0; i < num_threads; i++) {
pthread_join(threads[i], NULL); // Wait for all threads to finish
}
// If any of the entries in the valid array are 0, then the sudoku solution is invalid
for (i = 0; i < num_threads; i++) {
if (valid[i] == 0) {
printf("Sudoku solution is invalid!\n");
return EXIT_SUCCESS;
}
}
printf("Sudoku solution is valid!\n");
return EXIT_SUCCESS;
}
可以看到,这个程序一共有27个线程。
首先,制作数独程序是有规律的。
共有9个3x3小节,使用9x9网格。
小节包含1~9的其中一个数。
在 9x9 网格上水平和垂直输入 1 到 9 的数字。
在此,我想满足以下条件
9个线程来验证每个小节,
1 个线程检查专栏,
1 个线程检查行,
以及综合这些结果的主线程=1
我想把总线程数改成12
已经有 9 个线程 3x3,所以没有问题。
但是,有 9 列和行线程。
所以,我的问题是这样的。
如果要将 "for Sentence" 中的 "if(columnData)" 和 "if(columnData)" 更改为 1 个线程而不是 9 个线程,我可以将其从 "for sentence" 中取出吗? 或者我应该将其更改为 + 而不是 x?
如果你这样改变它我已经留下了一个,但是你如何创建一个主线程来合并这些结果? 你能把它做成datacolumnDatarowData吗?
真的很抱歉初学者编码+英语水平差。
太难了,所以才来这里求助
由于整个源码169行,所以只取使用线程的main函数部分
最后,提前感谢受访者。 另外,感谢阅读本文的人 post 和发布源代码的 "Sarmad Hashmi"。
原始源代码来自 Sarmad Hashmi
问题的一种可能解决方案:
将整个 9x9 板传递给每个线程函数。让线程函数提取rows
或columns
或sub grids
那么只需要三个线程
每个循环9次。
建议每个线程 return(通过 pthread_exit()
该线程的最终状态,因此只需要检查 3 个 returned 值。
这将允许消除对 malloc()
、结构 parameters
等
注意:引用的代码未能将每个 malloc'd
内存区域传递给 free()
,因此引用的代码有 27 个内存泄漏