Developer forums (C::B DEVELOPMENT STRICTLY!) > Development
Executing Pthread programming in codeblock fails.
(1/1)
Joy_of_Learning:
// 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 );
}
jordi:
--- Code: ---#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <iostream>
void* functionC(void*);
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
int counter = 0;
int main()
{
int rc1, rc2;
pthread_t thread1, thread2;
int no=100;
// On success, pthread_create returns 0
if( rc1=pthread_create( &thread1, NULL, &functionC, &no) )
{
printf("Thread creation failed: %d\n", rc1);
}
if( rc2=pthread_create( &thread2, NULL, &functionC, &no) )
{
printf("Thread creation failed: %d\n", rc2);
}
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
return 0;
}
void* functionC(void *n)
{
const int* param = static_cast<int*>(n);
pthread_mutex_lock( &mutex1 );
counter += *param;
printf("Counter value: %d\n",counter);
pthread_mutex_unlock( &mutex1 );
return nullptr;
}
--- End code ---
stahta01:
https://wiki.codeblocks.org/index.php?title=FAQ-Compiling_%28general%29#Q:_How_do_I_report_a_compilation_problem_on_the_forums.3F
And, once you learn how to post the build log and information about your compiler I would suggesting finding a newbie programming help site.
Edit: Looks like jordi posted fixes to the OP code because that code complied for me.
Tim S.
Navigation
[0] Message Index
Go to full version