我正在尝试GLib.ThreadPool
在 Vala 中使用 s,但是在搜索 Google Code 和现有文档之后,我找不到任何使用它们的好例子。我自己尝试使用它们会导致未处理GLib.ThreadError
的 s。
例如,考虑以下 26 行,它们将整数范围的乘法线程化。
thread_multiply.vala
class Range {
public int low;
public int high;
public Range(int low, int high) {
this.low = low;
this.high = high;
}
}
void multiply_range(Range r) {
int product = 1;
for (int i=r.low; i<=r.high; i++)
product = product * i;
print("range(%s, %s) = %s\n",
r.low.to_string(), r.high.to_string(), product.to_string());
}
void main() {
ThreadPool<Range> threads;
threads = new ThreadPool<Range>((Func<Range>)multiply_range, 4, true);
for (int i=1; i<=10; i++)
threads.push(new Range(i, i+5));
}
用它们编译它们valac --thread threaded_multipy.vala
很好......但会向我发出警告。考虑到多线程的危险,这让我觉得我做错了什么,最终可能会在我的脸上爆炸。
有谁知道GLib.ThreadPool
正确使用谁?感谢您的阅读,如果您有答案,则更加感谢。
编辑:我认为可能是因为我的编译机,但不,Thread.supported()
这里评估为 true。