C Pthreads问题,无法传递我想要的信息?
所以我试图让线程启动函数打开一个通过命令行给出的文件,每个线程一个文件,但我还需要启动函数来获取结果数组。所以基本上我需要以某种方式获取一个字符串(文件名)和一个二维结果数组到我的启动线程,我完全困惑了。
有人有任何建议或想法吗?谢谢。
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include "string.h"
void* func(void *args);
int main(int argc, const char * argv[])
{
int nthreads = 0;
int i = 0;
long **results;
printf("Enter number of threads to use:\n> ");
scanf("%d", nthreads);
pthread_t threadArray[nthreads];
// results 2d array; 3 rows by nthreads cols
results = malloc((nthreads*4) * sizeof(long *));
for(i = 0; i<nthreads; i++) {
pthread_create(&threadArray[i], NULL, wordcount, HELP!!!!);
}
for(i = 0; i<nthreads; i++) {
pthread_join(threadArray[i], NULL);
}
pthread_exit();
}
void * func(void *arguments)
{
FILE *infile = stdin;
infile = fopen(filename, "rb");
fclose (infile);
}
So I'm trying to make it so the threads startup function opens a file that was given via commandline, one file for each thread, but I also need the startup function to get my results array. So basically I need to get a string (the filename) and a 2D array of results to my startup thread some how, I'm thoroughly confused.
Anyone have any tips or ideas? Thanks.
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include "string.h"
void* func(void *args);
int main(int argc, const char * argv[])
{
int nthreads = 0;
int i = 0;
long **results;
printf("Enter number of threads to use:\n> ");
scanf("%d", nthreads);
pthread_t threadArray[nthreads];
// results 2d array; 3 rows by nthreads cols
results = malloc((nthreads*4) * sizeof(long *));
for(i = 0; i<nthreads; i++) {
pthread_create(&threadArray[i], NULL, wordcount, HELP!!!!);
}
for(i = 0; i<nthreads; i++) {
pthread_join(threadArray[i], NULL);
}
pthread_exit();
}
void * func(void *arguments)
{
FILE *infile = stdin;
infile = fopen(filename, "rb");
fclose (infile);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常,会声明并初始化包含线程数据的结构,并将指向该结构的指针作为线程参数传递。
然后,线程函数将
void*
强制转换回结构指针并获取数据。请记住,当线程被调度时,该结构的生命周期仍然需要有效(这意味着如果它是局部变量,您需要非常小心)。正如 Jonathan Leffler 指出的那样,向每个线程传递它自己的结构实例,或者要非常小心地重用它。否则,如果该结构在线程完成之前被重用,则该线程可能会读取供其他线程使用的数据。
管理这些问题的最简单方法可能是为每个线程创建一个 malloc() 结构,初始化它,将指针传递给线程并让线程 free() 它当数据处理完毕后。
Generally a structure that contains the data for the thread is declared and initialized, and a pointer to that structure is passed as the thread argument.
The thread function then casts the
void*
back to the structure pointer and has at the data.Just remember that the lifetime of that structure still needs to be valid when the thread gets scheduled (which means you need to be very careful if it's a local variable). And as Jonathan Leffler pointed out, pass each thread it's own instance of the structure, or be very careful how you reuse it. Otherwise a thread may read data intended for a different thread if the structure gets reused before the thread is finished with it.
Probably the simplest way to manage those issues is to
malloc()
a structure for each thread, initialize it, pass the pointer to the thread and let the threadfree()
it when it's done with the data.pthread_create 的最后一个参数可以是您想要的任何对象,例如你可以有:
The last parameter to pthread_create can be any object you want, so for example you could have: