Computer Science Canada

Do threads share same heap?

Author:  Geminias [ Tue Dec 11, 2007 12:44 pm ]
Post subject:  Do threads share same heap?

If I have a thread-safe object allocated on the heap is it okay to give multiple threads the pointer to it to utilize its methods? (Or will this cause a GPF?)

Author:  Geminias [ Tue Dec 11, 2007 1:52 pm ]
Post subject:  RE:Do threads share same heap?

The answer is yes!

Author:  md [ Tue Dec 11, 2007 4:58 pm ]
Post subject:  RE:Do threads share same heap?

Yes you can; but there are issues. Even though they don't technically run simultaneously (though with modern computers they really can) threads are best thought of as operating at the same time. You therefor need to take measures to make sure that any data structures shared by threads are either read-only by all but one thread or have measures in place to prevent concurrent writes by more then one thread. Usually people use Mutex's

Author:  Geminias [ Tue Dec 11, 2007 5:33 pm ]
Post subject:  RE:Do threads share same heap?

Yes, well I did say thread safe, meaning it implements semaphores. I don't really know why I asked my question... I think my brain temporarily froze. Of course the heap is accessible to threads of a process lol. I've just never designed an object called by multiple threads before, and the reason it came to me is because I was considering having a class that each thread would use, but wouldn't change, thus removing the copy-by-write overhead and giving merit to the use of such an object. I figure I mine as well be courteous and just use one of these objects and share it between threads. If I was to be lazy and just give each thread this object, and all programmers followed my lead, than computers would be really slow even with the incredible dataspeeds we're capable of and the ever decreasing price of ram lol


: