Note that the answer depends in large part on the terms under which the open-source project accepts your code.
Most will, at the very least, have a statement saying that by contributing it you have granted the project rights to use, distribute, etc. your contribution, and granted all the project's users rights to look at and execute your code. That doesn't negate your copyright, but it means you have irrevocably agreed to license it for use in that project.
Depending on the terms under which the project is then distributed and the details of the license you have agreed to, that may or may not give everyone else with access to the project rights to use your code in other contexts.
It's your responsibility to read and understand these details before you contribute code. If in doubt, you can ask the folks running the project to explain what they intended their licenses to say, but remember that free legal advice -- including everything you're seeing in response to your question -- is worth exactly what you paid for it.
If this really matters to you, get the exact language and hire your own lawyer to examine it for pitfalls. Or don't contribute code you aren't willing to see escape into general use. Or get someone else to do that research for you -- my employer has fairly specific rules about what kinds of open source I am and am not allowed to get involved with.