代码之家  ›  专栏  ›  技术社区  ›  Juan Francisco Garcia

执行线程时更改C trhead参数

  •  0
  • Juan Francisco Garcia  · 技术社区  · 5 年前

    我需要动态更新线程函数的参数。函数接收3个参数,并且:

    • 股票市场的结构

    void * operation_executer(void *args){
       pthread_mutex_lock(&marketMutex);
    
       struct exec_info *execData = args;        //Parsing data from parameters
       stock_market *market = execData->market;  // Create stock market structure
       pthread_mutex_unlock(&marketMutex);
    
       pthread_mutex_lock(execData->exit_mutex);
       // Waits until exit flag it's 1
       while(*(execData->exit) == 0){           
            pthread_cond_wait(&exitCond, execData->exit_mutex);
       }
       pthread_mutex_unlock(execData->exit_mutex);
    
       pthread_mutex_lock(&marketMutex);
    
        struct operation op;
        while(operations_queue_empty(market->stock_operations) == 0){
                dequeue_operation(market->stock_operations, &op);
                process_operation(market, &op);
        }
        pthread_mutex_unlock(&marketMutex);
    }
    
    

    #include "include/concurrency_layer.h";
    
    int main(){
       exit = 0;
        exec_info info_ex1;
        info_ex1.market = &market_madrid;
        info_ex1.exit = &exit;
        info_ex1.exit_mutex = &exit_mutex;
    
    
    
        pthread_create(&(tid[1]), NULL, &operation_executer, (void*) &info_ex1);
    
        pthread_mutex_lock(&exit_mutex);
        exit = 1;
        pthread_mutex_unlock(&exit_mutex);
    
        pthread_join(tid[1],&res);
    }
    

    其思想是函数必须在 exit=1

    0 回复  |  直到 5 年前
        1
  •  0
  •   MayurK    5 年前

    我认为线程将在 pthread_cond_wait() 在里面 operation_executer() 因为你没有打电话 pthread_cond_singal() 在你的节目里。如果你幸运的话 exit = 1; 先执行 main() while(*(execData->exit) == 0)

    您需要进行以下更改。

    1. 制造后从主管道 出口=1; 如下所示。

      pthread_mutex_lock(&exit_mutex); exit = 1; pthread_cond_singal(&exitCond); pthread_mutex_unlock(&exit_mutex);

    2. 你可以改变 while(*(execData->exit)==0) if(*(execData->exit) == 0) 在里面 exit 只标记一次。