Jump to content

  • Log In with Google      Sign In   
  • Create Account

std::Queue and multithreading


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
12 replies to this topic

#1 gnomgrol   Members   -  Reputation: 624

Like
0Likes
Like

Posted 04 December 2012 - 12:35 PM

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.

Sponsor:

#2 Zaoshi Kaba   Crossbones+   -  Reputation: 4592

Like
0Likes
Like

Posted 04 December 2012 - 01:53 PM

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, 04 December 2012 - 01:55 PM.


#3 Álvaro   Crossbones+   -  Reputation: 13913

Like
2Likes
Like

Posted 04 December 2012 - 01:58 PM

Of course, you could use a lock (like the one in C++11).

Edited by Álvaro, 04 December 2012 - 01:59 PM.


#4 Radikalizm   Crossbones+   -  Reputation: 2986

Like
0Likes
Like

Posted 04 December 2012 - 02:06 PM

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.

I gets all your texture budgets!


#5 Zipster   Crossbones+   -  Reputation: 878

Like
0Likes
Like

Posted 04 December 2012 - 02:08 PM

If you don't have access to C++11, then you can also download TBB and use their concurrent_queue.

#6 aclysma   Members   -  Reputation: 180

Like
0Likes
Like

Posted 05 December 2012 - 11:02 AM

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, 05 December 2012 - 11:04 AM.


#7 larspensjo   Members   -  Reputation: 1557

Like
0Likes
Like

Posted 05 December 2012 - 03:26 PM

I would recommend a std::mutex with a std::condition_variable. See Stack Overflow 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.
Current project: Ephenation.
Sharing OpenGL experiences: http://ephenationopengl.blogspot.com/

#8 ApochPiQ   Moderators   -  Reputation: 16401

Like
3Likes
Like

Posted 05 December 2012 - 03:47 PM

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.



Lockless programming is a black art. 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.

#9 Inferiarum   Members   -  Reputation: 733

Like
0Likes
Like

Posted 05 December 2012 - 04:49 PM

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

#10 larspensjo   Members   -  Reputation: 1557

Like
0Likes
Like

Posted 06 December 2012 - 04:28 PM

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.
Current project: Ephenation.
Sharing OpenGL experiences: http://ephenationopengl.blogspot.com/

#11 Zipster   Crossbones+   -  Reputation: 878

Like
0Likes
Like

Posted 06 December 2012 - 05:37 PM

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.

A busy wait is actually the best 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.

#12 ApochPiQ   Moderators   -  Reputation: 16401

Like
1Likes
Like

Posted 06 December 2012 - 07:28 PM

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.

#13 Hodgman   Moderators   -  Reputation: 31851

Like
1Likes
Like

Posted 06 December 2012 - 08:58 PM

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.

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 (and having the unit tests run and succeed every day), 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 (new/malloc/etc), which locks anyway, defeating the point.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS