POSIX线程问题 [英] Posix threads problem

查看:115
本文介绍了POSIX线程问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解的例子pthreads的。我已经做了如下code,它是给不同的答案,每次我跑!任何人都可以解释的错误吗?
TIA,
Sviiya

在code是在这里:

 的#include< pthreads.h中>
#包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;#定义NUM_THREADS 4结构thread_data {
        INT THREAD_ID;
        INT总和;
};结构thread_data thread_data_array [NUM_THREADS];无效* PrintHello(无效* threadarg)
{
        结构thread_data * my_data;
        INT TASKID;
        INT总和;        my_data =(结构thread_data *)threadarg;
        任务id = my_data->的thread_id;
        综上所述= my_data->和;        的printf(你好!世界是我,线#%d个\\ N,任务id);        返回my_data;
}诠释的main()
{
        的pthread_t线程[NUM_THREADS];
        INT RC;
        长吨;
        INT总和= 0;        对于(t = 0; T< NUM_THREADS;吨++){
                的printf(嗨%LD \\ n,T);                螺纹[T] = T;                thread_data_array [T] .thread_id = T;
                thread_data_array [T]的.sum =总和;
                RC =在pthread_create(安培;螺纹[T],NULL,PrintHello,(无效*)及thread_data_array [T]);
        }        返回0;
}

输出是在这里:

  [用户@办公室TMP] $ ./a.out
嗨! 0
嗨! 1
你好,世界!这就是我,线程0#
你好,世界!这就是我,线#1
嗨! 2
嗨! 3
你好,世界!这就是我,线#2
[用户@办公室TMP] $ ./a.out
嗨! 0
嗨! 1
你好,世界!这就是我,线程0#
你好,世界!这就是我,线#1
嗨! 2
嗨! 3
你好,世界!这就是我,线#2
你好,世界!这就是我,线#3
[用户@办公室TMP] $ ./a.out
嗨! 0
你好,世界!这就是我,线程0#
嗨! 1
你好,世界!这就是我,线#1
嗨! 2
你好,世界!这就是我,线#2
嗨! 3
你好,世界!这就是我,线#3
[用户@办公室TMP] $ ./a.out
嗨! 0
嗨! 1
你好,世界!这就是我,线程0#
嗨! 2
嗨! 3
你好,世界!这就是我,线#3
[用户@办公室TMP] $


解决方案

有没有错误,这只是线程是如何工作的。你的主线程创建新线程,这是在这一点上准备运行。在时间(由操作系统来确定)的一些点,你的主线程将被暂停,其他线程中的一个将关于特定量的时间(通常为几十毫秒)执行。该系统将保持线程之间切换,同时存在正​​在运行的线程的程序。

在时间的确切点,当你的主线程将被中断,其他线程可以打印的Hello World将取决于操作系统调度决定什么的基础上,多长时间你的主线程已经在运行,在其他活动系统外部(I / O)活动等。

修改以包括以下我的评论:

为什么你看到3个中原因,然后是4,那么只有2你好世界,是你创建的线程,但你没有等他们真正得到调度和运行。当你的主循环结束时,程序是死的,你的其他线程而不管有机会运行。如果你希望所有的线程来完成,你需要做一个在pthread_join 在每个线程之前,从main返回。

I am trying to understand pthreads by example. I have made the following code that is giving different answers everytime I run! Could anyone explain the bug please? TIA, Sviiya

The code is here:

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

#define NUM_THREADS 4

struct thread_data {
        int  thread_id;
        int  sum;
};

struct thread_data thread_data_array[NUM_THREADS];

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

        my_data = (struct thread_data *) threadarg;
        taskid = my_data->thread_id;
        sum = my_data->sum;

        printf("Hello World! It's me, thread #%d\n", taskid);

        return my_data;
}

int main ()
{
        pthread_t threads[NUM_THREADS];
        int rc;
        long t;
        int sum=0;

        for (t=0; t < NUM_THREADS; t++) {
                printf("Hi! %ld\n", t);

                threads[t] = t;

                thread_data_array[t].thread_id = t;
                thread_data_array[t].sum = sum;
                rc = pthread_create(&threads[t], NULL, PrintHello, (void *) &thread_data_array[t]);
        }

        return 0;
}

The output is here:

[user@office tmp]$ ./a.out 
Hi! 0
Hi! 1
Hello World! It's me, thread #0
Hello World! It's me, thread #1
Hi! 2
Hi! 3
Hello World! It's me, thread #2
[user@office tmp]$ ./a.out 
Hi! 0
Hi! 1
Hello World! It's me, thread #0
Hello World! It's me, thread #1
Hi! 2
Hi! 3
Hello World! It's me, thread #2
Hello World! It's me, thread #3
[user@office tmp]$ ./a.out 
Hi! 0
Hello World! It's me, thread #0
Hi! 1
Hello World! It's me, thread #1
Hi! 2
Hello World! It's me, thread #2
Hi! 3
Hello World! It's me, thread #3
[user@office tmp]$ ./a.out 
Hi! 0
Hi! 1
Hello World! It's me, thread #0
Hi! 2
Hi! 3
Hello World! It's me, thread #3
[user@office tmp]$

解决方案

There is no bug, this is just how threads work. Your main thread creates new threads, which are at that point "ready to run". At some point in time (determined by the OS), your main thread will be suspended and one of the other threads will run for a certain amount of time (usually a few tens of milliseconds). The system will keep switching between threads while there exist running threads for your program.

The exact point in time when your main thread will be interrupted and one of the other threads can print it's Hello World will depend on what the OS scheduler decides, based on how long your main thread is already running, other activity in the system, external (I/O) events, etc.

EDIT to include my comment below:

The reason why you're seeing 3, then 4, then only 2 "Hello Worlds", is that you created the threads, but you didn't wait for them to actually get scheduled and run. When your main loop ends, the program is dead, irrespective whether your other threads have had a chance to run. If you want all of your threads to be finished, you need to do a pthread_join on each of your threads before returning from main.

这篇关于POSIX线程问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆