清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>
使用WINAPI的CreateThread
和C/C++的_beginthread/_beginthreadex
和pthread的pthread_create
win7
mingw
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | /* purpose: 练习windowAPI的线程函数CreateThread, 和标准C库的线程函数_beginthread/_beginthreadex, 和mingw的线程函数pthread_create author:looyer@sina.com date:2016/4/20 */ #include <windows.h> #include <process.h> #include <iostream> #include <cstdlib> #include <cstdio> #include <pthread.h> //需要-lpthread using namespace std; //使用_beginthread创建的线程函数,对线程控制较弱,使用_endthread(void)结束线程 void __cdecl threadA( void *p) { for ( int i = 0; i < 5; i++) { cout << "thread_A\n" ; Sleep(500); } } //使用_beginthreadex创建的线程函数 unsigned int __stdcall threadB( void *p) { for ( int i = 0; i < 5; i++) { cout << "thread B\n" ; Sleep(500); } } //使用WinAPI的CreateThread创建的线程函数 DWORD WINAPI ThreadC( LPVOID param) { for ( int i = 0; i < 5; i++) { cout << "thread C\n" ; Sleep(300); } return 0; } //使用pthread_create创建的线程函数 void * thread_D( void *param) { for ( int i = 0; i < 5; i++) { cout << "thread D\n" ; Sleep(400); } return 0; } int main( int argc, char *argv[]) { _beginthread(&threadA, 0, 0); HANDLE hEx = ( HANDLE )_beginthreadex(0, 0, &threadB, 0, 0, 0); HANDLE hThread = CreateThread(0, 0, ThreadC, 0, 0, 0); pthread_t pid; pthread_create(&pid, 0, thread_D, 0); WaitForSingleObject(hThread, INFINITE); CloseHandle(hThread); WaitForSingleObject(hEx, INFINITE); CloseHandle(hEx); pthread_join(pid, 0); //等待thread_D执行完成 system ( "pause" ); return 0; } |
1 2 3 4 5 6 7 | NAME = multi_thread_create $(NAME): $(NAME).o g++ -g -o $(NAME) $(NAME).o -lpthread -luser32 -lkernel32 $(NAME).o: $(NAME).cpp g++ -g -c $(NAME).cpp |
1 2 | make -f makefile.txt cmd |
