gpt4 book ai didi

c - C 程序中的 Linux POSIX 在取消线程中失败

转载 作者:太空宇宙 更新时间:2023-11-04 07:33:36 24 4
gpt4 key购买 nike

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

但在此之前(问题出现在下面的代码中)我只是想检查线程是否正确取消。那就是我被困的地方。看到错误需要时间,但基本上在(有时 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

最佳答案

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

关于c - C 程序中的 Linux POSIX 在取消线程中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10875658/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com