c++ - 我如何使用互斥锁锁定对 bool 的访问?

标签 c++ multithreading sdl-2

已解决!:我正在新线程中复制 Map 实例,但不使用引用。

我正在学习如何使用多线程。为此,我正在编写一个小游戏,我希望游戏在主线程中运行,而下一个关卡 block 将在另一个线程中加载。为此,我在 vector 周围设置了一个互斥锁,以告诉加载线程接下来要加载什么。在这个互斥量中,我还有一个 bool 值来告诉线程何时终止。

在 Map::Map() 中初始化线程

pending_orders_mutex = SDL_CreateMutex();
can_process_order = SDL_CreateCond();
chunk_loader_thread = SDL_CreateThread(Map::chunk_loader,"chunk_loader_thread",(void*)this);

加载线程

int Map::chunk_loader(void * data)
{
    Map map = *(Map*)data;
    bool kill_this_thread = false;

    Chunk_Order actual_order;
    actual_order.load_graphics = false;
    actual_order.x = 0;
    actual_order.y = 0;



    while (!kill_this_thread)
    {
        SDL_LockMutex(map.pending_orders_mutex);            // lock mutex
        printf("3-kill_chunk_loader_thread: %d\n", map.kill_chunk_loader_thread);
        kill_this_thread = map.kill_chunk_loader_thread;
        printf("4-kill_chunk_loader_thread: %d\n", map.kill_chunk_loader_thread);
        if (!kill_this_thread)
        {
            if (map.pending_orders.size())
            {
                actual_order = map.pending_orders.back();
                map.pending_orders.pop_back();
                printf("in thread processing order\n");
            }
            else
            {
                printf("in thread waiting for order\n");
                SDL_CondWait(map.can_process_order, map.pending_orders_mutex);
            }
        }
        SDL_UnlockMutex(map.pending_orders_mutex);          // unlock mutex

        //load actual order
    }
    printf("thread got killed\n");
    return 0;
}

杀死线程(主线程)

SDL_LockMutex(pending_orders_mutex);            // lock mutex
  printf("setting kill command\n");
  printf("1-kill_chunk_loader_thread: %d\n", kill_chunk_loader_thread);
kill_chunk_loader_thread = true;                // send kill command
  printf("2-kill_chunk_loader_thread: %d\n", kill_chunk_loader_thread);
SDL_CondSignal(can_process_order);              // signal that order was pushed
SDL_UnlockMutex(pending_orders_mutex);          // unlock mutex

SDL_WaitThread(chunk_loader_thread, NULL);

控制台输出

3-kill_chunk_loader_thread: 0
4-kill_chunk_loader_thread: 0
in thread waiting for order
setting kill command
1-kill_chunk_loader_thread: 0
2-kill_chunk_loader_thread: 1
3-kill_chunk_loader_thread: 0
4-kill_chunk_loader_thread: 0
in thread waiting for order

为什么主线程不更改加载线程中的“kill_chunk_loader_thread” bool 值?

最佳答案

首先,你应该尝试在问题中上传一个最小的完整程序。

看起来你设置了 kill_chunk_loader_thread = true

但你没有设置 map.kill_chunk_loader_thread = true

map 声明的部分从你的问题中消失了,但我猜你没有使用对局部或全局变量的引用,或者你只是执行结构复制,所以当你改变一个结构时另一个完全没有受到影响。

编辑:

Map map = *(Map*)data; 复制 map 结构(我猜是默认的复制构造函数)所以从现在开始,如果源 map 发生变化,拷贝将不会.

你应该继续使用指针,像这样:Map* pMap = (Map*)data; 并像这样检查指针:kill_this_thread = pMap->kill_chunk_loader_thread; 所以你从源映射中读取。

关于c++ - 我如何使用互斥锁锁定对 bool 的访问?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50721989/

相关文章:

c++ - 访问私有(private)成员

c - 为什么我的线程合并排序比基本的递归合并排序慢? [复制]

c++ - CLion 无法识别 SDL2_image

c++ - 为什么流输出中的 < 而不是 << 仍然可以编译?

c++ - 我可以使用C++删除Stacks中的特定位置数据吗?

c++ - 线程写入日志文件

C:优化导致SDL_QUIT为真?

sdl - 可以在 SDL_Init() 之前调用 SDL_GetBasePath() 和 SDL_GetPrefPath() 吗?

c++ - 在 switch 中使用 constexpr 成员函数

android - 安全取消后台线程中的 ORMLite 写入