From realluca76 at yahoo.it Thu Mar 8 09:16:18 2007 From: realluca76 at yahoo.it (realLuca76 bahbihboh) Date: Thu, 8 Mar 2007 10:16:18 +0100 (CET) Subject: threads sync with heavy CPU load Message-ID: <96191.81416.qm@web23406.mail.ird.yahoo.com> Hi. I have to synchronize two threads (T1 and T2) in this way: T1 does a job and meanwhile T2 waits, then T2 does a job and meanwhile T1 waits, then T1 does a job and meanwhile T1 waits and so on... (loop). I have used two approaches: 1) pthread_mutex_lock + pthread_cond_wait + pthread_cond_signal + pthread_mutex_unlock (in both threads) Here's the code for the example: T1 increments a counter and does some additional CPU heavy work while T2 is blocked. then it (u)sleeps. T2 does the same. Well: if both threads sleep for 1 second after their operations all seems to work. If they (usleep) for 10000 us, for example (or if they don't sleep at all), the synchronization doesn't work properly. The value of pthread_cond_wait is uncorrect. Should i have to add real time extensions to my code in order to solve the problem? Any help is GREATLY appreciated, Luca --------------------------------------------------------------------------------------------------------------- #ifdef HAVE_CONFIG_H #include #endif #include #include pthread_mutex_t my_mutex; pthread_cond_t my_condition; bool condizione=false; int shared_counter = 0; void* routineT1(void* obj) { while(1) { int a = pthread_mutex_lock(&my_mutex); int b = 145; //dummy default while(condizione) b = pthread_cond_wait(&my_condition, &my_mutex); if(shared_counter < 10) shared_counter ++; else shared_counter = 1; std::cerr<< "\n T1: I do my job ( I increment the counter: "< Hi, I did not find a definitive source on the net (due to lots of false positives when googling), but I found this list that seems to discuss C++ / pthread library issues. My question is simple: Does the combination of C++98 and Posix pthread standards give any guarantee that the operator new is "thread safe", means that several threads can allocate objects via "new" concurrently without explicit locking by the user code? Our current research seems to indicate that pthread standards don't mention C++ in any way, and C++ does not mention threads, and it is pure benevolence (or plain sanity?) of the implementations I have seen up to now to provide a thread safe implementation of new (mostly based on a global lock). Some pointers to mandatory sources or better places to discuss this issue are welcome. Thanks a lot, Markus -- Markus Schaber | Logical Tracking&Tracing International AG Dipl. Inf. | Software Development GIS Fight against software patents in Europe! www.ffii.org www.nosoftwarepatents.org From hinnant at twcny.rr.com Fri Mar 16 20:55:52 2007 From: hinnant at twcny.rr.com (Howard Hinnant) Date: Fri, 16 Mar 2007 16:55:52 -0400 Subject: [c++-pthreads] pthreads and operator new In-Reply-To: <20070316140018.5e86a7ea@kingfisher.sec.intern.logix-tt.com> References: <20070316140018.5e86a7ea@kingfisher.sec.intern.logix-tt.com> Message-ID: <39F6AA06-F932-4577-8C29-5040D37EF4E4@twcny.rr.com> On Mar 16, 2007, at 9:00 AM, Markus Schaber wrote: > My question is simple: Does the combination of C++98 and Posix pthread > standards give any guarantee that the operator new is "thread safe", > means that several threads can allocate objects via "new" concurrently > without explicit locking by the user code? > > Our current research seems to indicate that pthread standards don't > mention C++ in any way, and C++ does not mention threads, and it is > pure benevolence (or plain sanity?) of the implementations I have seen > up to now to provide a thread safe implementation of new (mostly based > on a global lock). Your research is correct. Though you might also add "market value" to your list of reasons. C++ vendors would otherwise go out of business. The C++ committee is attempting to address this shortcoming in an upcoming standard colloquially termed C++0X (we hope the X will become a 9). -Howard From mohsen at pahlevanzadeh.org Sat Mar 24 09:51:53 2007 From: mohsen at pahlevanzadeh.org (Mohsen Pahlevanzadeh) Date: Sat, 24 Mar 2007 05:51:53 -0400 Subject: an error on Debian Message-ID: <4604F4B9.6020406@pahlevanzadeh.org> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Dear all, I wrote following code : #include #include #include int g1=0; int g2=0; void *task1(int *counter); //void task2(int *counter); void cleanup(int counter1,int counter2); int main(int argc,char *argv[]) { pthread_t thr1,thr2; if ( pthread_create(&thr1,NULL,task1,(void*)g1)) { perror("pthread_create : task1"); exit(EXIT_FAILURE); } /* if ((ret = pthread_create(&thr2,NULL,task2,(void *)&g2))) { perror("pthread_create : task2"); exit(EXIT_FAILURE); }*/ // pthread_join(thr2,NULL); pthread_join(thr1,NULL); cleanup(g1,g2); exit(EXIT_SUCCESS); }//end of main program void *task1(int *counter) { while(*counter < 5 ){ printf("task1 count: %d\n",*counter); (*counter)++; }//end of while void cleanup(int counter1,int counter2) { printf("Total iterations: %d\n",counter1+counter2); }//end of cleanup function But i receive following error: mohsen at debian:~/test/learning/pthread$ make gcc -c -Wall test.c -lpthread In file included from test.c:3: /usr/include/pthread.h:285: error: conflicting types for ?pthread_t? /usr/include/bits/pthreadtypes.h:152: error: previous declaration of ?pthread_t? was here /usr/include/pthread.h:286: error: conflicting types for ?pthread_attr_t? /usr/include/bits/pthreadtypes.h:54: error: previous declaration of ?pthread_attr_t? was here /usr/include/pthread.h:287: error: conflicting types for ?pthread_key_t? /usr/include/bits/pthreadtypes.h:82: error: previous declaration of ?pthread_key_t? was here /usr/include/pthread.h:289: error: conflicting types for ?pthread_mutexattr_t? /usr/include/bits/pthreadtypes.h:102: error: previous declaration of ?pthread_mutexattr_t? was here /usr/include/pthread.h:290: error: conflicting types for ?pthread_mutex_t? /usr/include/bits/pthreadtypes.h:95: error: previous declaration of ?pthread_mutex_t? was here /usr/include/pthread.h:291: error: conflicting types for ?pthread_condattr_t? /usr/include/bits/pthreadtypes.h:79: error: previous declaration of ?pthread_condattr_t? was here /usr/include/pthread.h:292: error: conflicting types for ?pthread_cond_t? /usr/include/bits/pthreadtypes.h:72: error: previous declaration of ?pthread_cond_t? was here test.c: In function ?main?: test.c:13: warning: passing argument 3 of ?pthread_create? from incompatible pointer type test.c:11: warning: unused variable ?thr2? test.c: In function ?task1?: test.c:38: warning: control reaches end of non-void function make: *** [main.o] Error 1 mohsen at debian:~/test/learning/pthread$ My distro is Debian Etch. kernel is 2.6.18 Please help me.... Yours,Mohsen -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFGBPS5/ZBAvBh9bHIRAmTtAJ4pcg3fHT5Iq3ootfiAWaGFwb94iQCfdKM4 o+gfENU0CfgL1Xk/NX8Lufw= =fLVY -----END PGP SIGNATURE----- From mohsen at pahlevanzadeh.org Sat Mar 24 09:49:10 2007 From: mohsen at pahlevanzadeh.org (Mohsen Pahlevanzadeh) Date: Sat, 24 Mar 2007 05:49:10 -0400 Subject: an error on pthread Message-ID: <4604F416.1050306@pahlevanzadeh.org> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Dear all, I wrote following code : #include #include #include int g1=0; int g2=0; void *task1(int *counter); //void task2(int *counter); void cleanup(int counter1,int counter2); int main(int argc,char *argv[]) { pthread_t thr1,thr2; if ( pthread_create(&thr1,NULL,task1,(void*)g1)) { perror("pthread_create : task1"); exit(EXIT_FAILURE); } /* if ((ret = pthread_create(&thr2,NULL,task2,(void *)&g2))) { perror("pthread_create : task2"); exit(EXIT_FAILURE); }*/ // pthread_join(thr2,NULL); pthread_join(thr1,NULL); cleanup(g1,g2); exit(EXIT_SUCCESS); }//end of main program void *task1(int *counter) { while(*counter < 5 ){ printf("task1 count: %d\n",*counter); (*counter)++; }//end of while void cleanup(int counter1,int counter2) { printf("Total iterations: %d\n",counter1+counter2); }//end of cleanup function But i receive following error: mohsen at debian:~/test/learning/pthread$ make gcc -c -Wall test.c -lpthread In file included from test.c:3: /usr/include/pthread.h:285: error: conflicting types for ?pthread_t? /usr/include/bits/pthreadtypes.h:152: error: previous declaration of ?pthread_t? was here /usr/include/pthread.h:286: error: conflicting types for ?pthread_attr_t? /usr/include/bits/pthreadtypes.h:54: error: previous declaration of ?pthread_attr_t? was here /usr/include/pthread.h:287: error: conflicting types for ?pthread_key_t? /usr/include/bits/pthreadtypes.h:82: error: previous declaration of ?pthread_key_t? was here /usr/include/pthread.h:289: error: conflicting types for ?pthread_mutexattr_t? /usr/include/bits/pthreadtypes.h:102: error: previous declaration of ?pthread_mutexattr_t? was here /usr/include/pthread.h:290: error: conflicting types for ?pthread_mutex_t? /usr/include/bits/pthreadtypes.h:95: error: previous declaration of ?pthread_mutex_t? was here /usr/include/pthread.h:291: error: conflicting types for ?pthread_condattr_t? /usr/include/bits/pthreadtypes.h:79: error: previous declaration of ?pthread_condattr_t? was here /usr/include/pthread.h:292: error: conflicting types for ?pthread_cond_t? /usr/include/bits/pthreadtypes.h:72: error: previous declaration of ?pthread_cond_t? was here test.c: In function ?main?: test.c:13: warning: passing argument 3 of ?pthread_create? from incompatible pointer type test.c:11: warning: unused variable ?thr2? test.c: In function ?task1?: test.c:38: warning: control reaches end of non-void function make: *** [main.o] Error 1 mohsen at debian:~/test/learning/pthread$ My distro is Debian Etch. kernel is 2.6.18 Please help me.... Yours,Mohsen -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFGBPQW/ZBAvBh9bHIRArCkAJ9f3DNW9BtejrB5DRJZijkoeCiGLQCfTQ9n SrK6gggZbAHQ/YOtV2TeBcA= =6EVs -----END PGP SIGNATURE----- From ben at decadent.org.uk Sat Mar 24 18:38:57 2007 From: ben at decadent.org.uk (Ben Hutchings) Date: Sat, 24 Mar 2007 18:38:57 +0000 Subject: [c++-pthreads] an error on Debian In-Reply-To: <4604F4B9.6020406@pahlevanzadeh.org> References: <4604F4B9.6020406@pahlevanzadeh.org> Message-ID: <1174761537.11281.10.camel@deadeye.i.decadent.org.uk> Answered off-list. Ben. -- Ben Hutchings Horngren's Observation: Among economists, the real world is often a special case. -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 189 bytes Desc: This is a digitally signed message part URL: From mohsen at pahlevanzadeh.org Sun Mar 25 07:50:31 2007 From: mohsen at pahlevanzadeh.org (Mohsen Pahlevanzadeh) Date: Sun, 25 Mar 2007 03:50:31 -0400 Subject: [c++-pthreads] an error on Debian In-Reply-To: <1174761537.11281.10.camel@deadeye.i.decadent.org.uk> References: <4604F4B9.6020406@pahlevanzadeh.org> <1174761537.11281.10.camel@deadeye.i.decadent.org.uk> Message-ID: <460629C7.5080708@pahlevanzadeh.org> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Ben Hutchings wrote: > Answered off-list. > > Ben. > I can't understand your purpose. Mohsen -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFGBinH/ZBAvBh9bHIRAicJAJ4lY38gaEt1N/en7Ao5t257esJpiACeKUDN YsfmPLRgcq1RL2oi7ZS9prU= =Tq0S -----END PGP SIGNATURE-----