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

  • SEARCH
  • Home
  • 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 9063527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:01:19+00:00 2026-06-16T16:01:19+00:00

I have a program which creates multiple threads and each one of them tries

  • 0

I have a program which creates multiple threads and each one of them tries to write 100 bytes in a file at a different location(offset).
The first thread writes 100 bytes starting from 0, the second 100 bytes starting from 100, the third 100 bytes starting from 300 and so on
If the threads are executed in this order everithing is ok and I do not need fseek. But for real time concurrency if I put the first thread to “sleep(2)” for 2 seconds, wait until all other threads are done, and use fseek to move the file cursor to the begining of the file this doesn’t happen.
I used mutexes to handle concurrency.
Code sample:

    offset=0;//for the first thread
    char data[100];
    int length; // how many chars are currently in data
    FILE * f;

    pthread_mutex_lock(&mutexFileWrite);
    f = fopen(fileName, "a");
    fseek(f,offset, SEEK_SET);  
    fwrite(data,sizeof(char),length,f);
    fclose(f);
    pthread_mutex_unlock(&mutexFileWrite);
  • 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-06-16T16:01:20+00:00Added an answer on June 16, 2026 at 4:01 pm

    Don’t open the file in append mode if you don’t plan on only appending to it.

    From the POSIX reference for fopen:

    Opening a file with append mode (a as the first character in the mode argument) shall cause all subsequent writes to the file to be forced to the then current end-of-file, regardless of intervening calls to fseek().

    Looks like you’re looking for r+ mode.

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

Sidebar

Related Questions

I have a server which handles multiple incoming socket connections and creates 2 different
I have a C++ program in which I have multiple threads writing into my
I have a program which creates JButtons which are then added to a JPanel
I have created a concurrent, recursive directory traversal and file processing program, which sometimes
I have a windows form that creates multiple console applications of the same program
I currently program a program which uses multiple threads. To be able to share
I can understand how one can write a program that uses multiple processes or
I'm writing a moderately complex iOS program that needs to have multiple threads for
I have a program in which the user adds multiple objects to a scene.
I have been making a program that creates multiple CSV's from another source CSV

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.