0% found this document useful (0 votes)
40 views2 pages

Ex-6Implement Threading & Synchronization Applications

This C code implements threading and synchronization using pthreads. It creates two threads using pthread_create and assigns each thread the doSomething function. doSomething increments a shared counter variable, prints a start and finish message, and contains a for loop to simulate work. The main thread then waits for the two created threads to finish using pthread_join before ending. When run, it prints the start and finish messages from each thread in no particular order, demonstrating asynchronous parallel execution.

Uploaded by

Vasantha Kumari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views2 pages

Ex-6Implement Threading & Synchronization Applications

This C code implements threading and synchronization using pthreads. It creates two threads using pthread_create and assigns each thread the doSomething function. doSomething increments a shared counter variable, prints a start and finish message, and contains a for loop to simulate work. The main thread then waits for the two created threads to finish using pthread_join before ending. When run, it prints the start and finish messages from each thread in no particular order, demonstrating asynchronous parallel execution.

Uploaded by

Vasantha Kumari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Ex-6Implement Threading & Synchronization Applications

#include<stdio.h>

#include<string.h>

#include<pthread.h>

#include<stdlib.h>

#include<unistd.h>

pthread_t tid[2];

int counter;

void* doSomeThing(void *arg)

unsigned long i = 0;

counter += 1;

printf("\n Job %d started\n", counter);

for(i=0; i<(0xFFFFFFFF);i++);

printf("\n Job %d finished\n", counter);

return NULL;

int main(void)

int i = 0;
int err;

while(i < 2)

err = pthread_create(&(tid[i]), NULL, &doSomeThing, NULL);

if (err != 0)

printf("\ncan't create thread :[%s]", strerror(err));

i++;

pthread_join(tid[0], NULL);

pthread_join(tid[1], NULL);

return 0;

output

$ ./tgsthreads

Job 1 started

Job 2 started

Job 2 finished

Job 2 finished

You might also like