Than you all for the answers. I read a bit more and I realized I could use Thread::Queue, since it has insert and peek, which I needed. My queue was a priority based queue, so if a command had a higher priority, it would go ahead of a lower priority command.
Thread::Queue worked fine, and now my program works.