Swapchain creation crash when multi-thread

Hello,
I am making a win32 app which creates a separate new window whenever some menu is selected
For each created window, I am rendering using Vulkan independently
I use std::thread in each window. Inside each thread, I create Vulkan objects independently (at now)

I checked, only one window (one thread case) works very all right, rendering OK

However if I create another window, now I have two std::thread
Then, the program generates an error
when doing

template <typename SurfaceFormatKHRAllocator, typename Dispatch>
VULKAN_HPP_NODISCARD VULKAN_HPP_INLINE typename ResultValueType<std::vector<VULKAN_HPP_NAMESPACE::SurfaceFormatKHR, SurfaceFormatKHRAllocator>>::type
PhysicalDevice::getSurfaceFormatsKHR( VULKAN_HPP_NAMESPACE::SurfaceKHR surface, Dispatch const & d ) const
{
…

std::vector<VULKAN_HPP_NAMESPACE::SurfaceFormatKHR, SurfaceFormatKHRAllocator> surfaceFormats;

…

do
{
  result = static_cast<VULKAN_HPP_NAMESPACE::Result>(
    d.vkGetPhysicalDeviceSurfaceFormatsKHR( m_physicalDevice, static_cast<VkSurfaceKHR>( surface ), &surfaceFormatCount, nullptr ) );

The result (return value) says OK (0)
but
error log on console says below, and eventually the program crashes and the computer itself halts

[error] 1928166024 - VUID-vkDestroyBuffer-buffer-parent: Validation Error: [ VUID-vkDestroyBuffer-buffer-parent ] Object 0: handle = 0x25ff7d7cee0, type = VK_OBJECT_TYPE_INSTANCE; | MessageID = 0x72ed7a88 | vkDestroyBuffer(): buffer (VkBuffer 0x6eea0c0000000185) was created, allocated or retrieved from VkDevice 0x25fdf7f73e0, but command is using (or its dispatchable parameter is associated with) VkDevice 0x25fedad0060. The Vulkan spec states: If buffer is a valid handle, it must have been created, allocated, or retrieved from device (Vulkan® 1.3.283 - A Specification (with all registered extensions))
[error] 1567320034 - UNASSIGNED-Threading-Info: Validation Error: [ UNASSIGNED-Threading-Info ] Object 0: handle = 0x6eea0c0000000185, type = VK_OBJECT_TYPE_BUFFER; | MessageID = 0x5d6b67e2 | vkDestroyBuffer(): Couldn’t find VkBuffer Object 0x6eea0c0000000185. This should not happen and may indicate a bug in the application.
[

So I think I need more knowledge about multhi-threading

However, yet I cannot find detailed web pages about how to program Vulkan APIs when using std::thread

I will appreciate if you can help me with this issue
Thank you

The error indicates an issue with destroying buffers not with the function you are highlighting I would suggest you look else where for your error. I would think its most likely you 2 threads creating buffers and destroying without synchronization.

As for multithreading with vulkan, all rules of multithreading apply as normal and its mostly common to build command buffers on multiple threads then submit them all on one thread (make sure to have one command pool per thread to avoid unnecessary locks). If you really wanted to you could create multiple queues then have multiple queue submits across multiple threads but this is not as common.

For example lets say you have a scene with 10,000 different meshes to be rendered (very extreme example). You could spawn 10 threads, each thread with its own command pool and buffer and each thread is responsible for 1000 of the meshes. On the main thread you would wait until these threads have finished their work and you would do one vkQueueSubmit() with all the command buffers.

If you are new to multithreading I suggest you do some more research into subjects such as synchronization, locks, mutexes, atomic variables and reasearch into std::thread() usage, and modern c++ functions such as std::for_each with the std::execution::par policy.

I would recommend watching this video for multithreading in c++:

Sascha Williams has a good example of vulkan multithreading here:

and here is a website which actually goes into the practically of multithreading in games/engines and its use cases/implementations:

hopefully all these resources help better your understanding. I haven’t used the win32 API directly so I am not sure what synchronization/resources has to happen when creating multiple windows on seperate threads.

1 Like

Hello, TooGood
Thank you for all your kindness
As you explained, I gave up using threads at this moment
Instead, I am using CreateProcess (Windows API), which is just fine and all right
And about using threads, I will study materials you recommended above
Have a great weekend and see you again

1 Like