What does pthread create return




















This section looks at the details of these mechanisms and their implications. Passing a single argument to a thread seems straightforward, but is easy to do incorrectly. As a simple example to illustrate the danger, Code Listing 6. The danger of this code can be illustrated with the loop in Code Listing 6. The intent is to pass the value 1 to the first thread, 2 to the second, and so on.

However, it is critical to note that there is only a single copy of the i variable. That is, this code passes the address of the single variable to all 10 threads; the code almost certainly does not pass the intended values. The key problem is that thread creation and execution is asynchronous.

That means that it is impossible to predict when each of the new threads start running. One possible timing is that all 10 threads are created first, leading to i storing the value At that point, each of the threads dereference their respective argptr variable and all get the same value of One common solution to this problem is to cast numeric values as pointers, as shown in Code Listing 6.

What makes this code work is the fact that scalar variables e. Code Listing 6. Casting integral values to pointers and back again is a common practice for passing parameters to pthreads.

However, while it is generally safe in practice, it is potentially a bug on some platforms. Specifically, this technique relies on the fact that pointers are at least as large as standard integer types. That is, int variables are typically but not required to be 32 bits in size. Modern CPU architectures tend to use or bit addresses. On the other hand, assume the argument was declared as a long variable instance.

When passing multiple arguments to a child thread, the standard approach is to group the arguments within a struct declaration, as shown in Code Listing 6. A common mistake with passing arguments in this manner is to declare the struct instance as a local variable instead of using dynamic allocation.

Consider this sample code:. However, there is no guarantee that this happens. The child thread now has a dangling pointer to potentially corrupted data. This is another example of a race condition that can happen with threads. There are three common ways to get return values back from a thread. All three use techniques that are similar to those used for passing arguments. The child thread receives a pointer to the struct instance, using the input parameters as needed. In this case, the values of a and b are added, and the resulting sum is copied back into the struct.

As shown in Code Listing 6. Once the child finishes, the main thread can retrieve all three values a , b , and sum from the struct itself. Asked 11 years, 2 months ago. Active 8 years ago. Viewed 13k times. Thanks for the help! Improve this question. You can create a struct with those two arguments sch and t1 — Kamal. Add a comment. Active Oldest Votes. Improve this answer. Potatoswatter Potatoswatter k 22 22 gold badges silver badges bronze badges. I"m still trying to figure out if you can get a "return" value from a function passed.

So this might be due to my limited knowledge, but if a function is void, can it "return" args? Daniel True, but that needed rewriting anyway and explicitness is generally the better style.

In order to catch the return value of child thread in parent read as main thread. You have to : 1 Joint the child threads with main thread. Edy Edy 41 2 2 bronze badges. Thanks a lot! First timer here, was running out of time, couldn't check all the Options at once :- — Edy. Dennis Miller Dennis Miller 9 9 silver badges 21 21 bronze badges.

Looks like Potatoswatter beat me to it. Sign up or log in Sign up using Google. Sign up using Facebook.



0コメント

  • 1000 / 1000