Click here to Skip to main content
12,504,695 members (62,597 online)
Rate this:
Please Sign up or sign in to vote.
See more: C Linux
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/", *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);
    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[^]

Is all threads use the same routine? Do the thread can copy a new data routine data? If not how can solve?
Posted 12-Dec-12 16:18pm
Updated 12-Dec-12 21:13pm
Jochen Arndt142.6K
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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


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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.160919.1 | Last Updated 13 Dec 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100