Author Topic: Executing Pthread programming in codeblock fails.  (Read 1817 times)

Offline Joy_of_Learning

  • Single posting newcomer
  • *
  • Posts: 1
Executing Pthread programming in codeblock fails.
« 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 );
}




Offline jordi

  • Multiple posting newcomer
  • *
  • Posts: 18
Re: Executing Pthread programming in codeblock fails.
« Reply #1 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;
}

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7323
    • My Best Post
Re: Executing Pthread programming in codeblock fails.
« Reply #2 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

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.
« Last Edit: March 26, 2022, 06:53:01 pm by stahta01 »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 32 bit.
On Debian Stretch, compiling CB Trunk against wxWidgets 3.0.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org