64,654
社区成员
发帖
与我相关
我的任务
分享
// Highthread.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <thread>
#include <iostream>
#include <fstream>
#include <time.h>
#include "mypool.h"
#include <windows.h>
using namespace std;
mutex mylock;
ofstream logfile,logfile1;
void outp(int a)
{
logfile << a << endl;
logfile.flush();
}
void outpp(int a)
{
logfile1 << a << endl;
logfile1.flush();
}
int main()
{
logfile.open("d:/2.txt");
int i = 0;
while (i < 1000)
{
thread th(outp, i);
th.detach();
i++;
}
ThreadPool myppp(8);
while (i < 1000)
{
//Sleep(1); 加入这句写入的文件才正常
//如果用Sleep来等待才能添加任务,和用线程池来解决高并发任务就违背了,如何解决这个问题呢?
myppp.enqueue([&] {outpp(i); });
i++;
}
cout << "done" << endl;
logfile1.close();
getchar();
return 0;
}