Vala线程池的正确用法是什么?

Jje*_*jed 3 multithreading glib compiler-warnings vala threadpool

我正在尝试GLib.ThreadPool在Vala中使用s,但在搜索Google Code和现有文档后,我找不到任何好用的例子.我自己尝试使用它们导致未处理的GLib.ThreadErrors.

例如,考虑以下26行,它们是整数范围的乘法.

threaded_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));
}
Run Code Online (Sandbox Code Playgroud)

编译它们的valac --thread threaded_multipy.vala工作正常......但是向我发出警告.考虑到多线程的危险,这让我觉得我做错了什么,最终可能会在我脸上爆炸.

有谁知道谁GLib.ThreadPool正确使用?感谢您的阅读,更感谢您的回答.

编辑:我想可能是因为我的编译机器,但没有,Thread.supported()在这里评估为true.

lhw*_*lhw 5

我没看到你的代码有什么问题.编译器警告是关于不捕获ThreadErrors.你可能应该做什么.只需添加一个尝试并捕获如下:

 try {
    threads = new ThreadPool<Range>((Func<Range>)multiply_range, 4, true);
    for (int i=1; i<=10; i++)
    threads.push(new Range(i, i+5));
  }
 catch(ThreadError e) {
    //Error handling
    stdout.printf("%s", e.message);
 }
Run Code Online (Sandbox Code Playgroud)