gpt4 book ai didi

c - _beginthread 和 WaitForSingleObject

转载 作者:可可西里 更新时间:2023-11-01 09:21:13 27 4
gpt4 key购买 nike

我正在实现一个启动 4 个线程的程序。

函数是:

void* SOURCE_in(struct SOURCE_1*);
void* SOURCE_out(struct SOURCE*);
void* SINK_out(struct SINK_1*);
void* SINK_in(struct SINK*);

这些函数允许 4 个 block 之间进行通信。Block 1 与 Block 2 通信。 block 2 与第三 block 共享信息。Block 3 与 Block 4 进行信息通信。

我的问题在 main.c 中

int main(int argc, char** argv){

extern int p, e_source, e_sink_1, l_sink, l_source_1, flag, ctr_source;
extern int ctr_source_1, ctr_sink, ctr_sink1;

SOURCE source; /*C struct*/
SINK sink; /*C struct*/
SOURCE_1 source_1; /*C struct*/
SINK_1 sink_1; /*C struct*/

HANDLE CHT[4];

p = 0;
e_source = flag = 0;
l_sink = e_sink_1 = l_source_1 = 1;
max = 5;
ctr_source = ctr_source_1 = ctr_sink = ctr_sink_1 = 0;

/*Initialize FIFO*/
F *f1 = fifo_init(10);
F *f2 = fifo_init(10);

/*Connect FIFO and modules*/
source.output_source = f1;
sink.input_sink = f1;

sink_1.output_sink = f2;
source_1.input_source = f2;

/*Create Threads*/
CHT[0] = (HANDLE)_beginthread((void (*)(void *))&SOURCE_out, 0, &f1);
CHT[1] = (HANDLE)_beginthread((void (*)(void *))&SINK_in, 0, &f1);
CHT[2] = (HANDLE)_beginthread((void (*)(void *))&SINK_out, 0, &f2);
CHT[3] = (HANDLE)_beginthread((void (*)(void *))&SOURCE_in, 0, &f2);

/* Wait until all threads have terminated */

WaitForSingleObject(CHT[0], INFINITE);
WaitForSingleObject(CHT[1], INFINITE);
WaitForSingleObject(CHT[2], INFINITE);
WaitForSingleObject(CHT[3], INFINITE);

getchar();

return 0;}

我读到 WaitForSingleObject 函数不适用于 _beginthread....但是我的函数不是 nunsigned __stdcall 类型...

我构建的程序没有错误,我使用断点来测试它,它没问题。当我编译时我遇到了这个问题:

线程“Win32 线程”(0x11ec) 已退出,代码为 0 (0x0)。线程“Win32 线程”(0x918) 已退出,代码为 0 (0x0)。线程“Win32 线程”(0x8a4) 已退出,代码为 0 (0x0)。线程“Win32 线程”(0x2a8) 已退出,代码为 -1073741510 (0xc000013a)。线程“Win32 线程”(0x12f8) 已退出,代码为 -1073741510 (0xc000013a)。程序“[3984] SW=SW.exe: Native”已退出,代码为 -1073741510 (0xc000013a)。

程序永远不会到达函数 getchar()

在做这个程序之前,我做了一个程序,在两个 block 之间进行通信,一个读,另一个写。在那种情况下,我没有任何问题。

如果我不使用 WaitForSingleObject 函数,问题就会消失,但我的程序几乎总是在完成之前停止。

每个线程使用的函数,以中断方式停止。但我希望其他的继续下去,直到它出现为止。

最佳答案

_beginthread 的文档说明您不能使用其中一个等待功能:

The following sample code demonstrates how you can use the thread handle returned by _beginthreadex with the synchronization API WaitForSingleObject. The main thread waits for the second thread to terminate before it continues. When the second thread calls _endthreadex, it causes its thread object to go to the signaled state. This allows the primary thread to continue running. This cannot be done with _beginthread and _endthread, because _endthread calls CloseHandle, destroying the thread object before it can be set to the signaled state.

并且本文还为您提供了解决方案,即改用_beginthreadex

我认为当你这么说的时候你在问题中暗示了这一点

but my functions are not __stdcall

您只需更改函数即可使用 __stdcall 调用约定。

关于c - _beginthread 和 WaitForSingleObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10448121/

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