任務
將整數、字符串、CString作爲參數傳遞給線程。
代碼
#include "windows.h"
#include "iostream"
#include "atlstr.h"
using namespace std;
void Thread0(LPVOID lp) {
for (int i = 1; i < 100; i++) {
Sleep(1000);
cout << "thread0 " << (int)lp << endl;
}
}
void Thread1(LPVOID lp) {
char *p = (char *)lp;
Sleep(1000);
cout << "thread1 " << *p<<endl;
}
void Thread2(LPVOID lp) {
CString *p = (CString *)lp;
Sleep(1000);
cout << "thread2 " << *p<<endl;
}
HANDLE thrd0, thrd1, thrd2;
DWORD thid0, thid1, thid2;
int main(int argc, char **argv) {
int a = 888;
char s1[] = "abcdef";
CString s2("abcdefgh");
cout << "main thread" << endl;
thrd0 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)Thread0, (void *)a, 0, &thid0);
thrd1 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)Thread1, s1, 0, &thid1);
thrd2 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)Thread2, (void *)&s2, 0, &thid2);
Sleep(1000);
return 0;
}
結果
輸出是亂的因爲未進行線程同步。