0

我正在编写一个程序,它读取不同线程中的部分数据,然后在其中找到一个模式。

但在此之前(问题出现在下面的代码中)我只是想检查线程是否正确取消。这就是我卡住的地方。看到错误需要时间,但基本上在(有时 40 次)启动我的程序之后,它会挂在主线程的 while 循环中

while (alive_threads) { //never ends, because alive_threads==1, but pthread_cleanup_pop should make sure that all canceling threads reduces counter and as a result reduce it to 0
 usleep(1000);
}

...等待最后一个线程取消。

代码在这里: http: //pastebin.com/VqRrhXPD

我还提供了我正在读取的文件的内容。

开幕:

./a.out 20 test.txt 40 clean

20 -number of threads

40 -portion of data
4

1 回答 1

3

您正在修改全局变量alive_threads而没有任何内存同步。这会调用未定义的行为;很可能,它会导致一些减量丢失。

于 2012-06-04T02:30:23.417 回答