Click here to Skip to main content
15,884,885 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to use the following function:

[^]

But , it is said int the remark section that for versions of windows less than Windows 8 "It Is Not Thread Safe". Does this apply to threads withing the process only or it also apply to threads in another process. What I mean by the later is this: will the lack of thread safety be applicable if another instance of my application is lunched.

I intend to use a semaphore to synchronize threads within the process. I was just wondering if I need to factor threads in all other process into my plan to use a semaphore by using named semaphore. So what does a function being "Not Thread Safe" mean in this my context?
Posted
Comments
barneyman 22-Jun-15 20:14pm    
randomly :) be aware that the IStream created by the shell and create...hglobal cannot be QI'd for ISequentialStream, which may or may not be a problem for you
Gbenbam 24-Jun-15 13:46pm    
Noted. Thanks.

Your question is not quite valid. It all depends on what you mean. Thread safety is the concept applied to any number of threads, no matter if they are in the same process or different processes. But it all depends on the reason of unsafe execution. You simply need to understand these reasons and decide accordingly. Some operations can be unsafe for different thread, no matter what they are, but the objects creating unsafe situation might never be shared by two threads if they are in different processes, because the processes are isolated. Are you getting the idea?

First of all, read the MSDN help page thoroughly. It says that the stream is not thread safe, not the function. I don't think we have enough room in a single Quick Answer for detailed discussion of all aspects of thread safety, but let's consider why the memory stream can be not thread safe. This is because it provides the access to the same area of memory to the user of the interface pointer. You can copy the same pointer to another thread in the process, so two threads can access the same memory. Do you have to explain why this is unsafe?

In this particular case, the problem won't appear if you have only one thread working with this memory, and it would not appear if you run another instance of the same application. You need to understand what the processes are. They are isolated, so all memory addresses and handles to system objects have the values only meaningful to the current process. Even if two pointers in two different processes have the same numeric values, in different processes they still point to different physical memory. This is because, first of all, the memory accessed by a process is virtual, which is the hardware-supported memory management technique. And of course two different instances of your applications are two separate isolated processes. So, you should not have to worry.

At the same time, it does not mean that you never face problems with such memory stream related to the lack of thread safety, even if you use memory in different processes. Theoretically speaking, you can run into the problem if you create two stream on the same shared memory object: sharing memory between two processes is possible via IPC:
https://en.wikipedia.org/wiki/Inter-process_communication[^],
https://en.wikipedia.org/wiki/Shared_memory_(interprocess_communication)[^].

I hope this is not your case, but you need to understand the background.

[EDIT]

Also, remember one simple principle of thread synchronization: "not synchronization is best synchronization". There are many ways to avoid conflicts between threads instead of synchronizing them. For example, if you do all calculation on stack, using only the stack memory and memory passed in the function arguments, without sharing, you never create conflicts between threads.

It may require deep understanding on how objects are stored and bound, how thread works, things like that.

—SA
 
Share this answer
 
v3
Comments
nv3 22-Jun-15 11:43am    
Your statement that thread safety applies to multiple threads "no matter if they are in the same process or different processes" is in my opinion not correct. The term thread safety applies *only* to threads within the same process.

Of course, threads in different processes can produce race conditions when accessing the same system resources, but these are normally not called thread-safety problems. For example, two processes, each single-threaded, can cause such problems and it would be absurd to speak of thread-safety issues with regard to single-threaded processes, don't you think.

As a consequence, thread-safety is never an issue in single-threaded processes -- which makes sense after all.
Sergey Alexandrovich Kryukov 22-Jun-15 12:00pm    
I am sure that "thread safety" applies to any two threads. Where did you get that it doesn't? The only thing is: the thread safety is really a concern for threads in separate threads, but "rarely" doesn't mean "never". I can easily show some cases where thread safety is a concern between different processes; and this is all what matters. It's as simple as that.
—SA
Gbenbam 22-Jun-15 11:49am    
Thanks a million times.
Sergey Alexandrovich Kryukov 22-Jun-15 11:57am    
You are welcome.
—SA
Frankie-C 22-Jun-15 12:49pm    
You're both right. It basically depends on what you have to synchronize.
Let's look at aspects of basic execution units, anything that is executing concurrently is a thread (you can dynamically break a thread in subthreads called fibres, but they acts as threads again). The process is only the environment in which a thread executes. we say each process has a private, isolated environment, but in machine terms it is only a different configuration of same resource, memory, through the use of MMU.
At task manager level each thread switching imply switching of its context too, that really means the process.
Anyway in a bunch of running threads each resource or even, and you have not considered it, shared interprocess memory can be a resource concurrently accessed by threads intra or inter process.
In plain words that's why exists so many event and sync objects that works between process.
The concept of thread-safety does only apply to threads within a single process. So you don't have to care about threads in other processes, even if they are running the same application.
 
Share this answer
 
v2
Comments
Gbenbam 22-Jun-15 10:51am    
ok.Thanks.
phil.o 22-Jun-15 10:57am    
Please, mark this question as answered if you are satisfied with the answer that has been provided.
First, it helps to see which questions have already been answered; second, it will give to the one that helped you some reputation points.
Thank you :)
Sergey Alexandrovich Kryukov 22-Jun-15 11:30am    
I would not do that. This answer looks unclear and some statement may be considered formally untrue. Please see my other comment and Solution 2.
—SA
Sergey Alexandrovich Kryukov 22-Jun-15 11:20am    
This answer looks very misleading. I think you personally understand correctly how things work, but the way you answer can really confuse people. First phrase may create impression that thread safety is not applied withing a single process. This is confusing, because in most cases more than one thread in the same process is exactly where thread safety presents most of concerns. It's confusing when you say "thread", not "threads". What a reader should thing? If a thread is only one, there is no safety concerns? But this is obvious, and how "withing a single process" can be applicable. I guess I understand what you mean, but the beginners may not get it.

And the second statement is, strictly speaking, incorrect. It's quite possible to have thread safety issues between different processes, because there is IPC. If you explained how this is applied to the inquirer's case, I could be explained clearly, but presently one might think you are making a universal statement, which is of course not true.

Please see Solution 2 for the explanations.

—SA
nv3 22-Jun-15 11:30am    
Sergey, you are absolutely right about the first line. It should read "threads" and not "thread" in there. (corrected)

About possible conflicts of activities in multiple processes: Yes, multiple processes can produce race conditions when dealing with the same operating system resources. But these are usually not called thread-safety issues, but concurrency issues.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900