Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ MT-multithreading and servers » Use same variable in different threads
Re: Use same variable in different threads [message #30123 is a reply to message #30122] Fri, 10 December 2010 06:49 Go to previous messageGo to previous message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

Quote:


The question now is, how to program it?

That is always the question Smile Unfortunatelly the answer is not simple and it depends a lot on what you are trying to achieve...

As Didier pointed out, for simple int compatible variables Atomic* functions are best. For something rarely used (e.g. flag to stop all threads) I ussually use volatile. And fore manipulating larger objects, you should definitely use mutex. The basic approach for mutex might be to lock it everytime before you access the variable that is shared between threads and unlock after you are done with it. I think it is best to have one mutex for each object that is accessed from various threads.

In my experience, the best solution is to avoid inter-thread communication whenever possible Smile Of course, than is definitely not an universal solution...

Honza
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: OpenMP
Next Topic: Different native pthread.h implementations
Goto Forum:
  


Current Time: Thu May 09 14:33:20 CEST 2024

Total time taken to generate the page: 0.01996 seconds