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 7542029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:03:18+00:00 2026-05-30T08:03:18+00:00

Greeting !! I’ve a c ap running in linux which is a POSIX multithreading

  • 0

Greeting !!

I’ve a c ap running in linux which is a POSIX multithreading application ,
In thread 1 , I set global var in this way :

Pthread_mutex_lock(&Mutex1); 
for(idx=0;idx<1000;idx++)
{
      Data1[idx].doubleval = idx * 100.0 * anothervar  ;
}//for 
Pthread_mutex_unlock(&Mutex1); 

in thread2 , I get global var it in this way :

Pthread_mutex_lock(&Mutex1); 
for(idx=0;idx<1000;idx++)
{
     doublexxx = Data1[idx].doubleval ;
     DoSomething(doublexxx) ;
}//for 
Pthread_mutex_unlock(&Mutex1); 

Now , suppose I don’t want to use Mutex in my ap , if thread1 is setting Data1[33].doubleval while thread2 is reading Data1[33].doubleval , will Data1[33].doubleval be a very strange value ? I mean , for example , double var has 4 bytes , while thread1 is setting Data1[33].doubleval first 2 bytes , not finish later 2 bytes , at this monent
thread2 is trying to read Data1[33].doubleval ….is it possible ?

If it is possible , how can I do to protect a double var without this kind of problem ?
Mutex is great to use , I just wonder if there is another way to protect a double var
which thread1 is writing in it , at least thread2 try to read from the same var
will wait that 4 bytes double var finished !!!

  • 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-30T08:03:19+00:00Added an answer on May 30, 2026 at 8:03 am

    Whether or not it’s possible to read a “partially written” value depends on a large number of factors (e.g. target system and alignment) and is definitely not portable. For an example, imagine this code running on a 16-bit CPU with floating point support emulated in software.

    Even with a mutex, the second thread’s behaviour will still be undefined because it won’t know if it has read data before the first thread wrote it or after the first thread wrote it.

    I suspect that there’s higher level problems with your design – perhaps you should be using a condition variable to make the second thread wait until the first thread has written.

    I also suspect that the reason you’re asking about removing the mutex is to improve scalability/performance. For example, so that one thread to use one part of the array while a different thread modifies a completely different part of the same array. If this is the reason behind your question, then the solution depends on your scenario – it could be as simple as using multiple locks (one mutex per area of the array, up to one mutex per entry), but might mean implementing a “reader/writer” lock (so that multiple threads are allowed to read from the array at the same time, as long as no other thread is writing).

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

Sidebar

Related Questions

I'm trying something like this Output.py print Hello Input.py greeting = raw_input(Give me the
Greeting, This month I will start working on my master thesis. My thesis's subject
Greeting to the community. This is my very first question. I had in mind
How to display user greeting (the once which is defined in vm file) in
I want to make an iphone application similar to some greeting cards application, where
Can the following be done in C#?: var greeting = Hello + function ()
i saw this code somewhere switch(greet){ case HELLO: System.out.println(Formal Greeting); break; case HI: System.out.println(Friendly
Groovy has a concept of GStrings. I can write code like this: def greeting
If I have an XML document like this: <root> <elem name=Greeting> Hello </elem> <elem
Greeting, can i use this library in Windows Phone 7? There exists C# or

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.