why does Pthread Signal from main, hangs the code?











up vote
-1
down vote

favorite












The requirement for the sample application creating using pthread is given below:




  1. A single thread is created from main using Pthread

  2. Inside thread, Mutex is locked, a counter counts the value and while loop is incremented, While is set to the max count of 10.

  3. after while loop finishes, the mutex is unlocked.


The above requirement I have tried implementing using pthread



Code is shown below:



#include <pthread.h>
#include <stdio.h>
pthread_mutex_t count_mutex;
pthread_cond_t count_threshold_cv;

int samples = 10;
int count = 0;

struct example
{
int i;
int a;
};

void *inc_x(void *x_void_ptr)
{
pthread_mutex_lock(&count_mutex);
printf("Thread is locked n");

while(count < samples)
{
printf("inside While loop n");

struct example *E2_ptr;
E2_ptr = (struct example *)x_void_ptr;
printf("inside thread count = %dn",count);
E2_ptr->a = count;
E2_ptr->i = (count + 1);
count ++;
//pthread_cond_wait(&count_threshold_cv, &count_mutex);
}

pthread_mutex_unlock(&count_mutex);
printf ( "n Test Successful for Threadn");

pthread_exit(NULL);
}

int main()
{

int x = 100, y = 0,i = 0;
struct example *E1_ptr;

E1_ptr->a = 0;
E1_ptr->i = 0;

printf("Beforet E1_ptr->a = %dt, E1_ptr->i = %dn",E1_ptr->a,E1_ptr->i);


pthread_t inc_x_thread;


if(pthread_create(&inc_x_thread, NULL, inc_x, E1_ptr))
{
printf("Error creating threadn");
}


if(pthread_join(inc_x_thread, NULL))
{
printf("Error joining threadn");
}

for(i = 0; i<(samples-1); i++)
{
if(pthread_cond_signal(&count_threshold_cv))
{
printf("Error Signaling thread at sample = %dn",i);
}
}


printf("aftert E1_ptr->a = %dt, E1_ptr->i = %dn",E1_ptr->a,E1_ptr->i);

pthread_mutex_destroy(&count_mutex);
pthread_cond_destroy(&count_threshold_cv);
pthread_exit (NULL);


return 0;
}


Doubt:



In the above code, the thread executes its function properly and exits.
Once the condition is applied, i.e. the below shown code is uncommented then,



pthread_cond_wait(&count_threshold_cv, &count_mutex);


Then the thread is stopped after 1st iteration of while loop as expected.
The signal is generated from main by the code shown below:



for(i  = 0; i<(samples-1); i++)
{
if(pthread_cond_signal(&count_threshold_cv))
{
printf("Error Signaling thread at sample = %dn",i);
}
}


observed that the signal is never sent.



Can someone please guide me, where am I going wrong. I'm a newbie to Pthreads.



Thanks in advance.










share|improve this question
























  • this is because you join before you signal. Join will wait till thread finishes. Thread will wait till you signal. this is a dead lock. Move join closer to the end of the program.
    – Serge
    Nov 10 at 13:06












  • thanks for suggestion serge, but after that also program had same behavior. It hangs after 1st iteration. Does signaling of thread is normal from main.
    – c_learner
    Nov 10 at 15:09















up vote
-1
down vote

favorite












The requirement for the sample application creating using pthread is given below:




  1. A single thread is created from main using Pthread

  2. Inside thread, Mutex is locked, a counter counts the value and while loop is incremented, While is set to the max count of 10.

  3. after while loop finishes, the mutex is unlocked.


The above requirement I have tried implementing using pthread



Code is shown below:



#include <pthread.h>
#include <stdio.h>
pthread_mutex_t count_mutex;
pthread_cond_t count_threshold_cv;

int samples = 10;
int count = 0;

struct example
{
int i;
int a;
};

void *inc_x(void *x_void_ptr)
{
pthread_mutex_lock(&count_mutex);
printf("Thread is locked n");

while(count < samples)
{
printf("inside While loop n");

struct example *E2_ptr;
E2_ptr = (struct example *)x_void_ptr;
printf("inside thread count = %dn",count);
E2_ptr->a = count;
E2_ptr->i = (count + 1);
count ++;
//pthread_cond_wait(&count_threshold_cv, &count_mutex);
}

pthread_mutex_unlock(&count_mutex);
printf ( "n Test Successful for Threadn");

pthread_exit(NULL);
}

int main()
{

int x = 100, y = 0,i = 0;
struct example *E1_ptr;

E1_ptr->a = 0;
E1_ptr->i = 0;

printf("Beforet E1_ptr->a = %dt, E1_ptr->i = %dn",E1_ptr->a,E1_ptr->i);


pthread_t inc_x_thread;


if(pthread_create(&inc_x_thread, NULL, inc_x, E1_ptr))
{
printf("Error creating threadn");
}


if(pthread_join(inc_x_thread, NULL))
{
printf("Error joining threadn");
}

for(i = 0; i<(samples-1); i++)
{
if(pthread_cond_signal(&count_threshold_cv))
{
printf("Error Signaling thread at sample = %dn",i);
}
}


printf("aftert E1_ptr->a = %dt, E1_ptr->i = %dn",E1_ptr->a,E1_ptr->i);

pthread_mutex_destroy(&count_mutex);
pthread_cond_destroy(&count_threshold_cv);
pthread_exit (NULL);


return 0;
}


Doubt:



In the above code, the thread executes its function properly and exits.
Once the condition is applied, i.e. the below shown code is uncommented then,



pthread_cond_wait(&count_threshold_cv, &count_mutex);


Then the thread is stopped after 1st iteration of while loop as expected.
The signal is generated from main by the code shown below:



for(i  = 0; i<(samples-1); i++)
{
if(pthread_cond_signal(&count_threshold_cv))
{
printf("Error Signaling thread at sample = %dn",i);
}
}


observed that the signal is never sent.



Can someone please guide me, where am I going wrong. I'm a newbie to Pthreads.



Thanks in advance.










share|improve this question
























  • this is because you join before you signal. Join will wait till thread finishes. Thread will wait till you signal. this is a dead lock. Move join closer to the end of the program.
    – Serge
    Nov 10 at 13:06












  • thanks for suggestion serge, but after that also program had same behavior. It hangs after 1st iteration. Does signaling of thread is normal from main.
    – c_learner
    Nov 10 at 15:09













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











The requirement for the sample application creating using pthread is given below:




  1. A single thread is created from main using Pthread

  2. Inside thread, Mutex is locked, a counter counts the value and while loop is incremented, While is set to the max count of 10.

  3. after while loop finishes, the mutex is unlocked.


The above requirement I have tried implementing using pthread



Code is shown below:



#include <pthread.h>
#include <stdio.h>
pthread_mutex_t count_mutex;
pthread_cond_t count_threshold_cv;

int samples = 10;
int count = 0;

struct example
{
int i;
int a;
};

void *inc_x(void *x_void_ptr)
{
pthread_mutex_lock(&count_mutex);
printf("Thread is locked n");

while(count < samples)
{
printf("inside While loop n");

struct example *E2_ptr;
E2_ptr = (struct example *)x_void_ptr;
printf("inside thread count = %dn",count);
E2_ptr->a = count;
E2_ptr->i = (count + 1);
count ++;
//pthread_cond_wait(&count_threshold_cv, &count_mutex);
}

pthread_mutex_unlock(&count_mutex);
printf ( "n Test Successful for Threadn");

pthread_exit(NULL);
}

int main()
{

int x = 100, y = 0,i = 0;
struct example *E1_ptr;

E1_ptr->a = 0;
E1_ptr->i = 0;

printf("Beforet E1_ptr->a = %dt, E1_ptr->i = %dn",E1_ptr->a,E1_ptr->i);


pthread_t inc_x_thread;


if(pthread_create(&inc_x_thread, NULL, inc_x, E1_ptr))
{
printf("Error creating threadn");
}


if(pthread_join(inc_x_thread, NULL))
{
printf("Error joining threadn");
}

for(i = 0; i<(samples-1); i++)
{
if(pthread_cond_signal(&count_threshold_cv))
{
printf("Error Signaling thread at sample = %dn",i);
}
}


printf("aftert E1_ptr->a = %dt, E1_ptr->i = %dn",E1_ptr->a,E1_ptr->i);

pthread_mutex_destroy(&count_mutex);
pthread_cond_destroy(&count_threshold_cv);
pthread_exit (NULL);


return 0;
}


Doubt:



In the above code, the thread executes its function properly and exits.
Once the condition is applied, i.e. the below shown code is uncommented then,



pthread_cond_wait(&count_threshold_cv, &count_mutex);


Then the thread is stopped after 1st iteration of while loop as expected.
The signal is generated from main by the code shown below:



for(i  = 0; i<(samples-1); i++)
{
if(pthread_cond_signal(&count_threshold_cv))
{
printf("Error Signaling thread at sample = %dn",i);
}
}


observed that the signal is never sent.



Can someone please guide me, where am I going wrong. I'm a newbie to Pthreads.



Thanks in advance.










share|improve this question















The requirement for the sample application creating using pthread is given below:




  1. A single thread is created from main using Pthread

  2. Inside thread, Mutex is locked, a counter counts the value and while loop is incremented, While is set to the max count of 10.

  3. after while loop finishes, the mutex is unlocked.


The above requirement I have tried implementing using pthread



Code is shown below:



#include <pthread.h>
#include <stdio.h>
pthread_mutex_t count_mutex;
pthread_cond_t count_threshold_cv;

int samples = 10;
int count = 0;

struct example
{
int i;
int a;
};

void *inc_x(void *x_void_ptr)
{
pthread_mutex_lock(&count_mutex);
printf("Thread is locked n");

while(count < samples)
{
printf("inside While loop n");

struct example *E2_ptr;
E2_ptr = (struct example *)x_void_ptr;
printf("inside thread count = %dn",count);
E2_ptr->a = count;
E2_ptr->i = (count + 1);
count ++;
//pthread_cond_wait(&count_threshold_cv, &count_mutex);
}

pthread_mutex_unlock(&count_mutex);
printf ( "n Test Successful for Threadn");

pthread_exit(NULL);
}

int main()
{

int x = 100, y = 0,i = 0;
struct example *E1_ptr;

E1_ptr->a = 0;
E1_ptr->i = 0;

printf("Beforet E1_ptr->a = %dt, E1_ptr->i = %dn",E1_ptr->a,E1_ptr->i);


pthread_t inc_x_thread;


if(pthread_create(&inc_x_thread, NULL, inc_x, E1_ptr))
{
printf("Error creating threadn");
}


if(pthread_join(inc_x_thread, NULL))
{
printf("Error joining threadn");
}

for(i = 0; i<(samples-1); i++)
{
if(pthread_cond_signal(&count_threshold_cv))
{
printf("Error Signaling thread at sample = %dn",i);
}
}


printf("aftert E1_ptr->a = %dt, E1_ptr->i = %dn",E1_ptr->a,E1_ptr->i);

pthread_mutex_destroy(&count_mutex);
pthread_cond_destroy(&count_threshold_cv);
pthread_exit (NULL);


return 0;
}


Doubt:



In the above code, the thread executes its function properly and exits.
Once the condition is applied, i.e. the below shown code is uncommented then,



pthread_cond_wait(&count_threshold_cv, &count_mutex);


Then the thread is stopped after 1st iteration of while loop as expected.
The signal is generated from main by the code shown below:



for(i  = 0; i<(samples-1); i++)
{
if(pthread_cond_signal(&count_threshold_cv))
{
printf("Error Signaling thread at sample = %dn",i);
}
}


observed that the signal is never sent.



Can someone please guide me, where am I going wrong. I'm a newbie to Pthreads.



Thanks in advance.







c linux multithreading pthreads






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 12:55









mehrdad-pedramfar

3,83711233




3,83711233










asked Nov 10 at 12:53









c_learner

1




1












  • this is because you join before you signal. Join will wait till thread finishes. Thread will wait till you signal. this is a dead lock. Move join closer to the end of the program.
    – Serge
    Nov 10 at 13:06












  • thanks for suggestion serge, but after that also program had same behavior. It hangs after 1st iteration. Does signaling of thread is normal from main.
    – c_learner
    Nov 10 at 15:09


















  • this is because you join before you signal. Join will wait till thread finishes. Thread will wait till you signal. this is a dead lock. Move join closer to the end of the program.
    – Serge
    Nov 10 at 13:06












  • thanks for suggestion serge, but after that also program had same behavior. It hangs after 1st iteration. Does signaling of thread is normal from main.
    – c_learner
    Nov 10 at 15:09
















this is because you join before you signal. Join will wait till thread finishes. Thread will wait till you signal. this is a dead lock. Move join closer to the end of the program.
– Serge
Nov 10 at 13:06






this is because you join before you signal. Join will wait till thread finishes. Thread will wait till you signal. this is a dead lock. Move join closer to the end of the program.
– Serge
Nov 10 at 13:06














thanks for suggestion serge, but after that also program had same behavior. It hangs after 1st iteration. Does signaling of thread is normal from main.
– c_learner
Nov 10 at 15:09




thanks for suggestion serge, but after that also program had same behavior. It hangs after 1st iteration. Does signaling of thread is normal from main.
– c_learner
Nov 10 at 15:09












1 Answer
1






active

oldest

votes

















up vote
0
down vote













count_mutex and count_threshold_cv are not initialized, add:



int main()
{
pthread_mutex_init(&count_mutex, NULL);
pthread_cond_init(&count_threshold_cv, NULL);
//...




E1_ptr is not initialized.
There are many ways to solve it:



You can call malloc to allocate memory:



struct example *E1_ptr = malloc(sizeof(struct example));

E1_ptr->a = 0;
E1_ptr->i = 0;


or holds pointer to local variable:



struct example ex;
struct example *E1_ptr = &ex; //malloc(sizeof(struct example));

E1_ptr->a = 0;
E1_ptr->i = 0;


or



struct example ex;
ex.a = 0;
ex.i = 0;


then create thread with pthread_create(&inc_x_thread, NULL, inc_x, &ex)





pthread_cond_signal function does not wait. If a thread is blocked by condition variable pthread_cond_signal function unblocks this thread, otherwise returns immediately without waiting and does nothing. So your for loop with 10 iterations is executed as soon as possible, without any waiting for pthread_cond_wait is called.
So can rewrite your for loop to infinite loop, calling pthread_cond_signal repeatedly.



if(pthread_create(&inc_x_thread, NULL, inc_x, E1_ptr)) {
printf("Error creating threadn");
}

while(1) { // INFINITE LOOP
if(pthread_cond_signal(&count_threshold_cv)) {
printf("Error Signaling thread at sample = %dn",i);
}

if (taskDone) // testing global flag, if 1 break
break; // it means inc_x thread has ended
}

if(pthread_join(inc_x_thread, NULL)) { // it was pointed out in comment
printf("Error joining threadn"); // you need to join at the end of main function
}


taskDone is global int, with 0 as default value. It is set to 1 before pthread_exit is called in inc_x function. Setting/checking taskDone should be wrapped with some synchronization mechanism, for example by adding new mutex or use count_mutex.






share|improve this answer























  • Thanks for answer. i was assuming that in each iteration of for loop i shall be calling the thread and finish its execution till another iteration of for loop is performed. was trying to achieve something like that
    – c_learner
    Nov 10 at 16:27












  • is there a way that same thread can be called again and again? or at each iteration of for loop i shall create a new thread?
    – c_learner
    Nov 10 at 16:31












  • if a thread was started you have 2 options: joins this thread or detaches it. You cannot call again the same thread, you need to create new one.
    – rafix07
    Nov 10 at 16:40













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239149%2fwhy-does-pthread-signal-from-main-hangs-the-code%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













count_mutex and count_threshold_cv are not initialized, add:



int main()
{
pthread_mutex_init(&count_mutex, NULL);
pthread_cond_init(&count_threshold_cv, NULL);
//...




E1_ptr is not initialized.
There are many ways to solve it:



You can call malloc to allocate memory:



struct example *E1_ptr = malloc(sizeof(struct example));

E1_ptr->a = 0;
E1_ptr->i = 0;


or holds pointer to local variable:



struct example ex;
struct example *E1_ptr = &ex; //malloc(sizeof(struct example));

E1_ptr->a = 0;
E1_ptr->i = 0;


or



struct example ex;
ex.a = 0;
ex.i = 0;


then create thread with pthread_create(&inc_x_thread, NULL, inc_x, &ex)





pthread_cond_signal function does not wait. If a thread is blocked by condition variable pthread_cond_signal function unblocks this thread, otherwise returns immediately without waiting and does nothing. So your for loop with 10 iterations is executed as soon as possible, without any waiting for pthread_cond_wait is called.
So can rewrite your for loop to infinite loop, calling pthread_cond_signal repeatedly.



if(pthread_create(&inc_x_thread, NULL, inc_x, E1_ptr)) {
printf("Error creating threadn");
}

while(1) { // INFINITE LOOP
if(pthread_cond_signal(&count_threshold_cv)) {
printf("Error Signaling thread at sample = %dn",i);
}

if (taskDone) // testing global flag, if 1 break
break; // it means inc_x thread has ended
}

if(pthread_join(inc_x_thread, NULL)) { // it was pointed out in comment
printf("Error joining threadn"); // you need to join at the end of main function
}


taskDone is global int, with 0 as default value. It is set to 1 before pthread_exit is called in inc_x function. Setting/checking taskDone should be wrapped with some synchronization mechanism, for example by adding new mutex or use count_mutex.






share|improve this answer























  • Thanks for answer. i was assuming that in each iteration of for loop i shall be calling the thread and finish its execution till another iteration of for loop is performed. was trying to achieve something like that
    – c_learner
    Nov 10 at 16:27












  • is there a way that same thread can be called again and again? or at each iteration of for loop i shall create a new thread?
    – c_learner
    Nov 10 at 16:31












  • if a thread was started you have 2 options: joins this thread or detaches it. You cannot call again the same thread, you need to create new one.
    – rafix07
    Nov 10 at 16:40

















up vote
0
down vote













count_mutex and count_threshold_cv are not initialized, add:



int main()
{
pthread_mutex_init(&count_mutex, NULL);
pthread_cond_init(&count_threshold_cv, NULL);
//...




E1_ptr is not initialized.
There are many ways to solve it:



You can call malloc to allocate memory:



struct example *E1_ptr = malloc(sizeof(struct example));

E1_ptr->a = 0;
E1_ptr->i = 0;


or holds pointer to local variable:



struct example ex;
struct example *E1_ptr = &ex; //malloc(sizeof(struct example));

E1_ptr->a = 0;
E1_ptr->i = 0;


or



struct example ex;
ex.a = 0;
ex.i = 0;


then create thread with pthread_create(&inc_x_thread, NULL, inc_x, &ex)





pthread_cond_signal function does not wait. If a thread is blocked by condition variable pthread_cond_signal function unblocks this thread, otherwise returns immediately without waiting and does nothing. So your for loop with 10 iterations is executed as soon as possible, without any waiting for pthread_cond_wait is called.
So can rewrite your for loop to infinite loop, calling pthread_cond_signal repeatedly.



if(pthread_create(&inc_x_thread, NULL, inc_x, E1_ptr)) {
printf("Error creating threadn");
}

while(1) { // INFINITE LOOP
if(pthread_cond_signal(&count_threshold_cv)) {
printf("Error Signaling thread at sample = %dn",i);
}

if (taskDone) // testing global flag, if 1 break
break; // it means inc_x thread has ended
}

if(pthread_join(inc_x_thread, NULL)) { // it was pointed out in comment
printf("Error joining threadn"); // you need to join at the end of main function
}


taskDone is global int, with 0 as default value. It is set to 1 before pthread_exit is called in inc_x function. Setting/checking taskDone should be wrapped with some synchronization mechanism, for example by adding new mutex or use count_mutex.






share|improve this answer























  • Thanks for answer. i was assuming that in each iteration of for loop i shall be calling the thread and finish its execution till another iteration of for loop is performed. was trying to achieve something like that
    – c_learner
    Nov 10 at 16:27












  • is there a way that same thread can be called again and again? or at each iteration of for loop i shall create a new thread?
    – c_learner
    Nov 10 at 16:31












  • if a thread was started you have 2 options: joins this thread or detaches it. You cannot call again the same thread, you need to create new one.
    – rafix07
    Nov 10 at 16:40















up vote
0
down vote










up vote
0
down vote









count_mutex and count_threshold_cv are not initialized, add:



int main()
{
pthread_mutex_init(&count_mutex, NULL);
pthread_cond_init(&count_threshold_cv, NULL);
//...




E1_ptr is not initialized.
There are many ways to solve it:



You can call malloc to allocate memory:



struct example *E1_ptr = malloc(sizeof(struct example));

E1_ptr->a = 0;
E1_ptr->i = 0;


or holds pointer to local variable:



struct example ex;
struct example *E1_ptr = &ex; //malloc(sizeof(struct example));

E1_ptr->a = 0;
E1_ptr->i = 0;


or



struct example ex;
ex.a = 0;
ex.i = 0;


then create thread with pthread_create(&inc_x_thread, NULL, inc_x, &ex)





pthread_cond_signal function does not wait. If a thread is blocked by condition variable pthread_cond_signal function unblocks this thread, otherwise returns immediately without waiting and does nothing. So your for loop with 10 iterations is executed as soon as possible, without any waiting for pthread_cond_wait is called.
So can rewrite your for loop to infinite loop, calling pthread_cond_signal repeatedly.



if(pthread_create(&inc_x_thread, NULL, inc_x, E1_ptr)) {
printf("Error creating threadn");
}

while(1) { // INFINITE LOOP
if(pthread_cond_signal(&count_threshold_cv)) {
printf("Error Signaling thread at sample = %dn",i);
}

if (taskDone) // testing global flag, if 1 break
break; // it means inc_x thread has ended
}

if(pthread_join(inc_x_thread, NULL)) { // it was pointed out in comment
printf("Error joining threadn"); // you need to join at the end of main function
}


taskDone is global int, with 0 as default value. It is set to 1 before pthread_exit is called in inc_x function. Setting/checking taskDone should be wrapped with some synchronization mechanism, for example by adding new mutex or use count_mutex.






share|improve this answer














count_mutex and count_threshold_cv are not initialized, add:



int main()
{
pthread_mutex_init(&count_mutex, NULL);
pthread_cond_init(&count_threshold_cv, NULL);
//...




E1_ptr is not initialized.
There are many ways to solve it:



You can call malloc to allocate memory:



struct example *E1_ptr = malloc(sizeof(struct example));

E1_ptr->a = 0;
E1_ptr->i = 0;


or holds pointer to local variable:



struct example ex;
struct example *E1_ptr = &ex; //malloc(sizeof(struct example));

E1_ptr->a = 0;
E1_ptr->i = 0;


or



struct example ex;
ex.a = 0;
ex.i = 0;


then create thread with pthread_create(&inc_x_thread, NULL, inc_x, &ex)





pthread_cond_signal function does not wait. If a thread is blocked by condition variable pthread_cond_signal function unblocks this thread, otherwise returns immediately without waiting and does nothing. So your for loop with 10 iterations is executed as soon as possible, without any waiting for pthread_cond_wait is called.
So can rewrite your for loop to infinite loop, calling pthread_cond_signal repeatedly.



if(pthread_create(&inc_x_thread, NULL, inc_x, E1_ptr)) {
printf("Error creating threadn");
}

while(1) { // INFINITE LOOP
if(pthread_cond_signal(&count_threshold_cv)) {
printf("Error Signaling thread at sample = %dn",i);
}

if (taskDone) // testing global flag, if 1 break
break; // it means inc_x thread has ended
}

if(pthread_join(inc_x_thread, NULL)) { // it was pointed out in comment
printf("Error joining threadn"); // you need to join at the end of main function
}


taskDone is global int, with 0 as default value. It is set to 1 before pthread_exit is called in inc_x function. Setting/checking taskDone should be wrapped with some synchronization mechanism, for example by adding new mutex or use count_mutex.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 10 at 16:14

























answered Nov 10 at 16:04









rafix07

6,0681613




6,0681613












  • Thanks for answer. i was assuming that in each iteration of for loop i shall be calling the thread and finish its execution till another iteration of for loop is performed. was trying to achieve something like that
    – c_learner
    Nov 10 at 16:27












  • is there a way that same thread can be called again and again? or at each iteration of for loop i shall create a new thread?
    – c_learner
    Nov 10 at 16:31












  • if a thread was started you have 2 options: joins this thread or detaches it. You cannot call again the same thread, you need to create new one.
    – rafix07
    Nov 10 at 16:40




















  • Thanks for answer. i was assuming that in each iteration of for loop i shall be calling the thread and finish its execution till another iteration of for loop is performed. was trying to achieve something like that
    – c_learner
    Nov 10 at 16:27












  • is there a way that same thread can be called again and again? or at each iteration of for loop i shall create a new thread?
    – c_learner
    Nov 10 at 16:31












  • if a thread was started you have 2 options: joins this thread or detaches it. You cannot call again the same thread, you need to create new one.
    – rafix07
    Nov 10 at 16:40


















Thanks for answer. i was assuming that in each iteration of for loop i shall be calling the thread and finish its execution till another iteration of for loop is performed. was trying to achieve something like that
– c_learner
Nov 10 at 16:27






Thanks for answer. i was assuming that in each iteration of for loop i shall be calling the thread and finish its execution till another iteration of for loop is performed. was trying to achieve something like that
– c_learner
Nov 10 at 16:27














is there a way that same thread can be called again and again? or at each iteration of for loop i shall create a new thread?
– c_learner
Nov 10 at 16:31






is there a way that same thread can be called again and again? or at each iteration of for loop i shall create a new thread?
– c_learner
Nov 10 at 16:31














if a thread was started you have 2 options: joins this thread or detaches it. You cannot call again the same thread, you need to create new one.
– rafix07
Nov 10 at 16:40






if a thread was started you have 2 options: joins this thread or detaches it. You cannot call again the same thread, you need to create new one.
– rafix07
Nov 10 at 16:40




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239149%2fwhy-does-pthread-signal-from-main-hangs-the-code%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

鏡平學校

ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

Why https connections are so slow when debugging (stepping over) in Java?