【问题标题】:My g_thread_push is not working我的 g_thread_push 不工作
【发布时间】:2011-06-27 07:11:41
【问题描述】:

我正在尝试创建一个多线程 jpg 旋转程序,但我无法让 g_thread 工作。

int processUserRequest (UserRequest *uRequest,
            char * const* argv, int argc, int optind){
  struct RotationData CurData;
  CurData.argv=argv;
  CurData.argc=argc;
  CurData.optind=optind;
  CurData.uRequest=uRequest;
  gpointer user_data = &CurData;
  int transform = FALSE;
  int max_files = argc - optind;
  int i;
  gpointer data=&i;
  GThreadPool *pool;
  if(!g_thread_supported())
       g_thread_init(NULL);
pool = g_thread_pool_new(MultiThreadRotation,user_data, 5, TRUE, NULL); for(i=0;i { g_thread_pool_push(pool, &data,NULL); } //g_thread_pool_free (pool, TRUE,TRUE); //Create a montage file transform = createMontageFile (uRequest); return transform; }

MultiThreadRotation 函数应该是被 g_thread_pool_push 调用的,但是一次就不行了。谁能帮忙,我是新手。

另外,我考虑过从 g_thread_pool_push 输出错误,你将如何输出 GError *错误消息?

【问题讨论】:

    标签: c threadpool


    【解决方案1】:

    首先,在您粘贴的代码中,for 语句中有一个错误。

    假设这是固定的,这里有几点说明。

    1. 我不确定为什么会失败,但是您可以从 GError 的“消息”成员中获得一些指示,这是一个人类可读的 C 字符串,您可以将其与 printf() 或您喜欢的任何内容一起使用。不幸的是,您在 g_thread_*() 调用中将 GError 参数设置为 NULL。
    2. 此例程泄漏线程池;您应该在退出之前调用 g_thread_pool_free()。
    3. 如果您正在程序中执行其他线程,并且您关心性能,您应该仔细考虑您希望这些线程是独占的还是共享的。这是通过您设置为 TRUE(独占)的 g_thread_pool_new() 的参数设置的。

    【讨论】:

      猜你喜欢
      • 2012-05-22
      • 2018-09-25
      • 2016-08-27
      • 2018-12-23
      • 2012-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多