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 #30172 is a reply to message #30167] Mon, 13 December 2010 11:35 Go to previous messageGo to previous message
tojocky is currently offline  tojocky
Messages: 607
Registered: April 2008
Location: UK
Contributor

koldo wrote on Mon, 13 December 2010 00:16

Hello all

Nice answers Smile !

For now if I understand well I will use:

- AtomicVar class. Nice!

- Mutex as here:

struct mySharedData {
	int a, b;
	String c;
	Mutex m;
};

mySharedData data;

void ThreadFunction(){
	for(int i = 0; i < 10; i++){
		Thread::Sleep(Random(10)); // Pretend some work...
		data.m.Enter();	    // Enter the section that accesses the shared data
		data.a = i;
		data.c << data.a << "\n";
		Thread::Sleep(20); //Let's pretend that some slow operation happens here (for example file access)
		data.b = data.a;
		data.m.Leave();    // we don't need the exclusive access to data anymore, release the mutex
	}
}





But what about read control?
Example:
read data.a
write data.a and data.b
read data.b (b will be modified)

In this case I think:
1. the write look need to wait until the all reads was finished. and every read locks need to wait until the write lock was finished.
2. Or reads and writes will use only one lock (data.m). But in this case will not possible multiple parallel reads.

Later I will try to provide a simple example.

[Updated on: Mon, 13 December 2010 20:21]

Report message to a moderator

 
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 18:07:20 CEST 2024

Total time taken to generate the page: 0.02062 seconds