线程与子线程

2020-12-16  本文已影响0人  我的小诗

It depends on what exactly you mean by “within”. As others have pointed

out, if you are asking about the “create POSIX thread” operation, then

the answer is: yes, pthread_create() can be called from a POSIX thread. Depending on how you look at it, the

standard execution of a program is merely a single POSIX thread, and

the “create thread” function is simply creating a sibling of that

thread. Under POSIX, threads are all fully privileged siblings of each

other. They can all create more siblings, synchronize with any other

sibling, join with any other (joinable) sibling, they all share a single

view of memory (within the constraints of coherency) and all have the

same permissions on all of the program’s memory, etc.

Keep in mind that there is no concept of nested threads with a something like hierarchy. If you wanna achieve something like thisfork(2)is your friend although not fully portable.

Whilefread()andfwrite()are thread safe, the stream buffer represented by theFILE*is not. So you can have multiple threads accessing the same file, but not via the sameFILE*- each thread must have its own, and the file to which they refer must be shareable - which is OS dependent.

上一篇 下一篇

猜你喜欢

热点阅读