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个线程。
首先,制作Sudoku程序时有规则。
有9个3x3子节,使用9x9网格。
小节包含1至9的数字之一。
从1到9的数字在9x9网格上水平和垂直输入。
在这里,我想满足以下条件
[9个用于验证每个小节的线程,
1个用于检查列的线程,
1个用于检查行的线程,
和合成这些结果的主线程= 1
我想将线程总数更改为12。
已经有9个线程3x3,所以没有问题。
但是有9个列和行线程。
所以,我的问题是这个。
如果要将“ for Sentence”中的“ if(columnData)”和“ if(columnData)”更改为1个线程而不是9个线程,我可以将其从“ for句子”中删除吗?还是应该将其更改为+而不是x?
如果您像这样更改它,我将留下一个,但是如何创建一个主线程来汇总这些结果?您可以将其设置为数据columnData rowData吗?
对于初学者编码和英语水平很差,我感到非常抱歉。
太难了,所以我来到这里寻求帮助。
由于整个源代码为169行,因此仅使用了使用线程的主要功能部分。
最后,先感谢受访者。另外,还要感谢阅读本文的人以及发布源代码的“ Sarmad Hashmi”。
Sarmad Hashmi提供的原始源代码>>
int main(){pthread_t线程[num_threads]; int threadIndex = 0; int i,j; //为9个3x3子节创建9个线程,为9列创建9个线程,为9行创建9个线程。 // ...
该问题的一种可能的解决方案: