Jump to content
  • Advertisement

Nokturnal

Member
  • Content Count

    194
  • Joined

  • Last visited

Community Reputation

190 Neutral

About Nokturnal

  • Rank
    Member
  1. Nokturnal

    pthreads queries

    Quote:Original post by Nokturnal Quote:2)Now if multiple threads are awaiting on the same condition variable, which one will be awakened first and given the lock to the mutex ? See above, again :) The POSIX documentation is also helpful. Note to self : Do not read man pages after 1 am. Thanks zipster..! Moving on : Thread specific data. If i understand the concept correctly ,it means having a (local) copy of a globally declared data structure..Unless i am missing something... How different is it from directly defining the data in the thread itself? Should't that make it a "thread-specific" data ? Can some one list a practical example where this can come into play Here is the example : /* * tsd_once.c * * Demonstrate use of pthread_once to initialize something * exactly once within a multithreaded program. * * Note that it is often easier to use a statically initialized * mutex to accomplish the same result. */ #include <pthread.h> #include "errors.h" /* * Structure used as the value for thread-specific data key. */ typedef struct tsd_tag { pthread_t thread_id; char *string; } tsd_t; pthread_key_t tsd_key; /* Thread-specific data key */ pthread_once_t key_once = PTHREAD_ONCE_INIT; /* * One-time initialization routine used with the pthread_once * control block. */ void once_routine (void) { int status; printf ("initializing key\n"); status = pthread_key_create (&tsd_key, NULL); if (status != 0) err_abort (status, "Create key"); } /* * Thread start routine that uses pthread_once to dynamically * create a thread-specific data key. */ void *thread_routine (void *arg) { tsd_t *value; int status; status = pthread_once (&key_once, once_routine); if (status != 0) err_abort (status, "Once init"); value = (tsd_t*)malloc (sizeof (tsd_t)); if (value == NULL) errno_abort ("Allocate key value"); status = pthread_setspecific (tsd_key, value); if (status != 0) err_abort (status, "Set tsd"); printf ("%s set tsd value %p\n", arg, value); value->thread_id = pthread_self (); value->string = (char*)arg; value = (tsd_t*)pthread_getspecific (tsd_key); printf ("%s starting...\n", value->string); sleep (2); value = (tsd_t*)pthread_getspecific (tsd_key); printf ("%s done...%p\n", (char*)value->string,value); return NULL; } void main (int argc, char *argv[]) { pthread_t thread1, thread2; int status; status = pthread_create (&thread1, NULL, thread_routine, "thread 1"); if (status != 0) err_abort (status, "Create thread 1"); status = pthread_create (&thread2, NULL, thread_routine, "thread 2"); if (status != 0) err_abort (status, "Create thread 2"); pthread_exit (NULL); } The output from the above source : thread 1 set tsd value 0x8c2d4d8 thread 1 starting... thread 2 set tsd value 0x8c2d4e8 thread 2 starting... thread 1 done...0x8c2d4d8 thread 2 done...0x8c2d4e8 And the small change i made void *thread_routine (void *arg) { tsd_t *value; int status; status = pthread_once (&key_once, once_routine); if (status != 0) err_abort (status, "Once init"); value = (tsd_t*)malloc (sizeof (tsd_t)); printf ("%s set tsd value %p\n", (char*)arg, value); value->thread_id = pthread_self (); value->string = (char*)arg; printf ("%s starting...\n",(char*) value->string); sleep (2); //printf ("%s done...\n", (char*)value->string); printf ("%s done...%p\n", (char*)value->string,value); return NULL; } O/P: thread 1 set tsd value 0x8c2d4d8 thread 1 starting... thread 2 set tsd value 0x8c2d4e8 thread 2 starting... thread 1 done...0x8c2d4d8 thread 2 done...0x8c2d4e8 Thanks! [Edited by - Nokturnal on May 23, 2010 10:08:11 AM]
  2. Hi, I am playing around with pthreads. I am following Sir.Butenhof's book for the same. I have some confusion about the things mentioned there. Condition variables : So there can be one thread blocking on a condition.. (Thread A) When the condition is signaled by another thread(Thread B)..which by law has to unlock the mutex... is it possible this mutex is intercepted by another thread ?(Thread C , before Thread A locks it ) (perhaps higher priority) I guess this is what he means by Intercepted wakeups ? 1)Since simulating this is hard; will the thread unblock when this intercepted call ends ? 2)Now if multiple threads are awaiting on the same condition variable, which one will be awakened first and given the lock to the mutex ? Spurious wakeups: This means that when you wait on a condition variable, the wait may (occasionally) return when no thread specifically broadcast or signaled that condition variable. 3)Does this mean , pthread_cond_wait returns even though no signal was sent? Or its this a reference to timed wait variant of the same function ? :| 4) Another section mentions; Pthreads does not allow thread 1, for example, to wait on condition variable A specifying mutex A while thread 2 waits on condition variable A specifying mutex B. It is, however, perfectly reason-able for thread 1 to wait on condition variable A specifying mutex A while thread 2 waits on condition variable B specifying mutex A. That is, each condition variable must be associated, at any given time, with only one mutex—but a mutex may have any number of condition variables associated with it. Come to think of it , it would rather make more sense to allow the same... so that two threads can proceed in parallel (assuming exclusivity of data) This : http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html#SYNCHRONIZATION However says "Any mutex can be used, there is no explicit link between the mutex and the condition variable." 5) Any gains/Advantages of returning from a thread with pthread_exit as opposed to return? I could join and get the return values in either cases. Thanks!
  3. Nokturnal

    Code bloat : Templates

    Interesting, thanks! That should bring down the bloat for multiple types. As for multiple objects of the same type can be brought down by the following model by Bruce Eckel. http://www.linuxtopia.org/online_books/programming_books/c++_practical_programming/c++_practical_programming_135.html Cheers!
  4. Nokturnal

    [c++]model view presenteer

    Are you facing problems with the source or the concept I am sorry but neither is clear from the post. Here is what google says : http://www.c-sharpcorner.com/uploadfile/rmcochran/mvc_intro12122005162329pm/mvc_intro.aspx It's C sharp though. Cheers!
  5. Nokturnal

    Code bloat : Templates

    Quote:Original post by phresnel Btw, C++ templates are instantiated lazily, so unless expliticly asked for, only what you use is put into the final binary. In that respect, you can sometimes even reap smaller executables than you get by manually typing overloads. Do you mean to say if I add another function to my template class say printChar() , but there is no reference to the same in my code , the compiler will not generate code for it ? Thanks!
  6. Nokturnal

    Code bloat : Templates

    Curiosity :) I did run across this.. http://www.linuxtopia.org/online_books/programming_books/c++_practical_programming/c++_practical_programming_122.html I found the 2nd example complicated to follow :(
  7. I am trying to understand what leads to code bloat when using templates. For ex if I have a class as follows: #include<iostream> using namespace std; template< class T> class CBloat { T* pvar; public: CBloat(T *x):pvar(x){} void print() { cout<<*pvar<<endl; } }; int main() { int x =10; float y=20.0; CBloat<int> bInt(&x); CBloat<float> bFlt(&y); bInt.print(); bFlt.print(); return 0; } I understand the compiler will generate/instantiate two versions of the class CBloat. If the same needs to be prevented , what techniques are available? EDIT: I am unable to wrap my head around the 'gets compiled multiple times' issue. I came across statements such as 'If vector is included in two different classes' the compiler will compile all functions related to vector twice. i.e once for each class. Ideally should it link directly to the object of vector just once? ( assuming both instantiate the same type)
  8. Nokturnal

    Templates

    I am trying to follow thinking in c++. I have not yet seen a declaration as such: template<class T> class C<T, double> Can some one please help me understand this? Thanks! //: C05:PartialOrder2.cpp // Reveals partial ordering of class templates. #include <iostream> using namespace std; template<class T, class U> class C { public: void f() { cout << "Primary Template\n"; } }; template<class U> class C<int, U> { public: void f() { cout << "T == int\n"; } }; template<class T> class C<T, double> { public: void f() { cout << "U == double\n”; } }; template<class T, class U> class C<T*, U> { public: void f() { cout << "T* used\n”; } }; template<class T, class U> class C<T, U*> { public: void f() { cout << "U* used\n”; } }; template<class T, class U> class C<T*, U*> { public: void f() { cout << "T* and U* used\n”; } }; template<class T> class C<T, T> { public: void f() { cout << "T == U\n”; } }; int main() { C<float, int>().f(); // 1: Primary template C<int, float>().f(); // 2: T == int C<float, double>().f(); // 3: U == double C<float, float>().f(); // 4: T == U C<float*, float>().f(); // 5: T* used [T is float] C<float, float*>().f(); // 6: U* used [U is float] C<float*, int*>().f(); // 7: T* and U* used [float,int] // The following are ambiguous: // 8: C<int, int>().f(); // 9: C<double, double>().f(); // 10: C<float*, float*>().f(); // 11: C<int, int*>().f(); // 12: C<int*, int*>().f(); } ///:~
  9. Hi, /* RANT Though i have been working in C++ professionally for a while , it seems i do pretty poorly on brain-bench tests. I do understand that they are not the benchmark of a good programmer/problem solver.... but still i am pretty down about some of the questions encountered.Work has its own limitations in terms of increasing technical knowledge. */ So i wanted to start a side project to sharpen up. Can you guys suggest something ? I do not have any specific interests per-say. At the moment i do not wish to pick up another library though , so game dev is off the charts. I want the exercise to cover - STL - POSIX threads - Network communication - Your c++ basics inheritance etc. Thanks
  10. Nokturnal

    Female vocalists

    I'll highly recommend Black Mountain. Though the singer isnt on all the tracks....shes is spectacular. The band is slightly psyc + elements of classic rock. WIN!
  11. Nokturnal

    Book Suggestions

    I am looking for something the size/pop-philosphy of Jhonathan livingston seagull or tuesdays with morrie. Suggesions please ? Thanks!
  12. Nokturnal

    last.fm

    I am gedia ( http://www.last.fm/user/gedia )
  13. I am working on a performance critical app at the moment. Turns out it takes me 0.13 secs to traverse 400000 records in a given multimap (using the iterator). I wish to cut down this time , any suggestions ? What i have done so far is ++Itr instead of Itr++ Compilation flags -qarch=pwr5 (Aix) [Indirectly sets qtune] -O3 -q64 Cheers!
  14. Nokturnal

    Shows you grew up with

    I wonder how Jhonny sokko and his flying robot look now ? :)
  15. Nokturnal

    Daler Menhdi vs Prabhu Deva Sundaram

    God , that brings back old memories. Mehendi 4x! He was pretty much out of the radar till he came back with this
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!