WINAPI:
#include
#include
DWORD WINAPI tf1(void {
int n = 10;
while (n--) std::cout << "thread #1\n";
return 0;
}
DWORD WINAPI tf2(void {
int n = 10;
while (n--) std::cout << "thread #2\n";
return 0;
}
int main() {
HANDLE t1, t2;
t1 = CreateThread(0, 0, tf1, 0, 0, 0);
t2 = CreateThread(0, 0, tf2, 0, 0, 0);
WaitForMultipleObjects(2, &t1, TRUE, INFINITE);
return 0;
}
PTHREADS:
#include
#include
void *tf1(void {
int n = 10;
while (n--) std::cout << "thread #1\n";
return 0;
}
void *tf2(void {
int n = 10;
while (n--) std::cout << "thread #2\n";
return 0;
}
int main() {
pthread_t t1, t2;
pthread_create(&t1, 0, tf1, 0);
pthread_create(&t2, 0, tf2, 0);
pthread_join(t1, 0);
pthread_join(t2, 0);
return 0;
}
C++11:
#include
#include
void tf1() {
int n = 10;
while (n--) std::cout << "thread #1" << std::endl;
}
void tf2() {
int n = 10;
while (n--) std::cout << "thread #2" << std::endl;
}
int main() {
std::thread t1(tf1);
std::thread t2(tf2);
t1.join();
t2.join();
return 0;
}