Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Multithreading
/****************************************************************************** Welcome to GDB Online. GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl, C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog. Code, Compile, Run and Debug online from anywhere in world. *******************************************************************************/ #include <stdio.h> #include <stdlib.h> #include <pthread.h> pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t condition_var = PTHREAD_COND_INITIALIZER; void *function1 (void *data); void *function2 (void *data); int main () { pthread_t thread1, thread2; pthread_create (&thread1, NULL, &function1, NULL); pthread_create (&thread2, NULL, &function2, NULL); pthead_join (thread1, NULL); pthread_join (thread2, NULL); return 0; } // Write numbers 1-3 and 8-10 as permitted by function2() void * function1 (void *data) { for (;;) { // Lock mutex and wait for signal to release the mutex pthread_mutex_lock (&count_mutex); // Wait while function2 operates on count // mutex unlock if condition variable in function2 signaled pthead_cond_wait (&condition_var, &count_mutex); count++; printf("Counter value functionCount1: %d\n",count); pthread_mutex_unlock( &count_mutex ); if(count >= COUNT_DONE) return(NULL); } } // Write numbers 4-7 void *function2(void *data) { for(;;) { pthread_mutex_lock( &count_mutex ); if( count < COUNT_HALT1 || count > COUNT_HALT2 ) { // Condition of if statement has been met. // Signal to free waiting thread by freeing the mutex. // Note: functionCount1() is now permitted to modify "count". pthread_cond_signal( &condition_var ); } else { count++; printf("Counter value functionCount2: %d\n",count); } pthread_mutex_unlock( &count_mutex ); if(count >= COUNT_DONE) return(NULL); } }
run
|
edit
|
history
|
help
0
Random number IF var change
Byte array to base 64 string
BC150401319
C# - Date Validation
a
nr
Find the missing number in an array2
biswa
Something
SpawnController