5.2 向线程传递参数

编程入门 行业动态 更新时间:2024-10-26 17:32:43

5.2 向<a href=https://www.elefans.com/category/jswz/34/1771240.html style=线程传递参数"/>

5.2 向线程传递参数

        pthread_create()允许编程人员向线程的执行方法中传入一个参数,对于需要传递多个参数的情况,可以将这些参数封装到一个结构体中,然后将结构体对象的指针作为参数进行传入。传入的参数必须为(void *)类型。

        问题:考虑到线程启动和调度的不确定性,如何将参数安全地传递给创建的线程?

        回答:确保传入的参数都是线程安全的——这意味着它不能被其他线程修改。下面的三个例子说明了正确和错误的做法。

示例1

        这段代码展示了如何向线程中传递一个整数。主线程中每个子线程使用独有的一份数据内存进行传输,确保每个线程参数在传递过程中互不干扰。

/******************************************************************************
* FILE: hello_arg1.c
* DESCRIPTION:
*   A "hello world" Pthreads program which demonstrates one safe way
*   to pass arguments to threads during thread creation.
* AUTHOR: Blaise Barney
* LAST REVISED: 08/04/15
******************************************************************************/
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_THREADS	8char *messages[NUM_THREADS];void *PrintHello(void *threadid)
{long taskid;sleep(1);taskid = (long) threadid;printf("Thread %d: %s\n", taskid, messages[taskid]);pthread_exit(NULL);
}int main(int argc, char *argv[])
{pthread_t threads[NUM_THREADS];long taskids[NUM_THREADS];int rc, t;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: Zdravstvuyte, mir!";messages[6] = "Japan: Sekai e konnichiwa!";messages[7] = "Latin: Orbis, te saluto!";for(t=0;t<NUM_THREADS;t++) {taskids[t] = t;printf("Creating thread %d\n", t);rc = pthread_create(&threads[t], NULL, PrintHello, (void *) taskids[t]);if (rc) {printf("ERROR; return code from pthread_create() is %d\n", rc);exit(-1);}}pthread_exit(NULL);
}

        代码输出如下

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 0: English: Hello World!
Thread 1: French: Bonjour, le monde!
Thread 2: Spanish: Hola al mundo
Thread 3: Klingon: Nuq neH!
Thread 4: German: Guten Tag, Welt!
Thread 5: Russian: Zdravstvytye, mir!
Thread 6: Japan: Sekai e konnichiwa!
Thread 7: Latin: Orbis, te saluto!

示例2

        这个例程展示了如何通过结构体设置/传递多个参数,每个线程接收独有的结构体对象。

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);
}

        输出信息如下

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

示例3

        这个例程中进行了错误的参数传递,它把所有线程都能访问的共享内存中的变量t的地址传递给了各个线程。随着循环的进行,t的值可能会发生预料外的改变,从而对使用它的线程产生影响。

/*****************************************************************************
* FILE: hello_arg3.c
* DESCRIPTION:
*   This "hello world" Pthreads program demonstrates an unsafe (incorrect)
*   way to pass thread arguments at thread creation.  In this case, the
*   argument variable is changed by the main thread as it creates new threads.
* AUTHOR: Blaise Barney
* LAST REVISED: 07/16/14
******************************************************************************/
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_THREADS     8void *PrintHello(void *threadid)
{long taskid;sleep(1);taskid = *(long *)threadid;printf("Hello from thread %ld\n", taskid);pthread_exit(NULL);
}int main(int argc, char *argv[])
{pthread_t threads[NUM_THREADS];int rc;long t;for(t=0;t<NUM_THREADS;t++) {printf("Creating thread %ld\n", t);rc = pthread_create(&threads[t], NULL, PrintHello, (void *) &t);if (rc) {printf("ERROR; return code from pthread_create() is %d\n", rc);exit(-1);}}pthread_exit(NULL);
}

        输出信息如下。

Creating thread 0
Creating thread 1
Creating thread 2
Creating thread 3
Creating thread 4
Creating thread 5
Creating thread 6
Creating thread 7
Hello from thread 140737488348392
Hello from thread 140737488348392
Hello from thread 140737488348392
Hello from thread 140737488348392
Hello from thread 140737488348392
Hello from thread 140737488348392
Hello from thread 140737488348392
Hello from thread 140737488348392

更多推荐

5.2 向线程传递参数

本文发布于:2023-11-16 12:42:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1621090.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   参数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!