• 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
Laval B

Thread pool (basic idea)

9 posts in this topic

Hello everyone

I'm going to need some sort worker threadpool in my project so i can queue independent jobs and let the pool takes care of finding a thread to execute the job. The pool would run a fix number of threads though it could have a minimum and a maximum number of threads (i have no idea how to manage growing the pool).

What i'm thinking so far is this :

- A FIFO queue that will receive the jobs to be executed (from external threads).
- A mutex (critical section on windows or a pthread_mutext_t on POSIX systems) to synchronize access to the queue.
- An event that would be managed by the Pop/Push operations of the queue (event api on windows or pthread_cond_wait on POSIX systems).

The algorithm would be somthing like this :

All threads are sitting on the event. When a job is added to the queue, the event is fired. All the threads (that aren't busy doing executing some job) will try to dequeue the item (only one will because of the mutex) then the event is reset.

That's the basic idea, but alot of things seem wrong with this design. All the threads firing at the same time doesn't sound good. I'm also worried about contention.

I would be interested in having your ideas.

Thanks alot in advance.

P.S.

I am working with C++ and the code must compile on non C++11 compilers. Furthermore, it would be nice to avoid using Boost (not that it's bad quite on the contrary).
2

Share this post


Link to post
Share on other sites
Assuming you've ruled out things like TBB for good reason...

There are many things you can experiment with. I'd recommend going ahead with your plan but designing the interface in such a way as to allow alternative implementations. If your tasks are relatively meaty, you might find that contention isn't much of an issue.

One common way of avoiding contention in lock-based queues is to use a mutex at each end and a dummy node, as described by [url="http://www.research.ibm.com/people/m/michael/podc-1996.pdf"]this paper[/url] (PDF). Anthony Williams' new book, C++ Concurrency in Action also describes the technique.

If you're on Windows, you could experiment with [url="http://msdn.microsoft.com/en-us/library/windows/desktop/aa365198(v=vs.85).aspx"]IO Completion Ports[/url]. They're the Windows-native way of providing a way of spreading work arriving via a single queue across multiple threads.

Another simple approach is to give each thread it's own queue and assign work in a round-robin fashion (you'll need an atomic index to decide which queue to insert work in to next). If the work being enqueued is heterogeneous (i.e. high variance in task duration), you could also experiment with a simple work-stealing mechanism for threads that run out of work (the simplest being to try to steal a task from a randomly chosen queue N times before waiting). Edited by edd²
2

Share this post


Link to post
Share on other sites
[quote name='edd²' timestamp='1338052933' post='4943514']
Assuming you've ruled out things like TBB for good reason...
[/quote]

I haven't ruled out Intel TBB library but i still need to study it, looks like a big thing at first glance.

Thank you very much for the reference on the double lock queue.

I find the idea of using a queue for each thread very interesting though more complicated, especially the part on stealing task for load balancing (but it is very appealing). I must admit that programming lockfree stuff scares me a bit even after reading [url="http://www.drdobbs.com/parallel/210604448"]Herb Sutter's articles on Dr Dobb's[/url].

As for I/O completion port, i already use it for multiplexing network I/O. I also used a fix number of threads to manage those asynchronous I/O completions, for Windows of course (on Solaris, i'll probaly use /dev/poll). I want to use another threadpool mostly to process the requests (database queries, file lookup or transfer, etc) in order to avoid the I/O threads to block too long when a request takes too long to process.

So i'll experiment with what have now and see how it goes.
2

Share this post


Link to post
Share on other sites
In case you didn't know, it appears Solaris 10 and later also has [url="http://developers.sun.com/solaris/articles/event_completion.html"]completion ports[/url].
1

Share this post


Link to post
Share on other sites
[quote name='edd²' timestamp='1338057080' post='4943526']
In case you didn't know, it appears Solaris 10 and later also has [url="http://developers.sun.com/solaris/articles/event_completion.html"]completion ports[/url].
[/quote]

Well, i didn't know and the article i had was only talking about /dev/poll. Thank you again, much appriciated. The code will have to work on Solaris 10.
0

Share this post


Link to post
Share on other sites
[quote]As for I/O completion port, i already use it for multiplexing network I/O. I also used a fix number of threads to manage those asynchronous I/O completions, for Windows of course (on Solaris, i'll probaly use /dev/poll). I want to use another threadpool mostly to process the requests (database queries, file lookup or transfer, etc) in order to avoid the I/O threads to block too long when a request takes too long to process.[/quote]

Considering boost::asio does exactly that, using same API, on all these platforms, inclduing Solaris, has been tested over years by tens of thousands of people, is available under no restriction...

That includes asynchronous file IO, asynhronous callbacks, strands for interoperability with non-threaded APIs, asynchronous networking, thread pooling, ....

[quote]The code will have to work on Solaris 10.[/quote]

Boost and asio are supported on Solaris.


Thread scheduler in asio went through several rewrites, so did other parts. It really is a hard problem, even when foremost experts in platforms and C++ come together. Edited by Antheus
1

Share this post


Link to post
Share on other sites
[quote name='Antheus' timestamp='1338060061' post='4943540']
Thread scheduler in asio went through several rewrites, so did other parts. It really is a hard problem, even when foremost experts in platforms and C++ come together.
[/quote]

You are totally right. Looking at asio (non-boost version) is in my plans but i think getting my hands dirty with theses things will help me understand the way it works better even if i endup using a framework like asio, especially since i have enough time allocated for it.

Thank you for the advice, i appreciate. Edited by Laval B
1

Share this post


Link to post
Share on other sites
[quote name='edd²' timestamp='1338052933' post='4943514']
you'll need an atomic index to decide which queue to insert work in to next
[/quote]

I can always get away with incrementing the index atomically using _InterlockedIncrement on Windows (or [color=#444444][font=Arial,]__sync_add_and_fetch on gcc[/font][/color]) but since there would be a fix number of queues, the index needs to go back to zero when it is incremented from the value size - 1. I'm not sure how to do it with the atomic primitives i have ... there must be a way though. Edited by Laval B
0

Share this post


Link to post
Share on other sites
Just use the result of the atomic increment modulo the number of queues. There may be a tiny blip in the uniformity of the work distribution once every 4 billion-ish tasks if you don't have a number of queues that's a power of two, but that's probably not going to cause you much trouble in the long run [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

If you do decide to do it this way, it might be worth checking that those intrinsics do indeed wrap to 0. I'm pretty sure they do, though. Edited by edd²
1

Share this post


Link to post
Share on other sites
[quote name='edd²' timestamp='1338071216' post='4943576']
If you do decide to do it this way, it might be worth checking that those intrinsics do indeed wrap to 0. I'm pretty sure they do, though.
[/quote]

Yes, even if one of the task attribution isn't exact once in a while, the overall task distribution shoudn't be too bad.

I did a quick test on Windows (i don't have a Solaris box at home), and yes
[left][background=rgb(250, 251, 252)]_InterlockedIncrement wraps. The only problem is that all the atomic increment functions (at least on windows) work on signed integer types which wraps to a negative value. [img]http://public.gamedev.net//public/style_emoticons/default/sad.png[/img][/background][/left]

Edit :

Stupid me, i just have to cast and unsigned int into an int. The fonction wraps to zero.
By the way, the [url="http://www.research.ibm.com/people/m/michael/podc-1996.pdf"]paper[/url] you reffered me to earlier is really interesting. Edited by Laval B
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