multithreading - C - Thread result in main -
I am studying C but my book actually provides some resources. I want to know whether it is possible and useful to some extent to use thread and then pass the main result. In case, I would like to know how to transmit the information from one thread to the main (something like returning method in Java) The only solution is to use a global variable? To tell what I would like to do, this is a little example, is it possible / useful?
Main:
int i = 1; pthread_create (tid, void, functionTH, zero); Int z = // getResultFromThread thread "function":
zero * functionTH () {int z = 2; // Return the value 2 main and assign it to K in the main. } See the prototype
.
int pthread_create (pthread_t * restricted thread, const pthread_attr_t * restriction, zero * (* start_routine) (zero *), zero * restricted arg); Your thread ( start_routine ) is a function that returns zero * . Then your code is incorrect because your thread function does not return any returns. As you can guess, you return an indicator and return a value to your main thread.
You can wait to read this value by calling the thread. The value returned from the thread is passed through the value_ptr Arg.
The pthread_join () function will suspend the execution of the calling thread until the end of the target thread, until the target thread has already been aborted by a successful pthread_join () with a non-NULL value_ptr The value given to pthread_exit () will be made available in the location referenced by value_ptr.
Of course, you do not have to use this mechanism. Global variables can work just fine, but you have to synchronize access to variables or you will end up with race conditions. .
Comments
Post a Comment