a esql/c with threads question , just curiosity
Posted in 2012
Topics: Connectivity: ESQL/C, 4GL & Embedded SQL, Data Types & Schema Design, Platform-Specific Issues
Greeting !! This question is not about IDS , I am confused with thread's behavior , I use mutex a lot in thread , just wonder if I take mutex away , will the global vars become a very strange value ? Now I am coding esql/c application in redhat linux with IDS 11.5x , using a lot of threads , THREADLIB=POSIX and use a lot of mutex to protect global var like : double global1 ; //global1 is a global var !!...data type double Thread1: pthread_mutex_lock(&Mutex1); global1=xxx ; pthread_mutex_unlock(&Mutex1); Thread2: pthread_mutex_lock(&Mutex1); yyy=global1 ; pthread_mutex_unlock(&Mutex1); So far so good , my esql/c ap always one thread read global1, another thread write global , both in endless loop ,mutex avoid the write and read thread at the same time !! Now suppose I didn't use mutex_lock for some reason !! thread2 is reading global1 , at this very monent thread1 is writing global1 ,if at this monent , thread1 will change global1 from 1000000.11 to 2000000.22 , thread2 read global1 is 1000000.11 or 2000000.22 will be fine to me , if not these 2 value , in stead a strange value will be awful !! Since hardware has many cores , is it possible the thread1 writing first 2 bytes in cpu core1 and not yet finish last 2 bytes and then thread2 read it out(4 bytes) in cpu core2 ,that would be a disaster !! Any suggestion are welcome , sorry for my english !!!
As long as 'global' is a word sized object or smaller writing to it is an atomic operation and you should not be able to see a partially written word. You can probably safe read it without a mutex. Writers should alwayd acquire the mutex and for objects larger than a word (say an entire struct) both writers and readers should use a mutex. Art Art S. Kagel Advanced DataTools (www.advancedatatools.com) Blog: http://informix-myview.blogspot.com/ Disclaimer: Please keep in mind that my own opinions are my own opinions and do not reflect on my employer, Advanced DataTools, the IIUG, nor any other organization with which I am associated either explicitly, implicitly, or by inference. Neither do those opinions reflect those of other individuals affiliated with any entity with which I am affiliated nor those of the entities themselves. On Wed, Feb 22, 2012 at 4:48 AM, MARS CHEN <hedgezzz@yahoo.com.tw> wrote: > Greeting !! > > This question is not about IDS , I am confused with thread's behavior , > I use mutex a lot in thread , just wonder if I take mutex away , > will the global vars become a very strange value ? > > Now I am coding esql/c application in redhat linux with IDS 11.5x , > using a lot of threads , THREADLIB=POSIX and use a lot of mutex > to protect global var like : > > double global1 ; //global1 is a global var !!...data type double > > Thread1: > pthread_mutex_lock(&Mutex1); > global1=xxx ; > pthread_mutex_unlock(&Mutex1); > > Thread2: > pthread_mutex_lock(&Mutex1); > yyy=global1 ; > pthread_mutex_unlock(&Mutex1); > > So far so good , my esql/c ap always one thread read global1, > another thread write global , both in endless loop ,mutex avoid > the write and read thread at the same time !! > > Now suppose I didn't use mutex_lock for some reason !! > thread2 is reading global1 , at this very monent thread1 is writing global1 > ,if at this monent , thread1 will change global1 from > 1000000.11 to 2000000.22 , > thread2 read global1 is 1000000.11 or 2000000.22 will be fine to me , > if not these 2 value , in stead a strange value will be awful !! > Since hardware has many cores , is it possible the thread1 writing first > 2 bytes in cpu core1 and not yet finish last 2 bytes > and then thread2 read it out(4 bytes) in cpu core2 ,that would be a > disaster > !! > > Any suggestion are welcome , sorry for my english !!! > > > > ******************************************************************************* > Forum Note: Use "Reply" to post a response in the discussion forum. > > --20cf3026697aa95f4404b98b72af
probably. From: "MARS CHEN" <hedgezzz@yahoo.com.tw> To: ids@iiug.org Date: 02/22/2012 03:49 AM Subject: a esql/c with threads question , just curiosity [26300] Sent by: ids-bounces@iiug.org Greeting !! This question is not about IDS , I am confused with thread's behavior , I use mutex a lot in thread , just wonder if I take mutex away , will the global vars become a very strange value ? Now I am coding esql/c application in redhat linux with IDS 11.5x , using a lot of threads , THREADLIB=POSIX and use a lot of mutex to protect global var like : double global1 ; //global1 is a global var !!...data type double Thread1: pthread_mutex_lock(&Mutex1); global1=xxx ; pthread_mutex_unlock(&Mutex1); Thread2: pthread_mutex_lock(&Mutex1); yyy=global1 ; pthread_mutex_unlock(&Mutex1); So far so good , my esql/c ap always one thread read global1, another thread write global , both in endless loop ,mutex avoid the write and read thread at the same time !! Now suppose I didn't use mutex_lock for some reason !! thread2 is reading global1 , at this very monent thread1 is writing global1 ,if at this monent , thread1 will change global1 from 1000000.11 to 2000000.22 , thread2 read global1 is 1000000.11 or 2000000.22 will be fine to me , if not these 2 value , in stead a strange value will be awful !! Since hardware has many cores , is it possible the thread1 writing first 2 bytes in cpu core1 and not yet finish last 2 bytes and then thread2 read it out(4 bytes) in cpu core2 ,that would be a disaster !! Any suggestion are welcome , sorry for my english !!! ******************************************************************************* Forum Note: Use "Reply" to post a response in the discussion forum.
On Wed, Feb 22, 2012 at 01:48, MARS CHEN <hedgezzz@yahoo.com.tw> wrote: > This question is not about IDS , I am confused with thread's behavior , > I use mutex a lot in thread, just wonder if I take mutex away , > will the global vars become a very strange value ? > > Now I am coding esql/c application in redhat linux with IDS 11.5x , > using a lot of threads , THREADLIB=POSIX and use a lot of mutex > to protect global var like : > > double global1 ; //global1 is a global var !!...data type double > > Thread1: > pthread_mutex_lock(&Mutex1); > global1=xxx ; > pthread_mutex_unlock(&Mutex1); > > Thread2: > pthread_mutex_lock(&Mutex1); > yyy=global1 ; > pthread_mutex_unlock(&Mutex1); > > So far so good , my esql/c ap always one thread read global1, > another thread write global , both in endless loop ,mutex avoid > the write and read thread at the same time !! > > Now suppose I didn't use mutex_lock for some reason !! > thread2 is reading global1 , at this very monent thread1 is writing global1 > ,if at this monent , thread1 will change global1 from > 1000000.11 to 2000000.22 , > thread2 read global1 is 1000000.11 or 2000000.22 will be fine to me , > if not these 2 value , instead a strange value will be awful !! > Since hardware has many cores , is it possible the thread1 writing first > 2 bytes in cpu core1 and not yet finish last 2 bytes > and then thread2 read it out(4 bytes) in cpu core2 ,that would be a > disaster > !! > > Any suggestion are welcome > In principle, you could end up with a partial value, though on a variable that's an 8-byte double and on a 64-bit CPU, that's pretty unlikely since reading and writing a double is a single memory operation. If you were dealing with a structure or a character array or something like that (or perhaps if you were using a 32-bit CPU), then the partial write issue would be a more serious problem. So, you're right to be concerned. Everything you learned about global variables being bad is true - especially in threaded programs. -- Jonathan Leffler <jonathan.leffler@gmail.com> #include <disclaimer.h> Guardian of DBD::Informix - v2011.0612 - http://dbi.perl.org "Blessed are we who can laugh at ourselves, for we shall never cease to be amused." --e0cb4efe33e29ecf6c04b9907b32