每次尝试调用某个函数

时间:2017-03-24 21:06:06

标签: c++ multithreading

好的,所以我用线程进行了一项任务。我想假设每一段时间改变当前正在运行的线程,让我们说一秒钟。首先,我创建了一个Thread类:

 typedef unsigned long address_t;
 #define JB_SP 6
 #define JB_PC 7
#define STACK_SIZE (4096)

using namespace std;

class Thread{
public:
enum State{
    BLOCKED,
    READY,
    RUNNING
};
Thread(int tid, void(*f)(void), int stack_size) :
        tid(tid), stack_size(stack_size){
    address_t sp, pc;
    sp = (address_t)stack + STACK_SIZE - sizeof(address_t);
    pc = (address_t)f;
    sigsetjmp(env, 1);
    (env->__jmpbuf)[JB_SP] = translate_address(sp);
    (env->__jmpbuf)[JB_PC] = translate_address(pc);
    sigemptyset(&env->__saved_mask);
    state = READY;
    quantums = 0;
}
Thread (){}

address_t translate_address(address_t addr)
{
    address_t ret;
    asm volatile("xor    %%fs:0x30,%0\n"
            "rol    $0x11,%0\n"
    : "=g" (ret)
    : "0" (addr));
    return ret;
}
State get_state() const
{
    return state;
}
void set_state(State state1)
{
    state = state1;
}
int get_id() const
{
    return tid;
}
pthread_t& get_thread()
{
    return thread;
}

sigjmp_buf& get_env()
{
    return env;
}

void raise_quantums()
{
    quantums ++;
}

int get_quantums()
{
    return quantums;
}
int add_to_sync(int tid)
{
    sync.push_back(tid);
}
bool appear_in_sync_list(int tid)
{
    return (find(sync.begin(), sync.end(), tid) != sync.end());
}
private:
vector<int> sync;
int quantums;
State state;
char stack[STACK_SIZE];
sigjmp_buf env;
pthread_t thread;
int tid;
int stack_size;


};

我有这个改变线程的功能:

void running_thread(int sigNum)
{

sigset_t set;
sigemptyset(&set);
sigaddset(&set, SIGINT);
sigprocmask(SIG_SETMASK, &set, NULL);
total_quantum ++;
if (currentThread.get_state() == Thread::RUNNING)
{
    Thread& t = ready_threads.back();
    ready_threads.pop_back();
    currentThread.set_state(Thread::READY);
    ready_threads.push_back(currentThread);
    sigsetjmp(currentThread.get_env(), 1);
    currentThread = t;
    t.raise_quantums();
    siglongjmp(currentThread.get_env(), 1);
}
if (currentThread.get_state() == Thread::BLOCKED)
{
    Thread &t = ready_threads.back();
    ready_threads.pop_back();
    currentThread.set_state(Thread::BLOCKED);
    blocked_threads.push_back(currentThread);
    sigsetjmp(currentThread.get_env(), 1);
    currentThread = t;
    t.raise_quantums();
    siglongjmp(currentThread.get_env(), 1);

}
sigemptyset(&set);
sigaddset(&set, SIGINT);
sigprocmask(SIG_UNBLOCK, &set, NULL);
}

它实际上并不重要,我的问题是它甚至不被召唤。

我的程序首先调用此函数:

int clock_set()
{
int seconds = quantum / SECOND;
int usecs = quantum - seconds*SECOND;
timer.it_value.tv_sec = seconds;
timer.it_value.tv_usec = usecs;
timer.it_interval.tv_sec = seconds;
timer.it_interval.tv_usec = usecs;
struct sigaction sa;
sa.sa_handler = &running_thread;
if (sigaction(SIGVTALRM, &sa,NULL) < 0) {
    cerr << "system error: sigaction error.";
    return FAILURE;
}

// Start a virtual timer. It counts down whenever this process is executing.
if (setitimer (ITIMER_VIRTUAL, &timer, NULL)) {
    cerr << "system error: setitimer error.";
    return FAILURE;
}
return SUCCESS;
}

基本上我试图让running_thread每秒都激活一次,所以我使用了sigaction和sa_handler。

这是我的主要功能:

int main()
{
uthread_init(1000000) // Initiliaze variable 'quantum' to be a second, this function also calls clock_set
uthread_spawn(&g); // Creating a thread object with function g inserting it to ready_threads vector and to threads vector
uthread_spawn(&f); // creating a thread object with function f inserting it to ready_threads vector and to threads vector



}

向量&#34; ready_threads&#34;有2个线程。 为什么不调用running_thread?

0 个答案:

没有答案