Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4607880
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:45:52+00:00 2026-05-22T00:45:52+00:00

I used threads in this code. but when I execute this code in shell,

  • 0

I used threads in this code. but when I execute this code in shell, some threads didn’t print this line.

printf("\ti'm %dth thread:)", j);  
printf("\t%c  %d  %d  %d \n", arr[j].op, arr[j].byte, seq, no_file);

In addition, even some threads print this line twice. what happened to this process? And how to reorganize this code in order to threads print the line exactly once?

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <errno.h>

typedef struct {char op; int byte; int seq; int no_file; } ARRAY; 

ARRAY *arr;

void *thread_operation(void *arg){
    int j =*((int*)arg);
    seq = arr[j].seq;
    int no_file = arr[j].no_file;
    printf("\ti'm %dth thread:)", j);
    printf("\t%c  %d  %d  %d \n", arr[j].op, arr[j].byte, seq, no_file);
}

int main()
{
    int err, j, i = 10;
    long int tid;
    arr = (ARRAY*)malloc(sizeof(ARRAY) * i);
    srand(time(NULL));
    for (i = 0; i <= 10; i++){
        arr[i].op = 'r';
        arr[i].byte = (rand() % 10);
        arr[i].seq = i;
        arr[i].no_file = i + 10;
}

    for(j = 0; j < 10; j++){
        printf("creating %dth thread.....", j);
        err = pthread_create(&tid, NULL, thread_operation, &j);
        if(err != 0)
        printf("%s\n", strerror(err));
        printf("%dth done\n", j);
    }

    return 0;
}

This is result from my pc

creating 0th thread.....0th done
creating 1th thread.....    i'm 0th thread:)    r  9  0  10
1th done
creating 2th thread.....    i'm 2th thread:)    r  3  2  12
    i'm 2th thread:)    r  3  2  12
2th done
creating 3th thread.....    i'm 3th thread:)    r  6  3  13
3th done
creating 4th thread.....    i'm 4th thread:)    r  9  4  14
4th done
creating 5th thread.....    i'm 5th thread:)    r  3  5  15
5th done
creating 6th thread.....    i'm 6th thread:)    r  2  6  16
6th done
creating 7th thread.....    i'm 7th thread:)    r  2  7  17
7th done
creating 8th thread.....8th done
creating 9th thread.....    i'm 8th thread:)    r  6  8  18
9th done
    i'm 9th thread:)    r  8  9  19  
    i'm 9th thread:)    r  8  9  19
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T00:45:53+00:00Added an answer on May 22, 2026 at 12:45 am

    You need to use mutexes ton avoid race conditions, and you also need to use pthread_join to synchronize all your threads before the process dies.

    Mutexes allow you to stop some threads while one of them is executing an action using a shared ressource (see man pthread_mutex_lock, pthread_mutex_init)

    pthread_join is essential in your code, because it forces your main thread to wait for the other threads you created, if you don’t use it, you’re not sure all threads will be finished when the main thread returns from the main.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In this thread some one commented that the following code should only be used
How to use Progressbar without using thread and handler? i used following code but
I used multiple threads in a few programs, but still don't feel very comfortable
I used this tutorial http://delphi.about.com/od/kbthread/a/thread-gui.htm to create a class that asynchronously downloads a file
Ok, I've used locks quite a bit, but I've never had this scenario before.
I am using this code to create threads. while ((strLine = br.readLine()) != null)
I have written code for server and client sockets. I have used threads using
In Python specifically, how do variables get shared between threads? Although I have used
I'm just wondering if the same thread is used for each session, or if
For what I can read, it is used to dispatch a new thread in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.