c - Logging from winapi callbacks - Stack Overflow

I am developing a C application in Visual Studio 2022, using winapi. I have some issues regarding loggi

I am developing a C application in Visual Studio 2022, using winapi. I have some issues regarding logging (for debug purposes) from callbacks when using winapi thread pools. Below is a minimal example (without error handling, for shorter code):

VOID CALLBACK WorkCallback(PTP_CALLBACK_INSTANCE instance, PVOID parameter, PTP_WORK work)
{
    UNREFERENCED_PARAMETER(instance);
    UNREFERENCED_PARAMETER(parameter);
    UNREFERENCED_PARAMETER(work);
    printf("Hello from WorkCallback\n\r");
}

VOID Test()
{
    printf("Hello from Test\n\r");
    
    TP_CALLBACK_ENVIRON callbackEnviron;
    InitializeThreadpoolEnvironment(&callbackEnviron);

    PTP_POOL pool = CreateThreadpool(NULL);
    SetThreadpoolThreadMinimum(pool, 1);
    SetThreadpoolThreadMaximum(pool, 1);

    SetThreadpoolCallbackPool(&callbackEnviron, pool);

    PTP_CLEANUP_GROUP cleanupGroup = CreateThreadpoolCleanupGroup();
    SetThreadpoolCallbackCleanupGroup(&callbackEnviron, cleanupGroup, NULL);

    PTP_WORK work = CreateThreadpoolWork(WorkCallback, NULL, &callbackEnviron);
    SubmitThreadpoolWork(work);

    CloseThreadpool(pool);
    CloseThreadpoolCleanupGroupMembers(cleanupGroup, FALSE, NULL);
    CloseThreadpoolCleanupGroup(cleanupGroup);
}

int CDECL
main()
{
    Test();
    getchar();
    return 0;
}

When I execute this command without debugging, or with debugging but without breakpoints, the only output I get is Hello from Test. The only way I found to print Hello from WorkCallback is to put a breakpoint before SubmitThreadpoolWork, another one in WorkCallback, to step into SubmitThreadpoolWork when debugging, and to use the Parallel Stacks window (Debug->Windows->Parallel Stacks).

What is odd to me is that if I simply put a breakpoint in WorkCallback, it is not enough. And that behaviour under specific debug conditions is different than in regular execution. In some other SO questions I saw people pointing out that different threads might not be able to print to console, so I tried writing to files, but I observed the same behaviour as I described before.

Am I missing something related to Thread Pools? Why is this happening?

I am developing a C application in Visual Studio 2022, using winapi. I have some issues regarding logging (for debug purposes) from callbacks when using winapi thread pools. Below is a minimal example (without error handling, for shorter code):

VOID CALLBACK WorkCallback(PTP_CALLBACK_INSTANCE instance, PVOID parameter, PTP_WORK work)
{
    UNREFERENCED_PARAMETER(instance);
    UNREFERENCED_PARAMETER(parameter);
    UNREFERENCED_PARAMETER(work);
    printf("Hello from WorkCallback\n\r");
}

VOID Test()
{
    printf("Hello from Test\n\r");
    
    TP_CALLBACK_ENVIRON callbackEnviron;
    InitializeThreadpoolEnvironment(&callbackEnviron);

    PTP_POOL pool = CreateThreadpool(NULL);
    SetThreadpoolThreadMinimum(pool, 1);
    SetThreadpoolThreadMaximum(pool, 1);

    SetThreadpoolCallbackPool(&callbackEnviron, pool);

    PTP_CLEANUP_GROUP cleanupGroup = CreateThreadpoolCleanupGroup();
    SetThreadpoolCallbackCleanupGroup(&callbackEnviron, cleanupGroup, NULL);

    PTP_WORK work = CreateThreadpoolWork(WorkCallback, NULL, &callbackEnviron);
    SubmitThreadpoolWork(work);

    CloseThreadpool(pool);
    CloseThreadpoolCleanupGroupMembers(cleanupGroup, FALSE, NULL);
    CloseThreadpoolCleanupGroup(cleanupGroup);
}

int CDECL
main()
{
    Test();
    getchar();
    return 0;
}

When I execute this command without debugging, or with debugging but without breakpoints, the only output I get is Hello from Test. The only way I found to print Hello from WorkCallback is to put a breakpoint before SubmitThreadpoolWork, another one in WorkCallback, to step into SubmitThreadpoolWork when debugging, and to use the Parallel Stacks window (Debug->Windows->Parallel Stacks).

What is odd to me is that if I simply put a breakpoint in WorkCallback, it is not enough. And that behaviour under specific debug conditions is different than in regular execution. In some other SO questions I saw people pointing out that different threads might not be able to print to console, so I tried writing to files, but I observed the same behaviour as I described before.

Am I missing something related to Thread Pools? Why is this happening?

Share Improve this question asked Nov 16, 2024 at 8:08 brolandbroland 516 bronze badges 2
  • Perhaps Sleep for a while after SubmitThreadpoolWork(work); helps. – Iman Abdollahzadeh Commented Nov 16, 2024 at 8:55
  • On a side note: the correct way to print a line break on Windows is \r\n instead of \n\r – Remy Lebeau Commented Nov 16, 2024 at 10:45
Add a comment  | 

1 Answer 1

Reset to default 2

I suggest closing the pool at the very end of the provided sample. The CloseThreadpoolCleanupGroupMembers function waits for all the cleanup group members to terminate before releasing them. If you close the pool beforehand, as described, the callback function may never be invoked.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745663563a4638984.html

相关推荐

  • c - Logging from winapi callbacks - Stack Overflow

    I am developing a C application in Visual Studio 2022, using winapi. I have some issues regarding loggi

    5小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信