windows多线程编程
来源:互联网 发布:淘宝介入处理流程 编辑:程序博客网 时间:2024/06/07 01:51
进程:一个运行的程序,内存,cpu时间等都归进程所有.
线程:cpu调度的最小单位,一个进程内的多个线程共享4G内存.程序的一个执行路径
1.最基础的使用方法.
CreateThread,ExitThread,CloseHand
#include <iostream>#include <process.h>//CreateThread,ExitThread,CloseHandle#include <windows.h>#include <stdio.h>BOOL repeat = TRUE;DWORD WINAPI MyThread1(LPVOID lpParameter){while (repeat){printf("how are you?\n");Sleep(1);}DWORD exitCode;ExitThread(exitCode);return 0;}DWORD WINAPI MyThread2(LPVOID lpParameter){while (repeat){printf("very well\n");Sleep(1);}DWORD exitCode;ExitThread(exitCode);return 0;}DWORD WINAPI KillThread(LPVOID lpParameter){repeat = FALSE;return 0;}int main(){HANDLE handle1, handle2, handle3;DWORD dw1, dw2, dw3;handle1 = CreateThread(NULL, 0, MyThread1, NULL, 0, &dw1);if (handle1 == NULL){printf("creat thread1 failed!\n");return -1;}handle2 = CreateThread(NULL, 0, MyThread2, NULL, 0, &dw2);if (handle2 == NULL){printf("creat thread2 failed!\n");return -1;}Sleep(1000);handle3 = CreateThread(NULL, 0, KillThread, NULL, 0, &dw3);if (handle3 == NULL){printf("creat thread3 failed!\n");return -1;}CloseHandle(handle1);CloseHandle(handle2);CloseHandle(handle3);return 0;}vc多线程的编译需要设置,c/c++的code generation的use run-time library 选择Multithread;CreateRemoteThread函数在别的进程中创建一个线程
SuspendThread(HANDLE),RssumeThread(HANDLE)暂停和重启一个线程.
#include <iostream>#include <process.h>//CreateThread,ExitThread,CloseHandle#include <windows.h>#include <stdio.h>///////////////////////////////////////////////////////////////////////////////////////////////////////suspend resume 的使用/////////////////////////////////////////////////////////////////////////////////////////////////////DWORD WINAPI myThread(void*){for (int i = 0; i < 100; i++){printf("Hello,everyone!\n");Sleep(100);}return 0;}int main(){HANDLE handle;if ((handle = CreateThread(NULL, 0, myThread, NULL, 0, NULL)) == NULL)printf("create thread failed\n");Sleep(100);for (int i = 0; i < 10; i++){printf("***now suspend***\n");SuspendThread(handle);for (int i = 0; i < 3; i++){printf("Good thanks\n");Sleep(100);}printf("***resume***\n");ResumeThread(handle);Sleep(300);}}TerminateThread(HANDLE,DWORD)第二个参数为退出码.用于终止一个线程
这个函数非常的危险,立即终止了线程,会造成分配的堆栈没有释放,一般不用.
#include <iostream>#include <process.h>//CreateThread,ExitThread,CloseHandle#include <windows.h>#include <stdio.h>///////////////////////////////////////////////////////////////////////////////////////////////////////suspend resume 的使用/////////////////////////////////////////////////////////////////////////////////////////////////////DWORD WINAPI myThread(void*){for (int i = 0; i < 100; i++){printf("Hello,everyone!\n");Sleep(100);}return 0;}int main(){HANDLE handle;if ((handle = CreateThread(NULL, 0, myThread, NULL, 0, NULL)) == NULL)printf("create thread failed\n");Sleep(100);printf("***terminate****\n");TerminateThread(handle, 3);Sleep(1000);CloseHandle(handle);return 0;}CloseHandle(handle);return 0;}GetExitCodeThread(HANDLE,lpDWORD)第二个用于保存exitcode#include <iostream>#include <process.h>//CreateThread,ExitThread,CloseHandle#include <windows.h>#include <stdio.h>using namespace std;///////////////////////////////////////////////////////////////////////////////////////////////////////getcurrentthread getexitcodeThread 的使用/////////////////////////////////////////////////////////////////////////////////////////////////////DWORD WINAPI myThread(void*){Sleep(1000);std::cout << "current thread id:" << GetCurrentThreadId() << std::endl;return 3;}int main(){HANDLE handle;DWORD dw;if ((handle = CreateThread(NULL, 0, myThread, NULL, 0, &dw)) == NULL)printf("create thread failed\n");cout << "create thread id:" << dw << endl;Sleep(1000);GetExitCodeThread(handle, &dw);cout << "thread exit with " << dw << endl;}
0 0
- [windows编程]多线程编程
- Windows多线程编程
- Windows多线程编程
- Windows多线程编程总结
- windows多线程编程经验总结
- Windows多线程编程注意事项
- windows多线程编程经验总结
- Windows平台多线程编程
- Windows多线程编程总结
- Windows多线程编程
- windows多线程编程[转]
- Windows多线程编程总结
- WINDOWS多线程编程
- windows多线程编程
- Windows多线程编程总结
- Windows多线程编程
- windows多线程编程
- Windows 下多线程编程
- (6)html的核心元素与特性组
- 统计数字问题(Java语言实现)
- Kali Linux
- web开发相关缓存和性能
- LVM管理体系(四)删除逻辑卷(操作步骤)
- windows多线程编程
- 复撸C系列(5)-指针(1)
- [Android] waiting for the debugger to attach
- 实时开发框架Meteor API解读系列<七> Collection --01
- Dom笔记
- javascrip当中删除节点的方法
- Not so Mobile uva
- nginx相关内容
- 最优比率生成树 11WA