Code::Blocks Forums

Developer forums (C::B DEVELOPMENT STRICTLY!) => Development => Topic started by: Joy_of_Learning on March 26, 2022, 11:44:31 am

Title: Executing Pthread programming in codeblock fails.
Post by: Joy_of_Learning on March 26, 2022, 11:44:31 am
// 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 );
}



Title: Re: Executing Pthread programming in codeblock fails.
Post by: jordi on March 26, 2022, 04:51:56 pm
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;
}
Title: Re: Executing Pthread programming in codeblock fails.
Post by: stahta01 on March 26, 2022, 06:49:27 pm
https://wiki.codeblocks.org/index.php?title=FAQ-Compiling_%28general%29#Q:_How_do_I_report_a_compilation_problem_on_the_forums.3F (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.