📄 thread.c
字号:
#include <sys/wait.h>#include <signal.h>#include <stdio.h>#include <unistd.h>#include "thread.h"Thread::Thread(int synchronous = 0, int realtime = 0){ this->synchronous = synchronous; this->realtime = realtime; tid = (pthread_t)-1;}Thread::~Thread(){}void* Thread::entrypoint(void *parameters){ Thread *pt = static_cast<Thread *>(parameters);// allow thread to be cancelled in the middle of something pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0); pt->run();}void Thread::start(){ pthread_attr_t attr; struct sched_param param; pthread_attr_init( &attr );// caused SEGFLT when reading from files if(!synchronous) pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED ); if(realtime) { if(pthread_attr_setschedpolicy(&attr, SCHED_RR) < 0) printf("Couldn't set realtime thread.\n"); param.sched_priority = 50; if(pthread_attr_setschedparam(&attr, ¶m) < 0) printf("Couldn't set realtime thread.\n");; } pthread_create(&tid, &attr, Thread::entrypoint, this);}Thread::end(pthread_t tid) // need to join after this if synchronous{ if(tid >= 0) pthread_cancel(tid);}Thread::end() // need to join after this if synchronous{ if(tid >= 0) pthread_cancel(tid); if(!synchronous) tid = (pthread_t)-1;}Thread::join() // join this thread{ if(tid >= 0) pthread_join(tid, 0); tid = (pthread_t)-1;}Thread::exit_thread(){ pthread_exit(0); if(!synchronous) tid = (pthread_t)-1;}Thread::suspend_thread(){ if(tid >= 0) pthread_kill(tid, SIGSTOP);}Thread::continue_thread(){ if(tid >= 0) pthread_kill(tid, SIGCONT);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -