#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 1e2, Q = N;
using ll = long long;
int n, q;
ll a[N];
int sta[N];
struct q_t{
int l, r;
}que[Q];
ll res[N];
ll query(int l, int r){
ll sum = 0;
for(ll i = l ;i <= r; ++i) sum += a[i];
return sum;
}
void make_query(int id){
res[id] = query(que[id].l, que[id].r);
std :: this_thread :: sleep_for(chrono :: milliseconds(50));
}
struct thread_guard{
thread thr;
bool alive;
template<typename Ftp>
thread_guard(const Ftp& func_){
static auto func = func_;
static auto func1 = [&](bool& myvis){
func();
myvis = false;
};
alive = true;
thr = thread(func1, std::ref(alive));
}
~thread_guard(){
if(thr.joinable()) thr.join();
}
bool is_alive()const{
return alive;
}
};
int main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> q;
for(int i = 1; i <= n; ++i){
cin >> a[i];
}
for(int i = 1; i <= q; ++i){
cin >>que[i].l >> que[i].r;
}
int id = 1;
thread_guard thr( std::move(bind(make_query, 1)));
cout << thr.is_alive() << endl;
std :: this_thread :: sleep_for(chrono :: milliseconds(150));
cout << thr.is_alive() << endl;
return 0;
}
标签:thread,int,ll,c++,guard,alive,func,检测线
From: https://www.cnblogs.com/cdsidi/p/17112508.html