• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Anand Baumunk

std::Queue and multithreading

12 posts in this topic

Hello,
I have a problem with the multithreading in c++, since I use it the first time. I have a std::queue, which will have added stuff at the front at random times by thread 1. Thread 2 then reads in the objects at the back of the queue and pops them out.
But since the queue is critical memory, the programm will spit out memoryerrors from time to time.

How can I make sure that the queue is not accessed by thread 2, when thread 1 needs to write. What I found on the www wasn't really helpful, so I hope you can help me out here!

Thanks a bunch.
0

Share this post


Link to post
Share on other sites
You could try using concurrent_queue if it's available on your compiler: http://msdn.microsoft.com/en-us/library/ee355358.aspx

Edit:
Ffs... Full Editor button doesn't work and forum doesn't parse link correctly -.- Edited by Zaoshi Kaba
0

Share this post


Link to post
Share on other sites
Of course, you could use a lock (like [url="http://en.cppreference.com/w/cpp/thread/lock"]the one in C++11[/url]). Edited by Álvaro
2

Share this post


Link to post
Share on other sites
Normally seeing if you don't have access to specialised structures like the concurrent_queue Zaoshi mentioned, you would have to enforce a mutual exclusion on your data so it can accessed safely.

An often-used solution for this is, as Alvaro mentioned, a lock.
Be very careful when using synchronization mechanisms like these though, careless usage of these mechanisms may lead to some really nasty issues (deadlock anyone?) which will guarantee some massive headaches.
0

Share this post


Link to post
Share on other sites
If you don't have access to C++11, then you can also download [url="http://threadingbuildingblocks.org/"]TBB[/url] and use their [url="http://threadingbuildingblocks.org/docs/help/reference/containers_overview/concurrent_queue_cls.htm"]concurrent_queue[/url].
0

Share this post


Link to post
Share on other sites
The easy solution is to use a lock or someone else's thread safe queue as has already been suggested. If you're just trying to solve a problem, this is definitely the way to go.

If you're interested in really digging into concurrent programming for the purpose of learning, you may want to investigate writing your own lockless queue where exactly one thread can write and exactly one thread can read. It doesn't require a ton of code to do something simple and it's well worth the learning experience. The need to pass data continuously from thread A to thread B in this way is extremely common. An easy implementation is to have a "read pointer" and a "write pointer" that wraps in a circular buffer - just be careful of order of operations and make the read pointer and write pointer volatile. The "pointer" can be an index into the static-sized circular buffer.

Using a queue like this is great for passing data to be processed. It can also proxy a function call to another thread by using the "command" pattern. You can even pass exceptions this way. Edited by aclysma
0

Share this post


Link to post
Share on other sites
I would recommend a [url="http://en.cppreference.com/w/cpp/thread/mutex"]std::mutex[/url] with a [url="http://en.cppreference.com/w/cpp/thread/condition_variable"]std::condition_variable[/url]. See [url="http://stackoverflow.com/questions/1055398/differences-between-conditional-variables-mutexes-and-locks"]Stack Overflow[/url] for some explanation. That is what I use for my thread pool and queue of jobs. You'll find an example (using a queue) in the documentation of the condition variable.
0

Share this post


Link to post
Share on other sites
I recently saw an implementation of a lock free ringbuffer which would fit your problem, unfortunately it is written in Go, but it is a very small amount of code so if you are interested you could take a look:

https://github.com/textnode/gringo/blob/master/gringo.go
0

Share this post


Link to post
Share on other sites
I am not sure why you recommend a lock free ringbuffer, it will use a busy wait, won't it? A busy wait is seldom a good idea.

The Go example is short and nice, and will always yield the CPU to another process, if there is one that wants to execute. But if there isn't, it will busy wait.
0

Share this post


Link to post
Share on other sites
[quote name='larspensjo' timestamp='1354832893' post='5007905']
I am not sure why you recommend a lock free ringbuffer, it will use a busy wait, won't it? A busy wait is seldom a good idea.
[/quote]
A busy wait is actually the [i]best[/i] solution for situations with high contention where wait times tend to be quite low, because it ends up being less expensive then the context switch to a sleep state. However there is a break-even point where spinning is less efficient than sleeping. Really good implementations of concurrent data structures typically start with a busy wait, and eventually go to sleep if they wait too long.
0

Share this post


Link to post
Share on other sites
Most good OSes will briefly spin-wait on mutexes/critical sections and then context switch as necessary. So reinventing that wheel is kind of a waste.
1

Share this post


Link to post
Share on other sites
[quote name='ApochPiQ' timestamp='1354744046' post='5007535']I strongly suggest not messing with it until you are highly proficient with concurrent systems and understand them deeply. Moreover, it requires a very rich comprehension of how the OS and even hardware work to do lockless correctly. IMHO, suggesting lockless programming to someone who is not already very experienced with concurrency is utterly bad advice.[/quote]Quoted for emphasis.
I thought I had a very professional level of understanding regarding multi-threaded software, so I went off and wrote my own lock-free queue. Took me a month of weekends of debugging until I thought I had something solid...
After using my queue with no problems for 6 months ([i]and having the unit tests run and succeed every day[/i]), it's unit test finally failed one day, due to a small bug!
Now, I've had years of practice doing it, and I'm still quote anxious whenever I'm doing any lock-free stuff. Definitely get some practice and do your homework before stepping into that arena! Also, I've seen a lot of implementations that make use of dynamic memory ([i][font=courier new,courier,monospace]new[/font]/[font=courier new,courier,monospace]malloc[/font]/etc[/i]), which locks anyway, defeating the point.
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0