typedef struct Thread ...{ sys_thread_handle thread_id; //relate to system thread handle; for Linux pthread lib, it can be pthread_t; bool stoped; //thread stop flag; void*data; //data relate to thread } thread_t; //create the thread, this is easy. thread_t* Thread_create(void* func, int prio, int stacksize, void* args) ...{ //this is defferent from systems, but most of it is the same. thread_t thread = (thread_t*)malloc(sizeof(thread_t)); thread->stopped =false; //init data field thread->data = malloc(/**//*....*/); sys_thread thread_id = thread_create(func, (void*)thread/**//*the arg*/); thread->thread_id = sys_thread_id; return thread; } //exit the thread, this is hard int Thread_exit(thread_t *thread) ...{ thread->stopped =true; int error = pthraed_join(thread_id); //wait for the thread to exit itself. if(error) ...{ //error handle } free(thread->data); //data malloc by calling thread, so free it also in calling thread free(thread); // return0; // or other if error. } //thread function, important void* thread_task(void*arg) ...{ thread_t *self = (thread_t*)arg; //init data; void* data, otherdata; otherdata = self->data; data = malloc(/**//*.....*/); //.... while(!self->stopped) ...{ //some useful task; } //the data is malloc in the thread, so free it. free(data); return0; }