广州市网站建设企业,wordpress链接不对清除缓存文件,个人网站上传有啥要求,网站开发工具 哪个好pthread_create()允许编程人员向线程的执行方法中传入一个参数#xff0c;对于需要传递多个参数的情况#xff0c;可以将这些参数封装到一个结构体中#xff0c;然后将结构体对象的指针作为参数进行传入。传入的参数必须为(void *)类型。 问题#xff1a;考虑到线程启动和调… 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(t0;tNUM_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;sum0;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(t0;tNUM_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! Sum0
Thread 1: French: Bonjour, le monde! Sum1
Thread 2: Spanish: Hola al mundo Sum3
Thread 3: Klingon: Nuq neH! Sum6
Thread 4: German: Guten Tag, Welt! Sum10
Thread 5: Russian: Zdravstvytye, mir! Sum15
Thread 6: Japan: Sekai e konnichiwa! Sum21
Thread 7: Latin: Orbis, te saluto! Sum28
示例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(t0;tNUM_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