gpt4 book ai didi

c - 线程在 WIC 中(有时)返回错误

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

我使用 WIC 制作了一个编码器。然后我尝试在最耗时的部分使用线程。这是

/*Encodes a bitmap source*/
hr = piBitmapFrame->WriteSource(
piBitmapSrc, /*bitmap source*/
rc /*area which need to be wrote*/
);

piBitmapSrc → WICBitmap

rc → WICRect,执行工作的矩形区域

我就是这样尝试的。

HRESULT writeSrc(
IWICBitmapFrameEncode *piBitmapFrame,
IWICBitmap *piBitmapSrc,
WICRect *rc
)
{
/*Encodes a bitmap source*/
hr = piBitmapFrame->WriteSource(
piBitmapSrc, /*bitmap source*/
rc /*area which need to be wrote*/
);

return hr;
}

然后我这样调用它

const int numberOfThreads = 4;
std::thread t[numberOfThreads];
int ht = (lHeight / numberOfThreads);

WICRect rc;
rc.X = 0;
rc.Height = ht;
rc.Width = lWidth;

rc.Y = 0;
t[0] = std::thread(writeSrc, piBitmapFrame, piBitmapSrc, &rc);

rc.Y += ht;
t[1] = std::thread(writeSrc, piBitmapFrame, piBitmapSrc, &rc);

rc.Y += ht;
t[2] = std::thread(writeSrc, piBitmapFrame, piBitmapSrc, &rc);

rc.Y += ht;
rc.Height = (lHeight - (3*ht));
t[3] = std::thread(writeSrc, piBitmapFrame, piBitmapSrc, &rc);

t[0].join();
t[1].join();
t[2].join();
t[3].join();

问题是,piBitmapFrame->WriteSource() 调用了 4 次,但只有 1 次返回 S_OK。其他 3 返回以下错误代码

WINCODEC_ERR_STREAMWRITE

WINCODEC_ERR_CODECTOOMANYSCANLINES

这是为什么呢。我怎样才能正确使用线程来做到这一点

最佳答案

您可能希望更多地了解线程。线程共享全局数据和资源。在您的示例中,您将跨所有四个线程共享 piBitMapFrame 和 WICRect 结构。

考虑到 WICRect 结构,您实际上是在分派(dispatch)线程时修改其中的数据。该数据一旦被修改,将对所有 正在运行的线程可见。所有线程都引用您声明的单个 WICRect,如果它们检查同一个变量两次 - 它们可能会发现它不同。

我不熟悉这个库,但你应该检查 WriteSource 是否是一个线程安全的调用。 piBitmapFrame 是否包含可通过调用 WriteSource 修改的数据?回想上面的 WICRect 示例,WriteSource 可能无法很好地处理调用中更改的内部数据。

一个例子:

这个程序试图在两个执行线程中打印到标准输出,没有关于谁在什么时候写的任何同步。这不会崩溃,cout 是线程安全的,但它不会产生您可能期望的输出。输出将是标准输出缓冲区中的两个线程交错字符。

#include <iostream>
#include <thread>

void say_hello(){

/* no locking to prevent stdout contention */
for ( int i = 0; i < 10; i++){
std::cout << "Hello from thread " << std::this_thread::get_id() << "!!!" << std::endl;
}
}

int main(){

int i;
// create our thread
std::thread ct1(say_hello);

// say hello ourselves
say_hello();

// wait for thread ct1 to finish execution.
ct1.join();
return i;
}

示例输出:

HHeelllloo  ffrroomm  tthhrreeaadd  00xx170fcf5fd78b000007!3!0!0
!!H!e
llHoe lflroo mf rtohmr etahdr e0axd1 00cx57df8f0f070b!0!0!7
30H0e!l!l!o
fHreolml ot hfrreoamd t0hxr1e0acd5 d08x070f0f!f!7!b
00H7e3l0l0o! !f!r
omH etlhlroe afdr o0mx 1t0hcr5eda8d0 000x!7!f!f
f7Hbe0l0l7o3 0f0r!o!m!
thHreelaldo 0fxr1o0mc 5tdh8r0e0a0d! !0!x
7fHfefl7lbo0 0f7r3o0m0 !t!h!r
eaHde l0lxo1 0fcr5odm8 0t0h0r!e!a!d
0Hxe7lflfof 7fbr0o0m7 3t0h0r!e!a!d
0Hxe1l0lco5 df8r0o0m0 !t!h!r
eaHde l0lxo7 fffrfo7mb 0t0h7r3e0a0d! !0!x
10Hce5ldl8o0 0f0r!o!m!
thHreelaldo 0fxr7ofmf ft7hbr0e0a7d3 000x!1!0!c
5dH8e0l0l0o! !f!r
omH etlhlroe afdr o0mx 7tfhfrfe7abd0 007x31000c!5!d!8
00H0e!l!l!o
from thread 0x7fff7b007300!!!

关于c - 线程在 WIC 中(有时)返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30585814/

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