有些线程使用相同的例程,怎么办? [英] some thread using the same routine, how will do?

查看:86
本文介绍了有些线程使用相同的例程,怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关注代码:

Follow code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>

void* test_routine(void * arg)
{
    int *iValue = (int *) arg;
    int i = 0;
    FILE *pFile = NULL;
    char szFile[64] = { 0 };
    char szContent[128] = { 0 };

    snprintf(szFile, sizeof(szFile), "./data/thread_%04d.data", *iValue);
    pFile = fopen(szFile, "w");

    printf("   index = %d, file name = %s \n\n", *iValue, szFile);

    for ( i = 0; i < 500000; ++i ) {
        snprintf(szContent, sizeof(szContent), "[%04d] -> %05d: This is a test data created by dai.\n", *iValue, i);
        fwrite(szContent, 1, strlen(szContent), pFile);
    }

    fclose(pFile);

    return arg;
}

int main(int argc, char* argv[])
{
#define THREAD_NUMS		10
	int i = 0;
	tpool_t test_pool;	
	pthread_t thread_array[THREAD_NUMS];

	for ( i = 0; i < THREAD_NUMS; i++ ) {		
		pthread_create(&thread_array[i], NULL, test_routine, (void *) &i);
	}

	for ( i = 0; i < THREAD_NUMS; i++ ) {
		pthread_join(thread_array[i], NULL);
	}
	
	return 0;
}





结果并不像预期的那样,那个人正在使用* iValue,那么其他人可能会改变它。我在 https://computing.llnl.gov/tutorials/pthreads/ [ ^ ]



所有线程都使用相同的例程吗?线程是否可以复制新的数据例程数据?如果没有怎么解决?



The result was not as expect, the one thead is using the *iValue, then other maybe can change it. I read some infromation about thread on https://computing.llnl.gov/tutorials/pthreads/[^]

Is all threads use the same routine? Do the thread can copy a new data routine data? If not how can solve?

推荐答案

这是你的基本问题。当您创建线程(无论操作系统)时,您无法保证线程将立即启动。所以你有一个创建线程的循环并将指针传递给局部变量( i ),然后你继续改变 i 不知道线程是否开始,也不知道它是否获得 i 的价值。



因此,鉴于操作系统如何调度线程执行的模糊性,您可以创建所有10个线程,因此 i 的值在*之前为11 *线程的实际开始,所以他们都有11个。事实上,几乎任何组合都是可能的。



你有经典案例我需要开始要同步但尚未完全同步它们的线程。魔术不会发生线程同步,你需要创建事件/信号量和信号并正确等待它们以实现同步。



另外,你正在复制指针参数( * arg )而不是参数的值所以你带着一个指向 i iValue 中,所以即使它们被正确同步,你仍然会让每个线程指向一个正在改变的变量。



<编辑>



PS,我去查看你所链接的资料。当你读到关于pthreads时,你显然错过了这一部分创建线程和传递参数。 [ ^ ]。您实际上已经实现了该文档的示例3,*错误*传递值的方式。您应该已经实现了示例1.
Here is your basic problem. When you create a thread (regardless of OS) you have no guarantee that the thread will start immediately. So you have a loop that creates threads and passes a pointer to a local variable (i) and then you go on to change i without knowing if the thread started or not nor if it picked up the value of i or not.

So, given the vagueness of how operating systems schedule thread execution, it is possible for you to create all 10 threads, therefore the value of i would be 11 before *any* of the threads actually start so they would all have 11. In fact, just about any combination is possible.

You have the classic case of "I need the start of the threads to be synchronized but have done absolutely nothing to synchronize them". Thread synchronization does not happen by magic, you need to create events / semaphores and signal and wait on them properly to achieve synchronization.

Also, you are copying the "pointer to the argument" (*arg) and not the "value of the argument" so you are carrying around a pointer to i in iValue so even if they were properly synchronized, you''d still have each thread pointing to a variable that is changing.

<edit>

PS, I went and looked at the material you linked in. When you read about pthreads you obviously missed this section on creating threads and Passing Argument.[^]. You have actually implemented example 3 of that documents, the *wrong* way to pass values. You should have implemented example 1.


与在不同线程中执行不同函数完全相同。一个函数完全不知道它将被调用的线程。



-SA
Exact same way as executing different functions in different thread. A function is totally agnostic about the thread it will be called in.

—SA


这篇关于有些线程使用相同的例程,怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆