cs:posix:threads_example1
Return to Home page
If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it
You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: https://www.skenz.it/cs/posix/threads_example1?do=diff&rev2%5B0%5D=&rev2%5B1%5D=1512020853&difftype=sidebyside
no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
— | cs:posix:threads_example1 [2024/04/08 22:35] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Threads Example 1 ====== | ||
+ | **Concepts: | ||
+ | Example regarding basic functionalities of POSIX threads | ||
+ | |||
+ | **Text:**\\ | ||
+ | Implement an example program that highlights the main functionalities of threads. | ||
+ | |||
+ | **Solution: | ||
+ | <file C threads_example1.c> | ||
+ | /* | ||
+ | - Creation of the threads with pthread_create() | ||
+ | - Exchange with the threads of a structure with different values for each thread | ||
+ | - Exchange with the main of the returned value of a thread | ||
+ | - Wait a thread with pthread_join() | ||
+ | - Global variables and threads | ||
+ | */ | ||
+ | |||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | #define N_THREAD 3 | ||
+ | |||
+ | |||
+ | /* Global variable, all the threads can see this variable, if a thread modify the value of the variable, all the other threads see the modified value. Important: variables cannot be modified by more than one thread at a time, otherwise a race condition is generated. */ | ||
+ | int global_variable = 10; | ||
+ | |||
+ | |||
+ | typedef struct { | ||
+ | int thread_num; | ||
+ | char letter; | ||
+ | } my_t; | ||
+ | |||
+ | |||
+ | static void *process (void *arg) | ||
+ | { | ||
+ | int i; | ||
+ | my_t *param = (my_t *) arg; // cast the void argument to the real type | ||
+ | |||
+ | if (param-> | ||
+ | fprintf (stdout, " | ||
+ | for (i = 0; i < 10; i++) | ||
+ | write (STDOUT_FILENO, | ||
+ | printf(" | ||
+ | param-> | ||
+ | pthread_exit(& | ||
+ | } | ||
+ | |||
+ | |||
+ | int main (void) | ||
+ | { | ||
+ | int retcode; | ||
+ | pthread_t th[N_THREAD]; | ||
+ | char *retval; | ||
+ | my_t value[N_THREAD]; | ||
+ | int i = 0; | ||
+ | | ||
+ | for(i=0; i< | ||
+ | value[i].letter = ' | ||
+ | } | ||
+ | |||
+ | for(i=0; i< | ||
+ | value[i].thread_num = i; | ||
+ | retcode = pthread_create (& | ||
+ | if (retcode != 0) | ||
+ | fprintf (stderr, " | ||
+ | } | ||
+ | |||
+ | |||
+ | for(i=0; i< | ||
+ | |||
+ | retcode = pthread_join (th[i], (void *)& | ||
+ | if (retcode != 0) | ||
+ | fprintf (stderr, " | ||
+ | |||
+ | fprintf(stdout, | ||
+ | } | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | **Comments: | ||
+ | This example shows how to pass a structure to threads (in '' | ||
+ | |||
+ | Threads return a results (a data) to the '' | ||
+ | |||
+ | The output of the threads is printed in an interleaved way because they are executed concurrently. | ||
+ | |||
+ | Global variable can be accessed by all the thread. If you want to modify them, you need to do it in a non concurrent way. To this purpose // | ||
+ | |||
+ | **Compilation: | ||
+ | To compile the program type the following command: | ||
+ | <code bash> | ||
+ | gcc -Wall -o threads_example threads_example1.c -lpthread | ||
+ | </ | ||
If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it
You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: https://www.skenz.it/cs/posix/threads_example1?do=diff&rev2%5B0%5D=&rev2%5B1%5D=1512020853&difftype=sidebyside
/web/htdocs/www.skenz.it/home/data/pages/cs/posix/threads_example1.txt · Last modified: 2024/04/08 22:35 by 127.0.0.1