【问题标题】:Pthreads problem and a few questionspthreads问题和几个问题
【发布时间】:2009-12-08 05:35:36
【问题描述】:
#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define NUM_THREADS 8

char *messages[NUM_THREADS];

struct thread_data
{
   int thread_id;
   int  sum;
   char *message;
};

struct thread_data thread_data_array[NUM_THREADS];

void *PrintHello(void *threadarg)
{
   int taskid, sum;
   char *hello_msg;
   struct thread_data *my_data;

   sleep(1);
   my_data = (struct thread_data *) threadarg;
   taskid = my_data->thread_id;
   sum = my_data->sum;
   hello_msg = my_data->message;
   printf("Thread %d: %s  Sum=%d\n", taskid, hello_msg, sum);
   pthread_exit(NULL);
}

int main(int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
//int *taskids[NUM_THREADS];
int rc, t, sum;

sum=0;
messages[0] = "English: Hello World!";
messages[1] = "French: Bonjour, le monde!";
messages[2] = "Spanish: Hola al mundo";
messages[3] = "Klingon: Nuq neH!";
messages[4] = "German: Guten Tag, Welt!"; 
messages[5] = "Russian: Zdravstvytye, mir!";
messages[6] = "Japan: Sekai e konnichiwa!";
messages[7] = "Latin: Orbis, te saluto!";

for(t=0;t<NUM_THREADS;t++) {
  sum = sum + t;
  thread_data_array[t].thread_id = t;
  thread_data_array[t].sum = sum;
  thread_data_array[t].message = messages[t];
  printf("Creating thread %d\n", t);
  rc = pthread_create(&threads[t], NULL, PrintHello, (void *)&thread_data_array[t]);
  if (rc) {
    printf("ERROR; return code from pthread_create() is %d\n", rc);
    exit(-1);
    }
  }
pthread_exit(NULL);
}

上面的代码给我一个使用 gcc 在 linux 中的分段错误

作为初学者,我有几个关于线程的问题

  1. 如果同时创建了 3 个线程,如果它们必须退出,那么最先创建的线程最后会退出吗?
  2. 线程创建和终止程序每次的 o/p 是否都不同?如果是,为什么?(我注意到他们这样做了);

【问题讨论】:

  • 我已经在 QNX 中尝试过您的程序,效果很好。您是否尝试过编译和运行类似 int main() { printf("Hello world\n"); return 0;} 之类的简单程序?

标签: c++ c multithreading pthreads


【解决方案1】:

如果同时创建了 3 个线程,如果它们必须退出,那么首先创建的线程最后会退出吗?

回答:不能保证第一个线程应该首先退出。一旦它们被创建,它们就会被操作系统视为单独的实体,由操作系统决定哪个将首先退出。

每次线程创建和终止程序的 o/p 是否都不同,如果是,为什么? (我注意到他们这样做了);

答案:是的,它们确实不同,原因与上面解释的相同。

【讨论】:

    【解决方案2】:

    每个线程的调度方式和时间完全取决于操作系统。 如果不深入研究操作系统代码,就无法理解这一点。

    在大多数 pthread 的实现中(我没有全部使用,但我已经全部使用了)。
    当主线程退出时,所有其他线程停止执行,应用程序退出。

    因此,在退出主线程之前应该等待(或杀死)所有子线程才能退出。
    为此,我们有 pthread_join()。

    for(t=0;t<NUM_THREADS;t++)
    {
        void* result;
        pthread_join(threads[t],&result);
    }
    // Now all the children are dead.
    

    注意底层 io 系统不是线程安全的。
    因此,如果多个线程写入 IO,那么您可能会遇到一些交错。

    【讨论】:

      【解决方案3】:

      您的代码运行良好(我也在使用 gcc)..

      输出:

      Creating thread 0
      Creating thread 1
      Creating thread 2
      Creating thread 3
      Creating thread 4
      Creating thread 5
      Creating thread 6
      Creating thread 7
      Thread 1: French: Bonjour, le monde!  Sum=1
      Thread 0: English: Hello World!  Sum=0
      Thread 2: Spanish: Hola al mundo  Sum=3
      Thread 4: German: Guten Tag, Welt!  Sum=10
      Thread 3: Klingon: Nuq neH!  Sum=6
      Thread 5: Russian: Zdravstvytye, mir!  Sum=15
      Thread 6: Japan: Sekai e konnichiwa!  Sum=21
      Thread 7: Latin: Orbis, te saluto!  Sum=28
      

      【讨论】:

        猜你喜欢
        • 2011-06-14
        • 1970-01-01
        • 1970-01-01
        • 2010-12-15
        • 1970-01-01
        • 2013-06-30
        • 2015-01-03
        • 2010-10-21
        • 1970-01-01
        相关资源
        最近更新 更多