Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Follow code:
C++
#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;
}


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?
Posted
Updated 12-Dec-12 21:13pm
v2

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.
 
Share this answer
 
v2
Exact same way as executing different functions in different thread. A function is totally agnostic about the thread it will be called in.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900