2010-06-06 135 views
-2

承担创建通过在pthread_create 3个工作线程,多任务处理如何使调用无限循环功能

在这些工作线程例行

后工作线程增益控制,每次调用哪个没有回做计数一个简单的无限循环功能

如何在调用无限循环函数之后使工作线程增益控制并保存用于在工作线程中再次调用的无限循环函数的上下文?

+3

这个问题真的很混乱。你能说清楚,根据我的理解,如果你想让线程恢复控制,你应该等待一些共享变量(信号量等) – 2010-06-06 08:07:38

回答

0

让我换句话来看看我是否理解了这个问题。

您有一个主线程,它产生3个工作线程,每个工作线程执行长时间运行(无限)工作。

在您希望中断处理的某个点上,保存所有线程的状态以恢复以后停止的位置。

我认为这样做的最好方法是组织您的线程在事务绑定块中工作。重新启动时,检查上次完成的事务,然后从那里开始。

但是由于我怀疑这是低级别线程管道中的家庭作业分配,我可能会建议一个共享布尔值,在您每次通过循环退出并存储状态之后选中该值。另一种方法是“杀死”线程并捕获异常并存储状态。最后一个选项是混乱的。

+0

如果使用定时器来中断无限循环功能并通过从函数到工作线程的映射返回到工作线程? – user353573 2010-06-06 10:48:18

+0

可以通过pthread_cleanup_push保存无限循环函数的上下文(例如,我们停止的行,函数中的变量)吗? – user353573 2010-06-06 10:50:53

+0

我现在很困惑。如果启动该线程,OS将负责划分线程之间的时间,并且不需要关注上下文。它自己运行。线程也不会“返回”:它们在完成时停止。 正如在下面的帖子中所说的,轮询循环是糟糕的设计,并且最好让线程在等待更多工作时阻塞。使用阻塞队列,读取文件或FIFO,套接字,... – 2010-06-06 14:43:54

0

想做一个线程池的作用,呼吁无限循环功能后,每个工作线程可以改变其他任务(其他无限循环功能)运行

例如3个工作线程可以运行4个任务(无限循环功能)

#ifndef JOB_CPP 
#define JOB_CPP 

#include "job.h" 

#define NUM_OF_TASKS 4 
#define NUM_OF_WORKERS 3 
    void (* job_queue[NUM_OF_TASKS])(void*); 
    void (* fp[NUM_OF_WORKERS])(void*); // original running job 
    int running_task[NUM_OF_WORKERS]; 
    int idle[NUM_OF_TASKS]; 
    int last_running_task[NUM_OF_WORKERS]; 
    int no_of_tasks_running[NUM_OF_WORKERS]; 
    my_struct_t data = {PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, 0}; 

void func1(void *arg) 
{ 
    int count = 0; 
    int status; 
    while(true) 
    { 
     //if((count % 100) == 0) 
     //printf("func1 run %d\n", count); 
     count = count + 1; 
     //status = pthread_cond_signal(&data.cv); 
    } 
} 
void func2(void *arg) 
{ 
    int count = 0; 
    int status; 
    while(true) 
    { 
     //printf("func2 run %d\n", count); 
     count = count + 1; 
     //status = pthread_cond_signal(&data.cv); 
    } 
} 
void func3(void *arg) 
{ int count = 0; 
    int status; 
    while(true) 
    { 
     //printf("func3 run %d\n", count); 
     count = count + 1; 
     //status = pthread_cond_signal(&data.cv); 
    } 
} 
void func4(void *arg) 
{ int count = 0; 
    int status; 
    while(true) 
    { 
     //printf("func4 run %d\n", count); 
     count = count + 1; 
     //status = pthread_cond_signal(&data.done); 
    } 
} 

void jobinit() 
{ 
    for(int i=0; i<NUM_OF_TASKS; i++) 
    { 
     job_queue[i] = NULL; 
     idle[i] = 0; 
    } 
    for(int i=0; i<NUM_OF_WORKERS; i++) 
    { 
     fp[i] = NULL; 
     running_task[i] = 0; 
     last_running_task[i] = 0; 
     no_of_tasks_running[i] = 0; 
    } 
    jobadd(func1); 
    jobadd(func2); 
    jobadd(func3); 
    jobadd(func4); 
    jobrun(); 
} 
void jobadd(void (*job)(void*)) 
{ 
    for(int i=0; i<4; i++) 
    { 
     if(job_queue[i] == NULL) 
     { 
      job_queue[i] = job; 
      return; 
     } 
    } 
} 
void* workserver(void *arg); 
void* workserver(void *arg) 
{ 
    int status, timedout; 

    struct timespec timeout; 

    status = pthread_mutex_lock(&data.mutex); 
    while(true) 
    { 
     timedout = 0; 
     clock_gettime(CLOCK_REALTIME, &timeout); 
     timeout.tv_sec += 2; 

     sleep(1); 
     //void (* clean)(void*); 

     status = pthread_cond_timedwait(&data.cv, &data.mutex, &timeout); 
     if(status == ETIMEDOUT){ 
      printf("worker wait timed out %d\n", (int)arg); 
      timedout = 1; 
     }else if(status != 0){ 
      printf("worker wait failed %d\n", (int)arg); 
      status = pthread_mutex_unlock(&data.mutex); 
      return NULL; 
     } 
     printf("workserver number: %d\n", (int)arg); 

     status = pthread_mutex_unlock(&data.mutex);  

     printf("function run %d\n", (int)arg); 
     (* job_queue[(int)arg])(NULL); 

     printf("cond wait start %d\n", (int)arg); 
     status = pthread_cond_wait(&data.done, &data.mutex); 
     printf("cond wait end\n"); 

     status = pthread_mutex_lock(&data.mutex); 
    } 
} 
void jobrun() 
{ 
    for(int i=0; i<3; i++) {idle[i] = 0;} 
    pthread_t r1_threadid[3]; 

    for(int i=0; i<3; i++) 
    { 
     pthread_create(&r1_threadid[i], NULL, workserver, (void*)i); 
    } 

    int status; 
    struct timespec timeout; 

    timeout.tv_sec = time (NULL) + 2; 
    timeout.tv_nsec = 0; 

    while(true) 
    { 
    status = pthread_mutex_lock(&data.mutex); 
    while(data.value == 0) 
    { 
     status = pthread_cond_timedwait(&data.cond, &data.mutex, &timeout); 
    } 
    if(data.value != 0) 
    { 
     //printf("condition was signaled\n"); 
     data.value = 0; 
    } 
    status = pthread_mutex_unlock(&data.mutex); 
    if(status != 0) 
     printf("unlock mutex error"); 
    } 
} 
#endif 
+0

你真的需要更多地关注你的帖子的格式。这实际上难以辨认。 (内容与格式几乎不可理解。 我怀疑你想要的是找到一些库,它提供了一个“生产者/消费者”队列,以便你的工作者线程都可以在这个库上进行阻塞读取调用(作为消费者)。然后,您的主线程将工作分派到该队列中(或者可以从工作者处馈送:“反馈”)。阻塞呼叫会比一些繁忙的等待轮询“无限循环”更好。退出事件可以被送入队列以终止线程。 – 2010-06-06 08:41:02

+0

对不起,我已经在网页中使用了添加示例代码功能,但是在将代码复制到网页后,它仍然弄得一团糟,不知道我出错的地方 – user353573 2010-06-06 09:11:31

0

我想你应该澄清你的问题。

如果每个工作者线程调用一个无限循环,那么我想你的主线程将不得不在每个线程上调用pthread_cancel()。从我收集的信息可能需要调用其他pthread_*()函数来设置目标线程的“可取消性”。

当然这个建议引发了问题。最好的办法是防止那些无限循环。编写代码以使其具有退出条件......以便工作受到某种输入的限制或具有某种事件处理。

+0

在调用无限循环函数之后,主线程工作线程)取消现有的运行功能 我的意思是当一个线程运行一个任务(无限循环函数)以及工作线程如何使任务在特定点停止并保存上下文并运行另一个任务,然后返回运行先前的任务再次 – user353573 2010-06-06 09:09:08