C++ Thread erstellen und ausführen/String aufteilen

c++ erstellt einen Thread und führt ihn aus

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


HANDLE Thread1 = NULL;
int Thread1_bool = 0;
DWORD WINAPI Process_thread(LPVOID lpParameters)
{
    
    
	int i = 0;
	while (Thread1_bool)
	{
    
    
		i++;
		if (i == 100)
			Thread1_bool = false;
		std::cout << "子线程" << std::endl;
		std::this_thread::sleep_for(std::chrono::milliseconds(20));
	}
	return 0;
}

int main()
{
    
    
	//创建线程
	Thread1 = CreateThread(NULL, 0, Process_thread, NULL, 0, NULL);
	Thread1_bool = true;


	while (Thread1_bool)
	{
    
    
		std::cout << "主线程" << std::endl;
		Sleep(20);
	}
	std::cout << "结束" << std::endl;


	//等待函数,等待子线程 1秒
	DWORD dwRet = WaitForSingleObject(Thread1, 1000);
	//关闭线程
	CloseHandle(Thread1);
	return 1;
}

C++-String-Split

//字符串分割
vector<string> split(const string& str, const string& pattern)
{
    
    
	vector<string> ret;
	if (pattern.empty()) return ret;
	size_t start = 0, index = str.find_first_of(pattern, 0);
	while (index != str.npos)
	{
    
    
		if (start != index)
			ret.push_back(str.substr(start, index - start));
		start = index + 1;
		index = str.find_first_of(pattern, start);
	}
	if (!str.substr(start).empty())
		ret.push_back(str.substr(start));
	return ret;
}

std::string str = "abcd_efg";
std::string sp = "_";
vector<string> whd = split(str , sp );

Guess you like

Origin blog.csdn.net/qq_41648925/article/details/129137641