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:
- A single thread is created from main using Pthread
- Inside thread, Mutex is locked, a counter counts the value and while loop is incremented, While is set to the max count of 10.
- 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
add a comment |
up vote
-1
down vote
favorite
The requirement for the sample application creating using pthread is given below:
- A single thread is created from main using Pthread
- Inside thread, Mutex is locked, a counter counts the value and while loop is incremented, While is set to the max count of 10.
- 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
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
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
The requirement for the sample application creating using pthread is given below:
- A single thread is created from main using Pthread
- Inside thread, Mutex is locked, a counter counts the value and while loop is incremented, While is set to the max count of 10.
- 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
The requirement for the sample application creating using pthread is given below:
- A single thread is created from main using Pthread
- Inside thread, Mutex is locked, a counter counts the value and while loop is incremented, While is set to the max count of 10.
- 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
c linux multithreading pthreads
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
add a comment |
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
add a comment |
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
.
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
add a comment |
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
.
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
add a comment |
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
.
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
add a comment |
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
.
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
.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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