注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
挂的迷迷糊糊, 贴个代码帮助后人1. 分ID
#include<iostream>
#include<vector>
using namespace std;
struct allocateid{
vector<bool> vectbool;
int capacity;
int internal;
allocateid(int _s ){
vectbool.resize( _s*2 - 1, false);
capacity= _s;
internal = capacity - 2 ;
};
void _update(int idx , bool val ){
vectbool[idx] = val;
while(idx >=0 ){
int parent = ( (idx - 1) >> 1 );
int left = parent*2 + 1;
int right = left == vectbool.size() -1 ? left : parent*2 + 2;
vectbool[parent] = vectbool[left] && vectbool[right];
idx = parent;
if(idx ==0)
return;
}
}
void release(int x){
_update(x+ internal, false);
}
int allocate(){
int idx = 0;
int res = -1;
while(idx <= internal ){
if( vectbool[idx] == 1)
return -1;
int left = idx*2 + 1;
int right = idx*2 + 2;
if( vectbool[left] == false ){
if(left > internal ){
res=left;
break;
}else
idx = left;
}else if( vectbool[right] == false ){
if(right > internal ){
res=right;
break;
}else
idx = right;
}els;
int res = tq.try_pop();
if(res>0){
all_done = false;
cout<<"thread id="<<id<<" working on task= "<<res<<endl;
this_thread::sleep_for(chrono::milliseconds(500));
break;
}
}
if(all_done){
break;
}
}
cout<<"Exiting thread id="<<id<<endl;
}
};
int main(){
Scheduler s;
for(int i = 0 ; i<50;i++){
s.tqs[i%s.capacity].push(i+10);
}
this_thread::sleep_for(chrono::seconds(50));
cout<<"doen looping cleaning up \n";
return 0;
}
之后就是设计 API和behavioral。 就当攒人品了。
|