From bb433e232daae2c4add2bb6b0ccbd24cc0dd9607 Mon Sep 17 00:00:00 2001 From: zcy <290198252@qq.com> Date: Wed, 17 Nov 2021 16:02:58 +0800 Subject: [PATCH] no message --- general/src/threadsafe/thread_safe_list.hpp | 23 ++- test/src/cpp11/cpp11_test.cpp | 168 ++------------------ 2 files changed, 38 insertions(+), 153 deletions(-) diff --git a/general/src/threadsafe/thread_safe_list.hpp b/general/src/threadsafe/thread_safe_list.hpp index 063c06d..198e5ed 100644 --- a/general/src/threadsafe/thread_safe_list.hpp +++ b/general/src/threadsafe/thread_safe_list.hpp @@ -1,7 +1,7 @@ /* * @Author: caiyuzheng * @Date: 2021-10-06 23:05:26 - * @LastEditTime: 2021-10-08 00:30:49 + * @LastEditTime: 2021-11-17 15:58:26 * @LastEditors: Please set LastEditors * @Description: In User Settings Edit * @FilePath: \generallib\general\src\threadsafe\thread_safe_list.h @@ -10,6 +10,7 @@ #include #include +#include using namespace std; template @@ -82,6 +83,25 @@ public: return ret1; } } + T PopFrontAndWait() + { + if (!m_data.empty()) + { + m_mux.lock(); + auto ret = m_data.begin(); + auto ret1 = *ret; + m_data.erase(ret); + m_mux.unlock(); + return ret1; + }else{ + m_cv.wait(&m_mux); + auto ret = m_data.begin(); + auto ret1 = *ret; + m_data.erase(ret); + m_mux.unlock(); + return ret1; + } + } T PopEnd() { @@ -114,5 +134,6 @@ public: private: std::mutex m_mux; + std::condition_variable m_cv; std::list m_data; }; diff --git a/test/src/cpp11/cpp11_test.cpp b/test/src/cpp11/cpp11_test.cpp index f0f2a3b..84d3ca1 100644 --- a/test/src/cpp11/cpp11_test.cpp +++ b/test/src/cpp11/cpp11_test.cpp @@ -1,7 +1,7 @@ /* * @Author: your name * @Date: 2021-03-15 23:07:25 - * @LastEditTime: 2021-07-25 23:47:46 + * @LastEditTime: 2021-11-17 15:58:56 * @LastEditors: Please set LastEditors * @Description: In User Settings Edit * @FilePath: \cpp11\cpp11_test.cpp @@ -13,9 +13,9 @@ #include #include using namespace std; -#include "thread_usage.h" +// #include "thread_usage.h" #include "threadpool.h" -#include "json.h" +// #include "json.h" #include "template.h" extern "C"{ #include @@ -26,154 +26,19 @@ extern "C"{ #include "loger.h" #include #include "function/daemon.h" - -// [Added by thinkhy 09/12/20] -// Description: Kill process(es) by PID. -// Reference: http://www.vckbase.com/document/viewdoc/?id=1882 -// RETVALUE: SUCCESS TRUE -// FAILED FALSE -BOOL KillProcess(DWORD dwPid) -{ - HANDLE hPrc; - - if (0 == dwPid) - return FALSE; - - hPrc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwPid); // Opens handle to the process. - - if (!TerminateProcess(hPrc, 0)) // Terminates a process. - { - CloseHandle(hPrc); - return FALSE; - } - else - WaitForSingleObject(hPrc, 2000); // At most ,waite 2000 millisecond. - - CloseHandle(hPrc); - return TRUE; -} - -BOOL KillProcessByName(const TCHAR *lpszProcessName) -{ - unsigned int pid = -1; - BOOL retval = TRUE; - - if (lpszProcessName == NULL) - return -1; - - DWORD dwRet = 0; - HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); - PROCESSENTRY32 processInfo; - processInfo.dwSize = sizeof(PROCESSENTRY32); - int flag = Process32First(hSnapshot, &processInfo); - - // Find the process with name as same as lpszProcessName - while (flag != 0) - { - printf("kill process find %s\r\n",processInfo.szExeFile); - if (strcmp(processInfo.szExeFile, lpszProcessName) == 0) - { - // Terminate the process. - pid = processInfo.th32ProcessID; - HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, TRUE, pid); - printf("kill process pid %d\r\n",pid); - if (TerminateProcess(hProcess, 0) != TRUE) - { // Failed to terminate it. - retval = FALSE; - break; - } - } - - flag = Process32Next(hSnapshot, &processInfo); - } // while (flag != 0) - CloseHandle(hSnapshot); - if (pid == -1) - return FALSE; - - return retval; -} - -BOOL SetProcessPrivilege(char *lpName, BOOL opt) -{ - HANDLE tokenhandle; - TOKEN_PRIVILEGES NewState; - - if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &tokenhandle)) - { - LookupPrivilegeValue(NULL, lpName, &NewState.Privileges[0].Luid); - NewState.PrivilegeCount = 1; - NewState.Privileges[0].Attributes = opt != 0 ? 2 : 0; - AdjustTokenPrivileges(tokenhandle, FALSE, &NewState, sizeof(NewState), NULL, NULL); - CloseHandle(tokenhandle); - return 1; - } - else - { - return 0; - } -} - -int RangeProcess() -{ - DWORD Proc_pid[1024], Retn_bytes, Proc_count, Retn_bytes2; - unsigned int i; - HMODULE hMod[1024]; - HANDLE hProcess; - char szModName[MAX_PATH]; - if (EnumProcesses(Proc_pid, sizeof(Proc_pid), &Retn_bytes)) - { - Proc_count = Retn_bytes / sizeof(DWORD); - SetProcessPrivilege("SeDebugPrivilege", 1); - for (i = 0; i < Proc_count; i++) - { - hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, Proc_pid[i]); - if (hProcess != NULL) - { - EnumProcessModules(hProcess, hMod, sizeof(hMod), &Retn_bytes2); - GetModuleFileNameEx(hProcess, hMod[0], szModName, sizeof(szModName)); - printf("PID=%d Path=%s\n", Proc_pid[i], szModName); - } - CloseHandle(hProcess); - } - SetProcessPrivilege("SeDebugPrivilege", 0); - } - return 0; -} - -int test_fork(char *szCommandLine) -{ - if(nullptr == szCommandLine) - { - return -1; - } - STARTUPINFO si = {sizeof(si)}; - PROCESS_INFORMATION pi; - si.dwFlags = STARTF_USESHOWWINDOW; //指定wShowWindow成员有效 - si.wShowWindow = TRUE; //此成员设为TRUE的话则显示新建进程的主窗口 - BOOL bRet = CreateProcess( - NULL, //不在此指定可执行文件的文件名 - szCommandLine, //命令行参数 - NULL, //默认进程安全性 - NULL, //默认进程安全性 - FALSE, //指定当前进程内句柄不可以被子进程继承 - CREATE_NEW_CONSOLE, //为新进程创建一个新的控制台窗口 - NULL, //使用本进程的环境变量 - NULL, //使用本进程的驱动器和目录 - &si, - &pi); - if (bRet) - { - //不使用的句柄最好关掉 - CloseHandle(pi.hThread); - CloseHandle(pi.hProcess); - printf("new process id %d\n", pi.dwProcessId); - printf("new thread id %d\n", pi.dwThreadId); - } - return 0; -} +#include "threadsafe/thread_safe_list.hpp" int main(int argc, char **argv) { + + ThreadSafeList ip; + ip.PushFront(102); + ip.PushFront(111); + + std::cout< // i;