// I am learning C++ and started learning and practising OS concepts when I execute the below code, the pthread_create function fails at 3rd argument I tried to resolve the issue on my own passing the function with adress and without address to pthread_create. I need forum members to let me know where I am going wrong any suggestions. I am using code blocks 20.03. I am attaching the output log also.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include<iostream>
void *functionC();
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
int counter = 0;
int main()
{
int rc1, rc2;
pthread_t thread1, thread2;
int no=100;
if( (rc1=pthread_create( &thread1, NULL, &functionC, (void *)no)) )
{
printf("Thread creation failed: %d\n", rc1);
}
if( (rc2=pthread_create( &thread2, NULL, &functionC, (void *)no)) )
{
printf("Thread creation failed: %d\n", rc2);
}
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
exit(0);
}
void *functionC(void *n)
{
pthread_mutex_lock( &mutex1 );
counter++;
printf("Counter value: %d\n",counter);
pthread_mutex_unlock( &mutex1 );
}