View previous topic :: View next topic |
Author |
Message |
kot1990 Expert Cheater
Reputation: 1
Joined: 06 Sep 2009 Posts: 131 Location: Greece
|
Posted: Sat Oct 23, 2010 5:23 pm Post subject: problem with threads accessing the same memory... |
|
|
I have 2 threads accessing the same memory, an array. My program crashes, and I think it is because of this array. The 2 threads use this array and can modify it at any moment. I think that when the first thread is using the array (eg. in a loop) ,the other one changes the values of the array and a mess follows and my program crashes. What I want to do it to make sure one thread has finished using the array and then releasing that array for the other thread. I know this can be done but haven't done anything like that before.
EDIT: I think I've got the idea, but I'm sure windows has already a built api function for this. I am thinking setting another global variable like a flag like this:
Code: |
BYTE a;
a = 0; <-- no thread is currently using the array
a = 1; <-- array is being used by some thread,, no access
|
and setting a=1 in every thread and the thread that is faster will gain access, the other one will wait in a loop until a = 0 again...
|
|
Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Sat Oct 23, 2010 5:49 pm Post subject: |
|
|
I think C++0x has support for atomic datatypes?
I'm pretty clueless about threading, but you will probably have a much easier time if you just use a library that does the heavy lifting. I think Intel has one and there is also OpenMP
|
|
Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8585 Location: 127.0.0.1
|
|
Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Sat Oct 23, 2010 6:17 pm Post subject: |
|
|
Yes read up on critical sections, mutexs and semaphores.
|
|
Back to top |
|
 |
kot1990 Expert Cheater
Reputation: 1
Joined: 06 Sep 2009 Posts: 131 Location: Greece
|
Posted: Sat Oct 23, 2010 6:28 pm Post subject: |
|
|
thank you for all answers, that was what I was looking for,, critical sections
|
|
Back to top |
|
 |
|