Threading in C++

Blog

I know, I know. It has been over a month since my last post. Sadly, this graduate project of mine has been taking up all of my time.

Of course the time spent has not been a waste. In fact, it has been far from it. Part of the project involves users participating in a multiplayer trivia game done in Adobe Flash. The idea is that Flash will communicate with PHP via ZendAMF and PHP will talk to a C++ server via network packets. Since I haven’t written in C++ in ~3-4 years, it has taken me a bit to get back into the hang of things.

Things have changed since the last time I wrote in C++, especially because now I’m writing for a Linux OS, using direct network communication, and running in a threaded environment. I followed some people’s advice and started to look into the Boost Libraries. After about a week of frustration working with Boost.Thread and Boost.ASIO, I realized that everything I need to do can be done with pthread.h and sys/socket.h.

In the end it was primarily my fault as I didn’t realize that Boost was designed to make things work on different platforms without rewriting OS specific code. The problem is, the simple things I needed to do are therefore much more confusing to write and the developers didn’t produce the best example of documentation.

So after ditching Boost completly, I did a little bit of research to find the appropriate method for locking in muti-threaded environments for Linux. With pthread, the locking is extremely easy. Here are some basic examples:

  1. // This is the include file you need for this to work
  2. #include <pthread.h>
  3.  
  4. // If you need a mutex for an entire class (static), you can
  5. //  easily declare it with the PTHREAD_MUTEX_INITIALIZER macro.
  6. static pthread_mutex_t mutex_test = PTHREAD_MUTEX_INITIALIZER;
  7.  
  8. // Otherwise, just declare it with the type pthread_mutex_t
  9. pthread_mutex_t mutex_test;
  10. // In this case, you will have to initialize it with this call
  11. //  The second parameter is for attributes, in this case
  12. //  we want only the default attributes, i.e. NULL
  13. pthread_mutex_init( &mutex_test, NULL );
  14.  
  15. // This function will cause the mutex to lock (if available)
  16. //  or wait in a deadlock until it becomes available.
  17. pthread_mutex_lock( &mutex_test );
  18. // Then this will unlock the mutex for other threads to use.
  19. pthread_mutex_unlock( &mutex_test );
  20.  
  21. // Finally, we can destroy a mutex with this last call.
  22. pthread_mutex_destroy( &mutex_test );

Again, these are basic examples. If you wanted to look deeper into locking, you can even specify mutex types (Error Checking, Recursive, etc.)

Oh, and by the way: remember to compile with -pthread

No Comments

Leave a Reply

Allowed tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>



  • Donate

    If my work has helped you and you want to return the favor, you could purchase something for me from my Amazon Wish List or send me a donation via PayPal.

  • License

    Unless otherwise noted, all source code and compiled files published on this website are released under the terms of the GNU Lesser General Public License.