Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <semaphore.h>
- /* Global variables */
- int x = 0;
- sem_t sync;
- /* Thread function */
- void *my_func(void *arg)
- {
- /* wait for signal from main thread */
- sem_wait(&sync);
- printf("X = %d\n", x); //x er value 55 hobe jokon lock khule jab
- }
- void main ()
- {
- pthread_t thread; //
- /* semaphore sync should be initialized by 0 */
- if (sem_init(&sync, 0, 0) == -1) { //initialize value, semapore ar value asign kore.2nd
- // value ta shared value. 0 thakle onno process semapore k use korte parbe na.last er ta value.
- perror("Could not initialize mylock semaphore");
- exit(2);
- }
- if (pthread_create(&thread, NULL, my_func, NULL) < 0) { //thread create
- perror("Error: thread cannot be created");
- exit(1);
- }
- /* perform some operation(s) */
- x = 55;
- /* send signal to the created thread */
- sem_post(&sync); //count er value 1 barbe
- /* wait for created thread to terminate */
- pthread_join(thread, NULL);
- /* destroy semaphore sync */
- sem_destroy(&sync);
- exit(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement