0
我需要將二進制數據傳遞給本地線程。 我使用std :: string來保存二進制數據,我想出了一個想法如何將std :: string傳遞給本地字符串,並且想知道它是否安全。將std :: string傳遞給本地線程
#include <Windows.h>
#include <iostream>
#include <string>
using namespace std;
DWORD WINAPI MyThreadFunction(LPVOID lpParam)
{
string binaryDataInThread = string(*(string*)lpParam); // copy data to current thread
while (true)
{// do some stuff with binaryDataInThread
cout << "thread binaryData size: " << binaryDataInThread.size() << endl;
Sleep(1000);
}
return 0;
}
int main(int argc, char *argv[])
{
{
string binaryDataInMain;
for (int i = 0; i < 500; i++)
binaryDataInMain += (char)i;
CloseHandle(CreateThread(NULL, NULL, MyThreadFunction, &binaryDataInMain, NULL, NULL));
Sleep(1000); // wait for thread to copy data
} // destroy binaryDataInMain
system("pause");
ExitProcess(0);
}
binaryDataInThread的大小是500,所以所有的二進制數據都成功通過了。但安全嗎?