Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Pthreads
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Example== An example illustrating the use of pthreads in C: <syntaxhighlight lang="c"> #include <stdio.h> #include <stdlib.h> #include <assert.h> #include <pthread.h> #include <unistd.h> #define NUM_THREADS 5 void *perform_work(void *arguments){ int index = *((int *)arguments); int sleep_time = 1 + rand() % NUM_THREADS; printf("Thread %d: Started.\n", index); printf("Thread %d: Will be sleeping for %d seconds.\n", index, sleep_time); sleep(sleep_time); printf("Thread %d: Ended.\n", index); return NULL; } int main(void) { pthread_t threads[NUM_THREADS]; int thread_args[NUM_THREADS]; int i; int result_code; //create all threads one by one for (i = 0; i < NUM_THREADS; i++) { printf("In main: Creating thread %d.\n", i); thread_args[i] = i; result_code = pthread_create(&threads[i], NULL, perform_work, &thread_args[i]); assert(!result_code); } printf("In main: All threads are created.\n"); //wait for each thread to complete for (i = 0; i < NUM_THREADS; i++) { result_code = pthread_join(threads[i], NULL); assert(!result_code); printf("In main: Thread %d has ended.\n", i); } printf("Main program has ended.\n"); return 0; } </syntaxhighlight> This program creates five threads, each executing the function ''perform_work'' that prints the unique number of this thread to standard output. If a programmer wanted the threads to communicate with each other, this would require defining a variable outside of the scope of any of the functions, making it a [[global variable]]. This program can be compiled using the [[GNU Compiler Collection|gcc]] compiler with the following command: gcc pthreads_demo.c -pthread -o pthreads_demo Here is one of the many possible outputs from running this program. <syntaxhighlight lang="output"> In main: Creating thread 0. In main: Creating thread 1. In main: Creating thread 2. In main: Creating thread 3. Thread 0: Started. In main: Creating thread 4. Thread 3: Started. Thread 2: Started. Thread 0: Will be sleeping for 3 seconds. Thread 1: Started. Thread 1: Will be sleeping for 5 seconds. Thread 2: Will be sleeping for 4 seconds. Thread 4: Started. Thread 4: Will be sleeping for 1 seconds. In main: All threads are created. Thread 3: Will be sleeping for 4 seconds. Thread 4: Ended. Thread 0: Ended. In main: Thread 0 has ended. Thread 2: Ended. Thread 3: Ended. Thread 1: Ended. In main: Thread 1 has ended. In main: Thread 2 has ended. In main: Thread 3 has ended. In main: Thread 4 has ended. Main program has ended. </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)