c - 如何在执行某些操作之前确保其他线程已完成

标签 c multithreading synchronization pthreads inter-process-communicat

我的程序由许多将数据写入缓冲区的写入线程和一个从该共享缓冲区读取数据并将其输出的读取线程组成。我需要确保所有写入线程都已将其数据写入缓冲区,然后读取线程尝试从中读取数据。我的直觉告诉我我会使用信号量来实现这一点,但它们对我来说确实没有多大意义。这是我的两个功能:

void *search_thread_func(void *threadArgs)
{
printf("\n");
//pthread_mutex_lock(&mutex);
int count = 0;
int matches = 0;
int matches_this_line = 0;
int lines = 0;
int inputLines = 0;
int tid;
int *wbPtr;
//char *buffer;
char *sharedBuffer;
char *writeBuffer;
char* string;
string = ((struct st_args*)threadArgs)->string;
tid = ((struct st_args*)threadArgs)->threadId;
wbPtr = ((struct st_args*)threadArgs)->wBufPtr;
//buffer = ((struct st_args*)threadArgs)->threadBuffer;
sharedBuffer = ((struct st_args*)threadArgs)->sharedBuffer;
writeBuffer = ((struct st_args*)threadArgs)->writeBuffer;
//printf("Thread %d\n",(int)tid);
printf("Searching for %s\n", string);

//printf("%d\n", (int)sharedBuffer);
while(sharedBuffer[count] != NULL)
{
    //printf("%c", sharedBuffer[count]);
    count++;
    if(sharedBuffer[count] == '\n')
    {
        inputLines++;
    }
}

printf("\n");
//pthread_mutex_unlock(&mutex);


char *token;
char *temp = malloc(count*sizeof(char));
memcpy(temp, sharedBuffer, count);
token = strtok(temp, "\n");

while(token != NULL)
{
    printf("%s\n", token);

    char *q = strstr(token, string);
    if(q != NULL)
    {
        matches_this_line++;
        lines++;

        for(char *r = q; r != NULL; r= strstr(r+strlen(string), string))
        {
            matches++;
        }
        pthread_mutex_lock(&mutex);
        for(int j = 0; j < strlen(token); j++)
        {
            writeBuffer[*wbPtr] = token[j];
            *wbPtr = *wbPtr + 1;    
        }
        writeBuffer[*wbPtr] = '\n';
        *wbPtr = *wbPtr + 1;
        pthread_mutex_unlock(&mutex);

    }

    token = strtok(NULL, "\n");
}

printf("lines %d, matches %d\n", lines, matches);

printBuffer(writeBuffer, *wbPtr);

free(temp);
printf("\n");
}

void *write_thread_func(void *threadArgs)
{
printf("write func\n");
char *writeBuffer;
int * wbPtr;
FILE* wFP;
writeBuffer = ((struct wt_args*)threadArgs)->writeBuffer;
wFP = ((struct wt_args*)threadArgs)->wFP;
wbPtr = ((struct st_args*)threadArgs)->wBufPtr;
printf("wbPtr = %d\n", wbPtr);
printf("*wbPtr = %d\n", *wbPtr);
//printf("wb loc = %d\n", writeBuffer);

}

基本上,搜索线程搜索一些数据并将其他数据写入缓冲区。我还没有实现 write_thread_func 从缓冲区读取,但这非常简单。我了解互斥锁的工作原理,但我认为这在这里不起作用,因为我需要确保 write_thread_func 最后执行,或者等到 search_thread_funcs 完成写入缓冲区。

最佳答案

如果数据的使用者也是处理数据的线程的创建者,它可以简单地 pthread_join 它们全部并确保它们完成。否则,执行此操作的规范方法是在数据结构中使用互斥体和条件变量,以及基于数据结构内容的“数据已完成”谓词。使用者将等待条件变量,并且在更新谓词所依赖的数据并同时持有互斥体后,生成数据的线程将发出信号。

有多种方法可以使用信号量实现相同的目的(例如,让每个生产者发布信号量一次,而消费者等待 N 次,其中 N 是信号量的数量生产者),但这种类型的设计要求您的消费者了解更多细节(例如有多少生产者),而这些细节可能不是其需要了解的。

关于c - 如何在执行某些操作之前确保其他线程已完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22238390/

相关文章:

c - "printf"只打印变量地址

java - 如何知道Java中InterruptedException的原因

java - 如何根据对象的实例调用它来序列化函数,如果在线程中调用相同的实例则进行序列化,否则不进行序列化

c# - 更改线程优先级以使我的程序和计算机响应更快

multithreading - wxpython线程处理任务并更新GUI原因

C#程序Windows与linux时间同步

java - 多线程JAVA中的静态方法行为

c - 添加一个 int 到 char* 的中间(在 C 中)

c - 我看不懂这段代码?这是正常现象还是我还没准备好?

c++ - 交换文件内容的有效方法