Remember printf ? Yeah right you recoded it yourself, that's good, but for this project, you can use the stdlib one, the real one.
And you also have access to a lot of other functions, but you can't use your libft for this project.
Some of these functions are only used for the bonus part, I didn't used them so I'll let you search how to use them. For this project you'll have to compile your programm with the -pthread flag.
usleep()
intusleep(useconds_t usec);
usleep() is a function in the C standard library that causes the calling process to sleep for a specified number of microseconds.
usleep() example
#include<stdio.h>#include<unistd.h>intmain(void) {printf("Sleeping for 500000 microseconds...\n");usleep(500000);printf("Done sleeping.\n");return0;}
The gettimeofday() function gets the system’s clock time. The current time is expressed in elapsed seconds and microseconds since 00:00:00, January 1, 1970 (Unix Epoch).
The pthread_create() function starts a new thread in the calling process. The new thread starts execution by invoking start_routine(); arg is passed as the sole argument of start_routine().
You can find more information about pthread_create()here.
pthread_join()
intpthread_join(pthread_t thread,void**retval);
The pthread_join() function waits for the thread specified by
thread to terminate. If that thread has already terminated, then
pthread_join() returns immediately. The thread specified by
thread must be joinable.
The pthread_join() function waits for the thread specified by thread to terminate. If that thread has already terminated, then pthread_join() returns immediately.
pthread_create() & pthread_join() example
#include<pthread.h>#include<stdlib.h>#include<stdio.h>void*thread(void*arg) {char*ret;printf("thread() entered with argument '%s'\n", arg);if ((ret = (char*) malloc(20)) ==NULL) {perror("malloc() error");exit(2); }strcpy(ret,"This is a test");pthread_exit(ret);}main() {pthread_t thid;void*ret;if (pthread_create(&thid,NULL, thread,"thread 1")!=0) {perror("pthread_create() error");exit(1); }if (pthread_join(thid,&ret)!=0) {perror("pthread_create() error");exit(3); }printf("thread exited with '%s'\n", ret);}
In this example, we use the pthread_create() function to create a new thread that is initiated with the function called thread().
We then wait for the thread to terminate before printing the return value of the thread.
You can find more information about pthread_join()here.
The pthread_mutex_init() function initializes the mutex referenced by mutex with the attributes specified by attr. If attr is NULL, the default mutex attributes are used. When the mutex is successfully initialized, the mutex state becomes initialized and unlocked.
pthread_mutex_init() example
#include<pthread.h>intmain(void){pthread_mutex_t mutex;// Initializing the mutexpthread_mutex_init(&mutex,NULL);}
The above example uses the pthread_mutex_init() function to initialize a new mutex that can be used from all threads that we want to.
You can find more information about pthread_mutex_init()here.
pthread_mutex_destroy()
intpthread_mutex_destroy(pthread_mutex_t*mutex);
The pthread_mutex_destroy() function destroys the mutex object referenced by mutex. The mutex object becomes uninitialized and can be reinitialized with pthread_mutex_init() if needed.
pthread_mutex_init() & pthread_mutex_destroy() example
#include<pthread.h>#incluce <stdio.h>intmain(void){pthread_mutex_t mutex;// Initializing the mutexpthread_mutex_init(&mutex,NULL);printf("You can use the mutex from now on");// Destroying the mutexpthread_mutex_destroy(&mutex);}
You can find more information about pthread_mutex_destroy()here.
pthread_mutex_lock()
intpthread_mutex_lock(pthread_mutex_t*mutex);
The pthread_mutex_lock() function locks the mutex referenced by mutex.
You can find more information about pthread_mutext_lock()here.
pthread_mutex_unlock()
intpthread_mutex_unlock(pthread_mutex_t*mutex);
The pthread_mutex_unlock() function unlocks the mutex referenced by mutex.
pthread_mutex_lock() & pthread_mutex_unlock() example