1.在时间范围内主线程等待子线程的操作
#include"stdafx.h"
#include <iostream>
#include<iomanip>
//#include<unistd.h>
#include<windows.h>
#include<stdlib.h>
#include<time.h>
#include<thread>
using namespace std;
int temp = 0;
void thread01()
{
for (int i = 0; i < 10; i++)
{
cout << "Thread 01 is working !" << endl<<endl;
//打印当前系统时间
time_t timep = time(&timep); //获取从1970至今过了多少秒,存入time_t类型的timep
char buff[36];
ctime_s(buff, sizeof(buff), &timep);
printf("%s\n", buff); //用ctime将秒数转化成字符串格式,输出:Tue Nov 5 02:52:05 2019
cout << "Hello,CodeWorld!" << endl;
//打印当前系统时间和问候语
//生成并输出随机数
temp = rand() % 11;
cout << "生成的随机数为:" << temp << endl << endl;
Sleep(temp * 1000);
//生成并输出随机数
}
}
void thread02()
{
for (int i = 0; i < 10; i++)
{
cout << "Thread 02 is working !" << endl<<endl;
//打印当前系统时间
time_t timep = time(&timep); //获取从1970至今过了多少秒,存入time_t类型的timep
char buff[36];
ctime_s(buff, sizeof(buff), &timep);
printf("%s\n", buff); //用ctime将秒数转化成字符串格式,输出:Tue Nov 5 02:52:05 2019
cout << "Hello,CodeWorld!" << endl;
//打印当前系统时间和问候语
//生成并输出随机数
temp = rand() % 11;
cout << "生成的随机数为:" << temp << endl << endl;
Sleep(temp * 1000);
//生成并输出随机数
}
}
int main()
{
//生成随机数
srand((unsigned)time(NULL));
//生成随机数
thread task01(thread01);
thread task02(thread02);
task01.join();
task02.join();
for (int i = 0; i < 10; i++)
{
cout << "Main thread is working !" << endl;
Sleep(1000);
}
system("pause");
return 0;
}
实验结果:
Thread 01 is working !
Thread 02 is working !
Wed Nov 20 16:10:18 2019
Hello,CodeWorld!
生成的随机数为:Wed Nov 20 16:10:18 2019
Hello,CodeWorld!
生成的随机数为:8
8
Thread 01 is working !Thread 02 is working !
Wed Nov 20 16:10:26 2019
Hello,CodeWorld!
生成的随机数为:9
Wed Nov 20 16:10:26 2019
Hello,CodeWorld!
生成的随机数为:9
Thread 02 is working !
Thread 01 is working !
Wed Nov 20 16:10:35 2019
Hello,CodeWorld!Wed Nov 20 16:10:35 2019
Hello,CodeWorld!
生成的随机数为:9
生成的随机数为:9
Thread 02 is working !
Thread 01 is working !Wed Nov 20 16:10:44 2019
Hello,CodeWorld!
生成的随机数为:1
Wed Nov 20 16:10:44 2019
Hello,CodeWorld!
生成的随机数为:1
Thread 02 is working !
Wed Nov 20 16:10:45 2019
Hello,CodeWorld!
生成的随机数为:7
Thread 01 is working !
Wed Nov 20 16:10:45 2019
Hello,CodeWorld!
生成的随机数为:7
Thread 02 is working !
Wed Nov 20 16:10:52 2019
Thread 01 is working !
Hello,CodeWorld!Wed Nov 20 16:10:52 2019
Hello,CodeWorld!
生成的随机数为:5
生成的随机数为:5
Thread 02 is working !
Thread 01 is working !
Wed Nov 20 16:10:57 2019
Hello,CodeWorld!
生成的随机数为:5
Wed Nov 20 16:10:57 2019
Hello,CodeWorld!
生成的随机数为:5
Thread 02 is working !
Wed Nov 20 16:11:02 2019
Hello,CodeWorld!
生成的随机数为:10
Thread 01 is working !
Wed Nov 20 16:11:02 2019
Hello,CodeWorld!
生成的随机数为:10
Thread 02 is working !
Wed Nov 20 16:11:12 2019
Hello,CodeWorld!
Thread 01 is working !
生成的随机数为:1
Wed Nov 20 16:11:12 2019
Hello,CodeWorld!
生成的随机数为:1
Thread 02 is working !
Thread 01 is working !
Wed Nov 20 16:11:13 2019
Hello,CodeWorld!
生成的随机数为:0
Wed Nov 20 16:11:13 2019
Hello,CodeWorld!
生成的随机数为:0
Main thread is working !
Main thread is working !
Main thread is working !
Main thread is working !
Main thread is working !
Main thread is working !
Main thread is working !
Main thread is working !
Main thread is working !
Main thread is working !
请按任意键继续. . .
2.主线程和子线程互不干扰
#include"stdafx.h"
#include <iostream>
#include<iomanip>
//#include<unistd.h>
#include<windows.h>
#include<stdlib.h>
#include<time.h>
#include<thread>
using namespace std;
int temp = 0;
void thread01()
{
for (int i = 0; i < 10; i++)
{
cout << "Thread 01 is working !" << endl<<endl;
//打印当前系统时间
time_t timep = time(&timep); //获取从1970至今过了多少秒,存入time_t类型的timep
char buff[36];
ctime_s(buff, sizeof(buff), &timep);
printf("%s\n", buff); //用ctime将秒数转化成字符串格式,输出:Tue Nov 5 02:52:05 2019
cout << "Hello,CodeWorld!" << endl;
//打印当前系统时间和问候语
//生成并输出随机数
temp = rand() % 11;
cout << "生成的随机数为:" << temp << endl << endl;
Sleep(temp * 1000);
//生成并输出随机数
}
}
void thread02()
{
for (int i = 0; i < 10; i++)
{
cout << "Thread 02 is working !" << endl<<endl;
//打印当前系统时间
time_t timep = time(&timep); //获取从1970至今过了多少秒,存入time_t类型的timep
char buff[36];
ctime_s(buff, sizeof(buff), &timep);
printf("%s\n", buff); //用ctime将秒数转化成字符串格式,输出:Tue Nov 5 02:52:05 2019
cout << "Hello,CodeWorld!" << endl;
//打印当前系统时间和问候语
//生成并输出随机数
temp = rand() % 11;
cout << "生成的随机数为:" << temp << endl << endl;
Sleep(temp * 1000);
//生成并输出随机数
}
}
int main()
{
//生成随机数
srand((unsigned)time(NULL));
//生成随机数
thread task01(thread01);
thread task02(thread02);
task01.detach();
task02.detach();
for (int i = 0; i < 10; i++)
{
cout << "Main thread is working !" << endl;
Sleep(1000);
}
system("pause");
return 0;
}
实验结果:
Thread 01 is working !
Main thread is working !
Thread 02 is working !
Wed Nov 20 16:17:17 2019
Hello,CodeWorld!
生成的随机数为:8
Wed Nov 20 16:17:17 2019
Hello,CodeWorld!
生成的随机数为:8
Main thread is working !
Main thread is working !
Main thread is working !
Main thread is working !
Main thread is working !
Main thread is working !
Main thread is working !
Thread 01 is working !
Thread 02 is working !
Wed Nov 20 16:17:25 2019
Hello,CodeWorld!
生成的随机数为:9
Wed Nov 20 16:17:25 2019
Hello,CodeWorld!
生成的随机数为:9
Main thread is working !
Main thread is working !
请按任意键继续. . . Thread 02 is working !
Wed Nov 20 16:17:34 2019
Hello,CodeWorld!
生成的随机数为:9Thread 01 is working !
Wed Nov 20 16:17:34 2019
Hello,CodeWorld!
生成的随机数为:9
Thread 02 is working !
Wed Nov 20 16:17:43 2019
Hello,CodeWorld!
生成的随机数为:1
Thread 01 is working !
Wed Nov 20 16:17:43 2019
Hello,CodeWorld!
生成的随机数为:1
Thread 02 is working !
Wed Nov 20 16:17:44 2019
Hello,CodeWorld!
生成的随机数为:7
Thread 01 is working !
Wed Nov 20 16:17:44 2019
Hello,CodeWorld!
生成的随机数为:7
Thread 02 is working !
Wed Nov 20 16:17:51 2019
Hello,CodeWorld!Thread 01 is working !
生成的随机数为:5
Wed Nov 20 16:17:51 2019
Hello,CodeWorld!
生成的随机数为:5
Thread 01 is working !
Thread 02 is working !
Wed Nov 20 16:17:56 2019
Hello,CodeWorld!Wed Nov 20 16:17:56 2019
Hello,CodeWorld!
生成的随机数为:5
生成的随机数为:5
Thread 02 is working !
Wed Nov 20 16:18:01 2019
Hello,CodeWorld!
生成的随机数为:10
Thread 01 is working !
Wed Nov 20 16:18:01 2019
Hello,CodeWorld!
生成的随机数为:10
Thread 02 is working !
Wed Nov 20 16:18:11 2019
Hello,CodeWorld!
生成的随机数为:1
Thread 01 is working !
Wed Nov 20 16:18:11 2019
Hello,CodeWorld!
生成的随机数为:1
Thread 02 is working !
Wed Nov 20 16:18:12 2019
Hello,CodeWorld!
生成的随机数为:0
Thread 01 is working !
Wed Nov 20 16:18:12 2019
Hello,CodeWorld!
生成的随机数为:0
Linux平台下程序案例
#include <iostream>
#include<iomanip>
#include<unistd.h>
#include<stdlib.h>
#include<time.h>
#include<thread>
using namespace std;
int temp = 0;
void thread01()
{
for (int i = 0; i < 10; i++)
{
cout << "Thread 01 is working !" << endl<<endl;
//打印当前系统时间
time_t timep; //获取从1970至今过了多少秒,存入time_t类型的timep
time(&timep);
printf("%s\n", ctime(&timep)); //用ctime将秒数转化成字符串格式,输出:Tue Nov 5 02:52:05 2019
cout << "Hello,CodeWorld!" << endl;
//打印当前系统时间和问候语
//生成并输出随机数
temp = rand() % 11;
cout << "生成的随机数为:" << temp << endl << endl;
sleep(temp * 1000);
//生成并输出随机数
}
}
void thread02()
{
for (int i = 0; i < 10; i++)
{
cout << "Thread 02 is working !" << endl<<endl;
//打印当前系统时间
time_t timep; //获取从1970至今过了多少秒,存入time_t类型的timep
time(&timep);
printf("%s\n", ctime(&timep)); //用ctime将秒数转化成字符串格式,输出:Tue Nov 5 02:52:05 2019
cout << "Hello,CodeWorld!" << endl;
//打印当前系统时间和问候语
//生成并输出随机数
temp = rand() % 11;
cout << "生成的随机数为:" << temp << endl << endl;
sleep(temp * 1000);
//生成并输出随机数
}
}
int main()
{
//生成随机数
//int count = 10;
//int i = 0;
srand((unsigned)time(NULL));
//生成随机数
thread task01(thread01);
thread task02(thread02);
task01.detach();
task02.detach();
for (int i = 0; i < 10; i++)
{
cout << "Main thread is working !" << endl;
sleep(1000);
}
system("pause");
return 0;
}
Thread 01 is working !
Main thread is working !
Thu Nov 21 02:59:09 2019
Hello,CodeWorld!
生成的随机数为:0
Thread 02 is working !
Thu Nov 21 02:59:09 2019
Hello,CodeWorld!
生成的随机数为:7
Thread 01 is working !
Thu Nov 21 02:59:09 2019
Hello,CodeWorld!
生成的随机数为:5