简体   繁体   English

createthread() 如何更好地释放内存?

[英]createthread() how to release memory better?

I want to close a thread and release memory after createthread() .我想在createthread()之后关闭一个线程并释放内存。 I did this with return createthread() 's callback and closehandle() .我这样做是有回报createthread()的回调和closehandle() Some people said this can't release memory clear.有人说这样不能释放内存清除。 I am testing it, so far, so good.我正在测试它,到目前为止,很好。 It looks ok?看起来好吗?

I take a flag:endThread to do this, there are too many flags, too many if, it looks ugly, how to make it better?我拿了一个flag:endThread来做这个,flag太多,if太多,看起来很丑,怎么弄好?

typedef void(*pfunc)(char*);
HANDLE H_thread=NULL;
int endThread = 0;//0:thread not start;1:end thread order;2:thread started
void mycall(char*s){
    cout << "callback" << endl;
    cout << s << endl;
}
static DWORD WINAPI myfunc(LPVOID lp)
{
    while (1)
    {
        ((pfunc)lp)("2222");
        cout << "thread..........." << endl;
        Sleep(10);
        if (1==endThread)
        {
            endThread = 0;
            return 0;
        }
    }
}
void thread_callback(pfunc call){
    if (0==endThread)
    {
        H_thread = CreateThread(NULL, 0, myfunc, call, 0, NULL);
        endThread = 2;
        call("1111");
    }
}
int _tmain(int argc, _TCHAR* argv[])
{
    while (true)
    {
        thread_callback(mycall);
        endThread = 1;
        //wait for thread end.
        while (endThread != 0){
            Sleep(1);
        }
        CloseHandle(H_thread);
        H_thread = NULL;
    }
    while(1);
    return 0;
}

You are creating and destroying threads over and over.您一遍又一遍地创建和销毁线程。 It doesn't even check if the thread was done.它甚至不检查线程是否完成。 It's unlikely that that's your goal.这不太可能是你的目标。 Use WaitForSingleObject or WaitForMultipleObjects to make sure that the thread is done.使用WaitForSingleObjectWaitForMultipleObjects来确保线程完成。 In a Window program (not console) you can use SendMessage from the thread to GUI thread, to indicate that the thread is done.在 Window 程序(不是控制台)中,您可以使用SendMessage从线程到 GUI 线程,以指示线程已完成。

Try the example below.试试下面的例子。 Also you can use system("pause");你也可以使用system("pause"); instead of while(1);而不是while(1); , but that's only necessary if you are running the program from VS IDE in debug mode. ,但只有在调试模式下从 VS IDE 运行程序时才需要这样做。 See also this MSDN example另请参阅此MSDN 示例

#include <iostream>
#include <string>
#include <windows.h>

struct T_data {
    std::wstring text;
};

DWORD WINAPI thread_function(void *ptr) {
    T_data* data = (T_data*)ptr;
    MessageBoxW(GetConsoleWindow(), data->text.c_str(), 0, 0);
    return 0;
}

int main() {
    T_data *data = new T_data;
    data->text = L"hello world";
    DWORD threadId;
    HANDLE handle = CreateThread(NULL, 0, thread_function, data, 0, &threadId);

    if (handle) {
        printf("thread started\n");
        while (WaitForSingleObject(handle, 100))
            printf(".");
        printf("\nthread finished\n");
        CloseHandle(handle);
    }

    delete data;
    system("pause");
    return 0;
}

Deleting the data can get tricky if data is shared, depending on where and how you want to do that.如果数据是共享的,删除数据可能会变得棘手,这取决于您想要在何处以及如何执行此操作。 The calling thread may delete the data before receiving thread is finished processing it.调用线程可能会在接收线程完成处理数据之前删除数据。 I'll leave that out since you haven't shown any such shared data in the question.我会忽略这一点,因为您没有在问题中显示任何此类共享数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM