C Pthreads问题,无法传递我想要的信息?

发布于 2024-11-01 00:26:14 字数 967 浏览 5 评论 0原文

所以我试图让线程启动函数打开一个通过命令行给出的文件,每个线程一个文件,但我还需要启动函数来获取结果数组。所以基本上我需要以某种方式获取一个字符串(文件名)和一个二维结果数组到我的启动线程,我完全困惑了。

有人有任何建议或想法吗?谢谢。

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拔了角的鹿 2024-11-08 00:26:14

通常,会声明并初始化包含线程数据的结构,并将指向该结构的指针作为线程参数传递。

然后,线程函数将 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 thread free() it when it's done with the data.

不醒的梦 2024-11-08 00:26:14

pthread_create 的最后一个参数可以是您想要的任何对象,例如你可以有:

struct ThreadArguments {
    const char* filename;
    // additional parameters
};

void* ThreadFunction(void* arg) {
    CHECK_NOTNULL(arg);
    ThreadArguments* thread_arg = (ThreadArguments*) arg;
    // now you can access the other parameters through this thread_arg object
    // ...
}

// ...
ThreadArguments* arg = (ThreadArguments*) malloc(sizeof(ThreadArguments));
ret = pthread_create(&thread_id, attributes, &ThreadFunction, arg);
// make sure to check ret
// ...
pthread_join(thread_id);
free(arg);

The last parameter to pthread_create can be any object you want, so for example you could have:

struct ThreadArguments {
    const char* filename;
    // additional parameters
};

void* ThreadFunction(void* arg) {
    CHECK_NOTNULL(arg);
    ThreadArguments* thread_arg = (ThreadArguments*) arg;
    // now you can access the other parameters through this thread_arg object
    // ...
}

// ...
ThreadArguments* arg = (ThreadArguments*) malloc(sizeof(ThreadArguments));
ret = pthread_create(&thread_id, attributes, &ThreadFunction, arg);
// make sure to check ret
// ...
pthread_join(thread_id);
free(arg);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文