Hey,
given this code:
Code:
#include <stdio.h>
#include <pthread.h>
int test = 0;
pthread_mutex_t lock;
void* jobOne(){
while(1){
pthread_mutex_lock(&lock);
printf("%d", test);
pthread_mutex_unlock(&lock);
}
return NULL;
}
void* jobTwo(){
while(1){
test++;
}
return NULL;
}
int main(int argc, const char * argv[]) {
pthread_t thread01;
pthread_t thread02;
pthread_mutex_init(&lock, NULL);
pthread_create(&thread01, NULL, &jobOne, NULL);
pthread_create(&thread02, NULL, &jobTwo, NULL);
}
I have serveral question:
1. Does mutextlock mean, that all other threads are not getting exceuted as long as the mutext ist not locked? I presume no.
2. How can I code, that if the current mutext is locked, the thread seeing it is locked shall do something else?
3. How do I express the following thing:
"If the mutext is locked, wait until it is unlocked, but then do not lock it when entering the critical area"?
4. Why does the process get killed by the OS?
5. Why does p_thread_create demands a void * (void) at all
I also have issues with void pointers:
If i point to a position in space, where no function is but should be, what would happen to the process?
What if I point with a function pointer to something where data should be, meanwhile passing arguments, like:
Code:
void func (void(*f)(int));
Thanks!