What is the first thing you need to do in order to use pthread?
#include <pthread.h>
How would you compile a C++ program with multithreading capabilities in the Linux
Terminal?
c++ program.cpp -lpthread -o program
What is the first parameter in pthread_create?
thread, initialized as pthread_t t1; on a previous line in order to pass information on the
thread through a variable
What is the 2nd parameter in pthread_create?
attribute, not necessary for our class but it's a pointer to a structure that can define
thread attributes such as stack address etc
What should you pass through the 2nd parameter in pthread_create by default?
NULL
What is the 3rd parameter in pthread_create?
the function in which you want to run through the thread, EX: mySort
Do functions in the 3rd parameter of pthread_create include their own parameters?
NO, the 4th parameter of pthread_create includes the function's parameters.
What is the 4th parameter in pthread_create?
arg (arglist), pointer to void that contains the arguments to the function defined in the
earlier argument
What is the function to terminate a thread?
void pthread_exit(void *retval);
What is the function to make a thread wait?
pthread_join(thread1, NULL);
Why is the pthread_join function usually required?
We need to wait till the threads are complete, otherwise the main function will continue
while the threads are processing, risking termination of the program(return 0;) before
the threads are completed.
How do you declare a string in C/C++? (cstring)
, char s[5]; can visualize it as an array with 5 element slots
What should always be included at the end of a C/C++ string? (cstring)
\0, or null pointer
Is char a[4] = ('a', 'b', 'c', 'd') valid? (cstring)
no, assigning 5 characters to a string that has 4 characters. \0 is automatically initialized
as the end of any given string.
What is the strlen() function?
takes a string as its argument and returns the string's length, excluding the \0 at the
end.
'''
int strlen(char n []) {
int i = 0;
while (n[i++] != '\0') {}
return i - 1;
}
'''
What is the difference between pass by value and pass by reference?
When passing by value, the value passed in a parameter is not changed after the
function operations, whilst passing by reference the value does change.
Pass by reference:
myfunc(& int x){ x+= 5; }
int main() { int x = 10; myfunc(x); print(x) }
OUTPUT: 15
Pass by value:
myfunc(int x){ x+= 5; }
int main() { int x = 10; myfunc(x); print(x) }
OUTPUT: 10
What is sizeof()?
returns the size of either a variable, type or expression allocated in the memory in bytes.
What is the function of the linux command "cd"?
to change directories and change between file lists
What is the function of the linux command "ls"?
most commonly used command, lists all folders and files in a particular file path
What is the function of the linux command "man"?