如何将数组正确传递给具有结构的线程函数

时间:2018-04-03 00:38:11

标签: c multithreading pthreads

几天来,我一直在靠墙打我的头,因为我似乎无法找到解决这个问题的好方法。我需要使用结构将两个数组传递给线程函数。一个数组是我需要求和的结构,另一个是需要存储该求和结果的结果,然后我需要在主程序中最后一次求它们以得到总值。

  $('#uploaddata').submit(function(e){
    e.preventDefault();
    var form = new FormData($("#uploaddata")[0]);
    $.ajax({
      url: albackend,
      type:'POST',
      data: form,
      crossDomain: true,
      processData: false,
      contentType: false,
      success:function(data){
        console.log("success");
        console.log(data);
        $('#fileupload').hide();
        $('#videoselect').show();
      },
      error: function (responseData, textStatus, errorThrown) {
        alert(errorThrown);
      }
    });
  });

1 个答案:

答案 0 :(得分:0)

您可以执行与以下类似的操作:

struct ThreadArgs
{
    int type;
    //others
};

void* Thread(void* thread_args)
{
    int type;
    pthread_detach(pthread_self());
    type = ((struct ThreadArgs *) thread_args)->type;
    return NULL;
}

int main()
{
    pthread_t thread_id;
    struct ThreadArgs* thread_args;
    thread_args = (struct ThreadArgs *) malloc(sizeof(struct ThreadArgs));
    if (thread_args == NULL)
        PanicWithError("malloc() failed");
    thread_args->type = 1;
    if (pthread_create(&thread_id, NULL, Thread, (void *) thread_args) != 0)
         PanicWithError("pthread_create() failed");
    while (1)
    {}
    return 0;
}